List of Practicals
1. Java Basics
a. Write a Java program that takes a number as input and prints its multiplication
table upto 10.
b. Write a Java program to display the following pattern.
*****
****
***
**
*
c. Write a Java program to print the area and perimeter of a circle.
2. Use of Operators
a. Write a Java program to add two binary numbers.
b. Write a Java program to convert a decimal number to binary number and vice
versa.
c. Write a Java program to reverse a string.
3. Java Data Types
a. Write a Java program to count the letters, spaces, numbers and other characters
of an input string.
b. Implement a Java function that calculates the sum of digits for a given char array
consisting of the digits '0' to '9'. The function should return the digit sum as a
long value.
c. Find the smallest and largest element from the array
4. Methods and Constructors
a. Designed a class SortData that contains the method asec() and desc().
b. Designed a class that demonstrates the use of constructor and destructor.
c. Write a java program to demonstrate the implementation of abstract class.
5. Inheritance
a. Write a java program to implement single level inheritance.
b. Write a java program to implement method overriding
c. Write a java program to implement multiple inheritance.
INDEX
Practical 1
A] Aim: Write a program which takes a number as input from
user and prints its table up to 10
import [Link];
public class prac1 {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Input a number: ");
int num1 = [Link]();
for (int i=0; i< 10; i++)
{
[Link](num1 + " x " + (i+1) + " = " + (num1 *
(i+1)));
}
}
}
output:
B]Aim:- Write a Java program to display the following pattern.
*
**
***
****
*****
public class prog121 {
public static void main(String[] args) {
int i, j;
for (i = 1; i <= 6; i++) {
for (j = 1; j < i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Output:
C]Aim:write a program to print area and perimeter of the circle.
Program:
import [Link];
public class prog3 {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter the radius");
double radius=[Link]();
double perimeter=2*[Link]*radius;
double area=[Link]*radius*radius;
[Link]("perimeter is ="+perimeter);
[Link]("Area is="+area);
}
}
Output:
Practical 2
A]Aim: Write a Java program to addition of the two binary
numbers.
Program:
package [Link];
import [Link];
public class prog2a {
public static void main (String[] args){
Scanner sc = new Scanner([Link]);
int[] arr=new int[8];
int i=0,m=0,n=0,sum=0;
[Link] ("enter 1st binary number");
int n1 = [Link]();
[Link] ("enter 2nd binary number");
int n2 = [Link]();
for(i=[Link]-1;i>=0;i--){
m=n1%10;
n=n2%10;
n1=n1/10;
n2=n2/10;
sum=m+n+arr[i];
if(sum==1){
arr[i]=sum;
}
else if(sum==2){
arr[i]=0;
arr[i-1]=1;
}
else if(sum==3){
arr[i]=1;
arr[i-1]=1;
}
else{
arr[i]=0;
}
}
[Link]("Addition of two binary numbers is");
for(i=0;i<[Link];i++){
if(i==4){
[Link](" ");
}
[Link](arr[i]);
}
}
}
Output:
B]Aim: Write a program to convert a decimal number to binary
number and binary to decimal.
Program:
import [Link];
public class prog2b {
public static void main(String arg[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter a decimal number");
int n=[Link]();
[Link]("Binary number is : ");
binary(n);
}
static void binary(int num){
int i = 0;
int bin[]=new int[100];
bin[0]=0;
while(num>0){
bin[i++] = num%2;
num = num/2;
}
for(int j =i-1;j >= 0;j--){
[Link](bin[j]);
}
}
}
Output:
C]Aim: Write a program in Java to reverse a string.
Program:
import [Link];
public class prog2c {
public static void main(String args[]){
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("Enter a string to reverse");
original = [Link]();
int length = [Link]();
for ( int i = length - 1 ; i >= 0 ; i-- ){
reverse = reverse + [Link](i);
}
[Link]("Reverse of entered string is: "
+ reverse);
}
Output:
Practical 3
A]Aim: Write a Java program to count the letters, spaces,
numbers and other characters of an input string.
Program:
public class prog3a {
public static void main(String[] args){
String test = "core java";
count(test);
}
public static void count(String x){
char[] ch = [Link]();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < [Link](); i++){
if([Link](ch[i])){
letter ++ ;
}
else if([Link](ch[i])){
num ++ ;
}
else if([Link](ch[i])){
space ++ ;
}
else{
other ++;
}
}
Output:
B]Aim: Implement a Java function that caculates the sum of
digits for a given char array consisting fo the digits ‘0’ to ‘9’. The
function should return the digit sum as a long value.
Program:
public class prog3b {
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
[Link]("Please enter a number to calculate sum of digits");
int number = [Link]();
int sum = 0;
int input = number;
while (input != 0){
int lastdigit = input % 10;
sum += lastdigit;
input /= 10;
}
[Link]("Sum of digits of number %d is %d", number, sum);
[Link]();
}
}
Output:
C]Aim: Find the smallest and largest element from the array.
Program:
public class prog3c {
public static void main(String[] args){
int a[] = new int[] { 23, 34, 13, 64, 72, 90, 10,
15, 9, 27 };
int min = a[0]; // assume first elements as smallest number
int max = a[0]; // assume first elements as largest number
for (int i = 1; i < [Link]; i++) // iterate forloop from arrays 1st index (second
element)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
[Link]("Largest Number in a given array is : " + max);
[Link]("Smallest Number in a given array is : " + min);
}
}
Output:
Practical 4
A]Aim: Design a class Sort Data that contains the method
asec() and desc().
Program:
import [Link];
import [Link];
public class sortData {
void asec(){
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983
, 1999, 1987};
// before sorting
[Link]("Integer[] Array - before sorting : ");
for(Integer iValue : intArray){
[Link](iValue);
}
// sorting int[] array in ascending order
[Link](intArray);
// after sorting
[Link]("\nInteger[] Array - after sorting in ascending order : ");
for(Integer iValue : intArray) {
[Link](iValue);
}
}
void desc(){
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
[Link]("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) {
[Link](iValue);
}
// sorting int[] array in descending order
[Link](intArray, [Link]());
// after sorting
[Link]("\nInteger[] Array - after sorting in descending order : ");
for(Integer iValue : intArray) {
[Link](iValue);
}
}
public static void main(String[] args) {
sortData ai=new sortData();
sortData a2=new sortData();
[Link]();
[Link]();
}
}
Output:
B]Aim: Derive a class that demonstrate the use fo constructor
and destructor.
Program:
public class program4b {
program4b(){
[Link]("Hello");
}
public static void main(String args[]){
program4b c1=new program4b();
c1=null;
[Link]();
}
public void finalize(){
[Link]("Destroyed");
}
Output:
(C)Aim: Write a java program to demonstrate the
implementation of abstract class.
Program:
abstract class calc
{
abstract int sqr(int n1);
abstract int cube(int n1);
void show()
{
[Link]("Hello");
}
}
public class pract4 extends calc {
int sqr ( int n1)
{
return n1 * n1;
}
int cube ( int n1)
{
return n1 * n1 * n1;
}
public static void main (String args[])
{
pract4 p1 = new pract4();
[Link]([Link](333));
[Link]([Link](444));
[Link]();
}
}
Output:
Practical 5
A)Aim: Write a java program to implement single level
inheritance.
Program: -
public class Animal {
void eat(){
[Link]("eating...");
}
}
class Dog extends Animal{
void bark(){[Link]("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
[Link]();
}
}
Output:
B)Aim: Write a java program to implement method overriding.
Program: -
public class java5b {
//Overridden method
public void eat(){
[Link]("Human is eating");
}
}
class Boy extends java5b{
//Overriding method
public void eat(){
[Link]("Boy is eating");
}
public static void main( String args[]){
Boy obj = new Boy();
[Link]();
}
}
Output:
C]Aim: Write a java program to implement multiple inheritance.
Program:
interface Backend {
// abstract class
public void connectServer();
}
public class Frontend {
public void responsive(String str) {
[Link](str + " can also be used as frontend.");
}
public static void main(String[] args) {
// create object of Language class
Language java = new Language();
[Link]();
// call the inherited method of Frontend class
[Link]([Link]);
}
}
// Language extends Frontend class
// Language implements Backend interface
class Language extends Frontend implements Backend {
String language = "Java";
// implement method of interface
public void connectServer() {
[Link](language + " can be used as backend language.");
}
}
Output: