OOPS With JAVA Laboratory (22CSL38)
1.a. write a java program to implement class mechanism and create object to access the
members of class.
import [Link].*;
class Addition
{
int sum = 0;
public int addTwoInt(int a, int b)
{
// adding two integer value.
sum = a + b;
//returning summation of two values.
return sum;
}
class GFG
{
public static void main (String[ ] args)
{
// creating an instance of Addition class
Addition add = new Addition( );
// calling addTwoInt() method to add two integer using instance created
// in above step.
int s = [Link](1,2);
[Link]("Sum of two integer values :"+ s);
}
}
Output :
Sum of two integer values : 3
Dept. of Computer Science and Design 1
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
1.b. Write a java program to implement shift operators in java.
public class operators
{
public static void main(String[] args)
{
int a = 5;
int b = -10;
[Link]("a<<2 = "+ (a << 2));
[Link]("b>>2 = "+ (b >> 2));
[Link]("b>>>2 = "+ (b >>> 2));
}
}
Output :
a<<2 = 20
b>>2 = -3
b>>>2 = 1073741821
Dept. of Computer Science and Design 2
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
2. a. Write a Java program to illustrate Type Casting of the datatype and type
conversion.
class Conversion
public static void main(String args[])
byte b;
int i = 257;
[Link]("\nConversion of int to byte.");
b = (byte) i;
[Link]("i and b " + i + " " + b);
[Link]("\nConversion of double to int.");
i = (int) d;
[Link]("d and i " + d + " " + i);
[Link]("\nConversion of double to byte.");
Output:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Dept. of Computer Science and Design 3
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
2.b. write a java program to implement for-each loop to compute average of n natural
numbers.
Class ForEach
Public static void main (string args[ ] )
Intnums [ ] = { 1, 2, 3, 4 , 5 , 6, 7, 8, 9, 10 };
Int sum=0;
For(int x : nums )
[Link](“value is : “+ x);
Sum += x;
[Link](“summation: “+sum);
Output:Value is : 1
Value is : 2
Value is : 3
Value is : 4
Value is : 5
Value is : 6
Value is : 7
Value is : 8
Value is : 9
Value is : 10
Summation : 55
Dept. of Computer Science and Design 4
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
Dept. of Computer Science and Design 5
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
[Link] a program in Java to demonstrate method overloading using iterative
statements.
class MethodOverloadingExample
static int addition(int x, int y)
return x + y;
static double addition(int x, double y)
return x + y;
public static void main(String[] args)
int a = addition(8, 8);
double b = addition(4, 5.2);
[Link]("addition of integers: " + a);
[Link]("addition of double values: " + b);
Output:
addition of integers: 16
addition of integers and double values: 9.2
Dept. of Computer Science and Design 6
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
3.b. Write a program in Java to demonstrate constructor overloading using this
keyword.
public class OverloadingExample2
private int rollNum;
OverloadingExample2()
rollNum =100;
OverloadingExample2(int rnum)
this();
/*this() is used for calling the default
* constructor from parameterized constructor.
* It should always be the first statement
* inside constructor body.
*/
rollNum = rollNum+ rnum;
public int getRollNum()
return rollNum;
public void setRollNum(int rollNum)
[Link] = rollNum;
Dept. of Computer Science and Design 7
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
public static void main(String args[])
OverloadingExample2 obj = new OverloadingExample2(12);
[Link]([Link]());
Output:
112
Dept. of Computer Science and Design 8
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
4.a. Write a program in java that implement multilevel inheritance.
Class name
String name=”swathi”;
Int age = 20;
Class Mark extends Name
Int m1=30,m2=30,m3=30;
Class Student extends Mark
Int total;
Void calc(_
Total=m1+m2+m3;
Void show()
[Link](“\n NAME: “ +name+”\n AGE:”+age+”\n
MARK1=”+m1+”\n MARK2=”+m2+”\n MARK3=”+m3+”\n TOTAL:”+total);
Dept. of Computer Science and Design 9
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
Class Multilevelinheritance
Public static void main(string args[])
Student ob=new student();
[Link]();
[Link]( );
Output:
NAME: swathi
AGE:20
MARK1=30
MARK2=30
MARK3=30
TOTTAL:90
Dept. of Computer Science and Design 10
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
4.b . Write a Java program to implement method overriding that shows use of super
keyword.
class GFG
// Method m1 with 0 parameter.
public void m1()
[Link]("No parameter method");
// Method m1 with 1 integer parameter.
public void m1(int i)
[Link]("Int Parameter");
// Method m1 with 1 string parameter.
public void m1(String s)
[Link]("String Parameter");
// Main Class
public class Main
public static void main(String[] args)
Dept. of Computer Science and Design 11
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
// Creating object for GFG class.
// g is object of GFG class.
GFG g = new GFG();
// Here, m1 called with string parameter.
// m1(String s) method will be called.
g.m1("A");
Output :
String Parameter
Dept. of Computer Science and Design 12
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
5.a. Write a Java program to illustrate Dynamic Method Dispatch using hierarchical
inheritance
class A
void m1( )
[Link]("Inside A's m1 method");
class B extends A
// overriding m1( )
void m1( )
[Link]("Inside B's m1 method");
class C extends A
// overriding m1( )
void m1( )
[Link]("Inside C's m1 method");
Dept. of Computer Science and Design 13
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
// Driver class
class Dispatch
public static void main(String args[])
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
// obtain a reference of type A
A ref;
// ref refers to an A object
ref = a;
// calling A's version of m1()
ref.m1();
// now ref refers to a B object
Dept. of Computer Science and Design 14
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
ref =8 b;
// calling B's version of m1()
ref.m1();
// now ref refers to a C object
ref = c;
// calling C's version of m1()
ref.m1();
Dept. of Computer Science and Design 15
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
[Link] a Java program for abstract class to find areas of different shapes.
import [Link].*;
abstract class shape
int a,b;
abstract public void printarea( );
class rectangle extends shape
public int area_rect;
public void printarea( )
Scanner s=new Scanner([Link]);
[Link]("enter the length and breadth of rectangle");
a=[Link]( );
b=[Link]( );
area_rect=a*b;
[Link]("Length of rectangle "+a +"breadth of rectangle "+b);
[Link]("The area ofrectangle is:"+area_rect);
Dept. of Computer Science and Design 16
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
class triangle extends shape
double area_tri;
public void printarea( )
Scanner s=new Scanner([Link]);
[Link]("enter the base and height of triangle");
a=[Link]( );
b=[Link]( );
[Link]("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);
[Link]("The area of triangle is:"+area_tri);
class circle extends shape
double area_circle;
public void printarea( )
Scanner s=new Scanner([Link]);
[Link]("enter the radius of circle");
Dept. of Computer Science and Design 17
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
a=[Link]( );
area_circle=(3.14*a*a);
[Link]("Radius of circle"+a);
[Link]("The area of circle is:"+area_circle);
public class shapeclass
public static void main(String[] args)
rectangle r=new rectangle( );
[Link]();
triangle t=new triangle( );
[Link]();
circle r1=new circle( );
[Link]( );
Dept. of Computer Science and Design 18
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
6. Write a Java program that implements interface using extends keyword
interface A
void funcA( );
interface B extends A
void funcB( );
class C implements B
public void funcA( )
[Link]("This is funcA");
public void funcB( )
[Link]("This is funcB");
public class Demo
public static void main(String args[ ])
C obj = new C();
[Link]();
Dept. of Computer Science and Design 19
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
[Link]();
Output
This is funcA
This is funcB
Dept. of Computer Science and Design 20
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
7.a. write a java program that illustrate exception handling mechanism .
[Link].*;
class Error2
{
public static void main(String args[ ])
{
int a=10;
int b=5;
int c=5;
intx,y;
try
{
x=a/(b-c);
}
catch (ArithmeticException e)
{
[Link]("division by zero");
}
y=a/(b+c);
[Link]("y=" +y);
}
}
Output:
C:\javaprg>javac [Link]
[Link][Link]x is already defined in main<[Link] [ ] >
Intx = a / < b - c >;
[Link][Link]x is already defined in main<[Link] [ ] >
Int y = a / < b + c >;
2 errors
C:\javaprg>javac [Link]
C:\javaprg>java Error2
Division by zero
y=1
Dept. of Computer Science and Design 21
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
7.b. write a java program to implement break and continue statements.
class BreakandContinue
public static void main(String args[ ])
//Illustrating break statement (execution stops when value of i becomes to 4.)
[Link]("Break Statement\n....................");
for(inti=1;i<=5;i++)
if(i==4) break;
[Link](i);
// Illustrating continue statement (execution skipped when value of i becomes to 1.)
[Link]("Continue Statement\n....................");
for(inti=1;i<=5;i++)
if(i==1) continue;
[Link](i);
Output:
Break statement
…………………………………
Dept. of Computer Science and Design 22
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
Continue statement
……………………….
Process finished
Dept. of Computer Science and Design 23
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
8. Write a Java program to illustrate the working of String methods.
public class Stringoperation1
{
public static void main(String ar[])
{
String s="Sachin";
[Link]([Link]());//SACHIN
[Link]([Link]());//sachin
[Link](s);//Sachin(no change in original)
}
}
Output:
SACHIN
sachin
Sachin
Dept. of Computer Science and Design 24
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
9. Write a java program that creates threads by extending thread class .
a. First thread display “Good morning “every 1 sec,
b. Second thread displays “Hello “every 2 seconds
c. Third display “Welcome “every 3 seconds
class Frst implements Runnable
Thread t;
Frst()
t=new Thread(this);
[Link]("Good Morning");
[Link]();
public void run()
for(inti=0;i<10;i++)
[Link]("Good Morning"+i);
try
[Link](1000);
catch(Exception e)
[Link](e);
Dept. of Computer Science and Design 25
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
class sec implements Runnable
Thread t;
sec( )
t=new Thread(this);
[Link]("hello");
[Link]();
public void run( )
for(inti=0;i<10;i++)
[Link]("hello"+i);
try
[Link](2000);
catch(Exception e)
[Link](e);
Dept. of Computer Science and Design 26
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
class third implements Runnable
Thread t;
third( )
t=new Thread(this);
[Link]("welcome");
[Link]( );
public void run()
for(int i=0;i<10;i++)
[Link]("welcome"+i);
try
[Link](3000);
catch(Exception e)
[Link](e);
Dept. of Computer Science and Design 27
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
public class Multithread
public static void main(String arg[])
newFrst( );
new sec( );
new third( );
Output:
Good morning
Hello
Welcome
Hello 0
Welcome 0
Good morning 0
Good morning 1
Hello 1
Good morning 2
Welcome 1
Good morning 3
Hello 2
Dept. of Computer Science and Design 28
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
10. Write a java program producer consumer problem using Thread.
import [Link];
public class Threadexample
{
public static void main(String[ ] args)
throwsInterruptedException
{
// Object of a class that has both produce( )
// and consume( ) methods
final PC pc = new PC( );
// Create producer thread
Thread t1 = new Thread(new Runnable( )
{
@Override
public void run()
{
try
{
[Link]( );
}
catch (InterruptedException e)
{
[Link]();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable( )
{
@Override
public void run( )
{
try
{
[Link]();
}
catch (InterruptedException e)
Dept. of Computer Science and Design 29
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
{
[Link]();
}
}
});
// Start both threads
[Link]( );
[Link]( );
// t1 finishes before t2
[Link]( );
[Link]( );
}
// This class has a list, producer (adds items to list
// and consumber (removes items).
public static class PC
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>( );
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list
// is full
while ([Link]() == capacity)
wait();
[Link]("Producer produced-"+ value);
Dept. of Computer Science and Design 30
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
// to insert the jobs in the list
[Link](value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
[Link](1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while ([Link]() == 0)
wait();
// to retrive the ifrst job in the list
intval = [Link]();
[Link]("Consumer consumed-" + val);
// Wake up producer thread
notify();
// and sleep
[Link](1000);
}
}
}
}
}
Output:
Dept. of Computer Science and Design 31
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
Break statement
……………………….
Continue statement
…………………………
5 Process finished
Dept. of Computer Science and Design 32
Faculty of Engineering & Technology, SUK