Object Oriented Programming Through Java Laboratory Record

Object Oriented Programming Through Java Lab Record
For
I Yr. MCA II Semester
Compiled by Mrs. G.KALPANA, Asst.Professor in MCA Department
3
2014-2015
[Pick the date]

Department of MASTER OF COMPUTER APPLICATIONS

Chaitanya Bharathi Institute of Technology (Autonomous)

Gandipet, Hyderabad-75.


INDEX

S.NO / PROGRAMME NAME / P.NO.
I / Basic Programs / 4
1)Write java program to print Biggest of 3 Numbers using Logical Operators / 4
2) write a java program to print first 10 numbers in fibonacci series / 5
3) Write a java program to print Factorial of a given number / 6

4)Write java program to print the following o/p

/ 7
5) Write a java program to print sum of Sum of Digits / 8
6) Write a java Program for swapping two numbers / 9
7)Wrte a java program to print primes up to the given prime number / 10
8) Write java program to check given string is a palindrome or not / 11
9) wajp to print sum of n terms in the series 1/1! +1/2!+1/3!..... / 12
10) Write A Java Program to print Quadratic roots using command line arguments / 13
11) Write a java program to print the names in sorted order using arrays / 14
12) Write a java program to print multiplication table using arrays / 15
II / Method Overloading / 18
1)Write a java program to demonstrate method overloading / 18
2)Write a java program to find the volume of a Box using method overloading with different number of perameters. / 19
III / Constructor overloading : / 22
1) Write a java program to illustrate the concept of constructors and its overloading. / 22
2) Write a java program for Rectangle class using constructor overloading with different no. of parameter list / 24
3) Wajp to copy the values of one object into another using constructor. / 26
S.NO / PROGRAMME NAME / P.NO
IV / Inheritence / 28
1) Wajp for Rectange class using Simple Inheritance / 28
2) Write a Java program to demonstrate multilevel inheritance. / 30
3) Write a Java program to implement the following hierarchy and find area and perimeter Abstract / 31
V / Method Overriding / 35
1)Write a java program for Bank class using Method Overriding. / 36
2) Write a java program to demonstrate Method overriding (use super keyword) / 38
VI / Dynamic Method Dispatch / 39
1)Write a Java program to demonstrate dynamic dispatch. / 40
2) Wajp for Bank class using Dynamic Method Dispatch / 41
VII / Abstract Class: / 43
1) Write a Java program to implement a Vehicle Abstract class. / 43
2)Wajp to demonstrate the concept of abstract class / 46
VIII / Packages: / 48
1)Write a Java program to demonstrate use of user defined packages. / 49
2) write a java package for book class and then import and display the result. / 50
3) write a java program to find the cube of a number for various data types using package and then import and display the results. / 51
IX / Interfaces: / 53
1) Write a Java program to illustrate the multiple inheritance by using Interfaces. / 53
X / Super , Static, final key words : / 56
1)Write a java program to illustrate the keywords i)super ii)static iii)final / 56
S.NO / PROGRAMME NAME / P.NO
XI / Exception handling: / 57
1)wajp to demonstrate simple example for exception handling / 57
2)Wajp to demonstrate exception handling with multiple catch blocks / 58
3) wajp using NumberFormat exception / 60
4)Wajp for user defined exception / 61
XII / How to make executable jar files in JDK1.3.1? / 63
XIII / Multithreading / 64
1) Write a Java program to demonstrate the concept of daemon threads / 64
2) Write a Java program to demonstrate the concept of synchronization by suitable example / 66
3) Write a Java program to demonstrate the concept of Inter thread communication by Suitable example / 68
4) Write a program to demonstrate string tokenizer. / 70
XIV) / File I/O and Streams / 73
1)Wajp to Demonstration of FileOutputStream and PrintStream classes / 74
2) Write a java program to Write bytes to a file / 75
3) Write a java program to copy bytes from one file to another. / 76
XV / Applets / 79
1) Wajp for Sum of Two Numbers using Applet / 79
2)Wajp for Applet using drawstring(), drawRect() and drawOval() / 80
3) Write a Java program to demonstrate banner applet. / 81
4) wajp for Bouncing of a Ball using applet / 83
XIV /

AWT: 1 ) Wajp that prints a message by clicking on the button using AWT events and applets 2) GUI with controls menus and event handling.

/ 87

Java lab programmes

I.  Basic programs:

1)  Write java program to print Biggest of 3 Numbers using Logical Operators

Importjava.util.*;

class Biggest3

{

public static void main(String args[])

{

int n1, n2, n3, big;

Scanner scan= new Scanner(System.in);

System.out.print("Please Enter No 1: ");

n1=scan.nextInt();

System.out.print("Please Enter No 2: ");

n2=scan.nextInt();

System.out.print("Please Enter No 3: ");

n3=scan.nextInt();

if(n1>n2 & n1>n3)

big=n1;

else if(n2>n1 & n2>n3)

big=n2;

else

big=n3;

System.out.println("Biggest No: " + big);

}

}

O/P:

2) write a java program to print first 10 numbers in fibonacci series

classFibo

{

public static void main(String args[])

{

inta,b,temp,n;

a=0;

b=1;

for(n=1;n<=10;n++)

{

System.out.println(a);

temp=a+b;

a=b;

b=temp;

}

}

}s

o/p :

3) Write a java program to print Factorial of a given number

Importjava.util.*;

class Factorial

{

public static void main(String args[])

{ int n, i, fact=1;

Scanner scan= new Scanner(System.in);

System.out.print("Please Enter a No.");

n=scan.nextInt();

for(i=n;i>=1;i--)

{

fact=fact*i ;

}

System.out.println("Factorial of " + n + " is " + fact);

}

}

O/P:

4)Write java program to print the following o/p

Following star pattern is printed
*
**
***
****
*****

class Stars

{

Public static voidmain(String[]args)

{

int row, numberOfStars;

for(row =1; row <=10; row++)

{

for(numberOfStars=1;numberOfStars<=row;

numberOfStars++)

{

System.out.print("*");

}

System.out.println();// Go to next line

}

}

}

Output of program:

5) Write a java program to print sum of Sum of Digits

Logic: 513 -> 5+1+3=9

n / res / n / sum
513 / 0
513%10 / 3 / 3
513/10 / 51 / 3
51%10 / 1 / 4
51/10 / 5 / 4
5%10 / 5 / 9
5/10 / 0 / 9

importjava.util.*;

classSumDigits

{

public static void main(String args[])

{

int n, res,sum=0;

Scanner scan= new Scanner(System.in);

System.out.print("Please Enter No. = ");

n=scan.nextInt();

System.out.print("Sum of digits of a number" +n+ "is = "); while(n>0)

{

res=n%10;

n=n/10;

sum=sum+res;

}

System.out.print(+sum);

}

}

o/p:

6) Write a java Program for swapping two numbers

importjava.util.*;

class Swap

{

public static void main(String args[])

{

int n1, n2, temp;

Scanner scan= new Scanner(System.in);

System.out.print("Please Enter No 1:=");

n1=scan.nextInt();

System.out.print("Please Enter No 2: =");

n2=scan.nextInt();

temp=n1;

n1=n2;

n2=temp;

System.out.println("First No: " + n1);

System.out.println("Second No: " + n2);

}

}

o/p:

7)Wrte a java program to print primes up to the given prime number

importjava.util.*;

public class Prime

{

public static void main(String args[])

{

inti,j,p=1;

Scanner sc =new Scanner(System.in);

System.out.print("enter a number up to which u want print primes =");

int n=sc.nextInt();

for(i=2;i<=n;i++)

{

p=1;

for(j=2;j<i;j++)

{

if(i%j==0)

{

p=0;

}}

if(p==1)

System.out.println(j);

}

}

}

o/p:

8) Write java program to check given string is a palindrome or not

importjava.util.*;

public class Palin

{

public static void main(String args[])

{

System.out.print("enter strings = ");

Scanner sc=new Scanner(System.in);

String s=sc.next();

StringBuffer tmp=new StringBuffer(s);

tmp.reverse();

String str2;

str2=new String(tmp);

if(s.equals(str2))

System.out.println("the given string " + s + " is palindrome");

else

System.out.println("the given string " + s + " is not palindrome");

}

}

O/P:

9) wajp to print sum of n terms in the series 1/1! +1/2!+1/3!.....

importjava.util.Scanner;

classSeriesfact

{

public static void main(String args[])

{

int i;

float sum=0,f=1;

System.out.println("enter number of terms in the series");

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

for(i=1;i<=n;i++)

{ for(f=1;f<=i;f++)

{

f=f*i;

sum=sum + (float)1/f;

}

}

System.out.println("sum of" +n +"terms is = " +sum);

}

}

o/p:

10) Write A Java Program to print Quadratic roots using command line arguments

importjava.lang.*;

class Quadratic

{

public static void main(String args[])

{

inta,b,c,d;

double r1,r2;

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]);

c=Integer.parseInt(args[2]);

d=b*b-4*a*c;

if(d==0)

{

r1=r2=-(float)b/(2*a);

System.out.println("the roots are real equal =" +r1 + " and r2 = " +r2);

}

else if(d>0)

{

double t=Math.sqrt(d);

r1=(-b+t)/(2*a);

r2=(+b+t)/(2*a);

System.out.println("the roots are real and distict \n r=" +r1 +" and r2=" +r2);

}

else if(d<0)

{

System.out.println("the roots are imaginary and there is no real solution ");

}

}}

o/p:

11) Write a java program to print the names in sorted order using arrays

import java.lang.*;

class Stringsort

{

static String name[]={"Bombay" , "Madras" ,"Delhi" ,"Pune"};

public static void main(String args[])

{

int size=name.length;

String temp=null;

for(int i=0;i<size;i++)

{

for(int j=i+1;j<size;j++)

{

if ( name[j].compareTo(name[i])<0)

{

temp=name[i];

name[i]=name[j];

name[j]=temp;

}

}

}

System.out.println("soted names are ");

for(int i=0;i<size;i++)

{

System.out.println(name[i]);

}

}

}

o/p

12) Write a java program to print multiplication table using arrays

import java.util.Scanner;

import java.io.*;

import java.lang.*;

class MatrixMul

{

public static void main(String[] args)

{

int i= 0,j=0,k=0,p,q,m,n;

int a[][] = new int[10][10];

int b[][] = new int[10][10],c[][] = new int[10][10];

Scanner sc = new Scanner(System.in);

System.out.print("Enter no of rows in the A matrix= ");

p=sc.nextInt();

System.out.print("Enter a no of columns in the A matrix = ");

q=sc.nextInt();

System.out.print("Enter no of rows in the B matrix= ");

m = sc.nextInt();

System.out.print("Enter no of columns in the B matrix= ");

n = sc.nextInt();

if(q==m)

{

System.out.print ("Enter A matrix values :");

for(i=0;i<p;i++)

{

for(j=0; j<q ;j++)

{

a[i][j] = sc.nextInt() ;

}

}

System.out.println("\nMatrix A values are ");

for(i=0;i<p;i++)

{

for(j=0;j<q;j++)

{

System.out.print(" "+a[i][j]) ;

}

System.out.println();

}

System.out.println("Enter matrix B values :");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

b[i][j] = sc.nextInt() ;

}

}

System.out.println("\nMatrix B values ");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

System.out.print(" "+b[i][j]) ;

}

System.out.println();

}

System.out.println("\n C Matrix is C = A * B Result");

if(q == m)

{

for(i=0;i<p;i++)

{

for(j=0;j<n;j++)

{

c[i][j]= 0 ;

for(k=0;k<q;k++)

{

c[i][j] = c[i][j] + (a[i][k] * b[k][j]) ;

}

}

}

}

// for C matrix Printing

for(i=0;i<p;i++)

{

for(j=0;j<n;j++)

{

System.out.print(" "+c[i][j]) ;

}

System.out.println();

}

}else

{

System.out.println("Matrix multiplication not possible");

}

}

}

O/P:

13) Write a java programme for bubble sort using arrays

class Bubble

{

public static void main(String args[])

{

int[] nums={12,87,9,-4,76,34,890,543,77,02};

int a,b,t;

int size;

size=10;

System.out.print("original array is");

for (int i=0;i<size;i++)

System.out.print(" " +nums[i]);

System.out.println();

for(a=1;a<size;a++)

for(b=size-1;b>=a;b--)

{

if(nums[b-1]>nums[b])

{

t=nums[b-1];

nums[b-1]=nums[b];

nums[b]=t;

}

}

System.out.print("sorted array is ");

for(int i=0;i<size;i++)

System.out.print(" "+nums[i]);

System.out.println();

}

}

Output:

II. Write a java program to demonstrate classes and objects.

class Vehicle

{

int pass;

int fcapacity;

int speed;

}

class MainDemo

{

public static void main(String args[])

{

Vehicle carobj=new Vehicle();

Vehicle busobj=new Vehicle();

Vehicle vanobj=new Vehicle();

int distance;

carobj.pass=6;

carobj.fcapacity=26;

carobj.speed=60;

distance=carobj.fcapacity*carobj.speed;

System.out.println("car can carry: "+carobj.pass+" passengers with fuel capacity

"+carobj.fcapacity+"liter and distance it travels"+distance+"km");

busobj.pass=26;

busobj.fcapacity=260;

busobj.speed=40;

distance=busobj.fcapacity*busobj.speed;

System.out.println();

System.out.println("bus can carry "+busobj.pass+" passengers with fuel capacity

"+busobj.fcapacity+"liter and distance it travels "+distance+"km");

vanobj.pass=15;

vanobj.fcapacity=100;

vanobj.speed=80;

distance=vanobj.fcapacity*vanobj.speed;

System.out.println();

System.out.println("van can carry"+vanobj.pass+"passengers with fuel capacity

"+vanobj.fcapacity +"liter and distance it travels "+distance+"km");

}}

Output:

II.  Method Overloading

Method overloading means when two or more methods have the same name but a different signature. Signature of a method is nothing but a combination of its name and the sequence of its parameter types.

1) Write a java program to demonstrate method overloading

Program:

class OverloadDemo

{

void max(float a, float b)

{

System.out.println("\nmax method with float argument invoked");

if(a>b)

System.out.println(a+" is Greater");

else

System.out.println(b+" is Greater");

}

void max(double a, double b)

{

System.out.println("max method with double arg invoked");

if(a>b)

System.out.println(a+" a is Greater");

else

System.out.println(b+" b is Greater");

}

max(long a, long b)

{

System.out.println("\nmax method with long arg invoked");

if(a>b)

System.out.println(a+" a is Greater");

else System.out.println(b+" b is Greater");

}

public static void main(String[] args)

{

OverloadDemo o=new OverloadDemo();

o.max(23L,12L);

o.max(2,3);

o.max(54.0,35f);

}

}

o/p:

2)Write a java program to find the volume of a Box using method overloading with different number of perameters.

class Box

{

int width,breadth,height;

void getdata(int length)

{

width=breadth=height=length;

}

void getdata(int a,int b)

{ width=a;

breadth=height=b;

}

void getdata(int x,int y, int z)

{ width=x;

breadth=y;

height=z;

}

int volume()

{

System.out.println(" ");

return width*breadth*height;

}}

class MainBox

{ public static void main(String args[])

{ int vol;

Box mybox1=new Box();

Box mybox2=new Box();

Box mybox3=new Box();

mybox1.getdata(10,20,30);

vol=mybox1.volume();

System.out.println("volume of my box with 3 perameters is = " +vol); mybox2.getdata(10,20);

vol=mybox2.volume();

System.out.println("volume of my box with 2 perameters is = " +vol);

mybox3.getdata(5);

vol=mybox3.volume();

System.out.println("volume of my box with 1 perameter is = " +vol);

}

}

o/p:

3)Wajp for overloading using variable arguments methods.

class Vararg

{

static void vaTest(int ...v)

{

System.out.println();

System.out.print(" first method invoked :" +"no of args :" +v.length +" and the contents are :");