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 java.io.*;
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 = add.addTwoInt(1,2);
System.out.println("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;
System.out.println("a<<2 = "+ (a << 2));
System.out.println("b>>2 = "+ (b >> 2));
System.out.println("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;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\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 )
System.out.println(“value is : “+ x);
Sum += x;
System.out.println(“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)
3.a.Write 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);
System.out.println("addition of integers: " + a);
System.out.println("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)
this.rollNum = 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);
System.out.println(obj.getRollNum());
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()
System.out.println(“\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();
Ob.calc();
Ob.show( );
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()
System.out.println("No parameter method");
// Method m1 with 1 integer parameter.
public void m1(int i)
System.out.println("Int Parameter");
// Method m1 with 1 string parameter.
public void m1(String s)
System.out.println("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( )
System.out.println("Inside A's m1 method");
class B extends A
// overriding m1( )
void m1( )
System.out.println("Inside B's m1 method");
class C extends A
// overriding m1( )
void m1( )
System.out.println("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)
5.b.Write a Java program for abstract class to find areas of different shapes.
import java.util.*;
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(System.in);
System.out.println("enter the length and breadth of rectangle");
a=s.nextInt( );
b=s.nextInt( );
area_rect=a*b;
System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);
System.out.println("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(System.in);
System.out.println("enter the base and height of triangle");
a=s.nextInt( );
b=s.nextInt( );
System.out.println("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);
System.out.println("The area of triangle is:"+area_tri);
class circle extends shape
double area_circle;
public void printarea( )
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of circle");
Dept. of Computer Science and Design 17
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
a=s.nextInt( );
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
System.out.println("The area of circle is:"+area_circle);
public class shapeclass
public static void main(String[] args)
rectangle r=new rectangle( );
r.printarea();
triangle t=new triangle( );
t.printarea();
circle r1=new circle( );
r1.printarea( );
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( )
System.out.println("This is funcA");
public void funcB( )
System.out.println("This is funcB");
public class Demo
public static void main(String args[ ])
C obj = new C();
obj.funcA();
Dept. of Computer Science and Design 19
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
obj.funcB();
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 .
importjava.lang.*;
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)
{
System.out.println("division by zero");
}
y=a/(b+c);
System.out.println("y=" +y);
}
}
Output:
C:\javaprg>javac Error2.java
Error2.java:12:x is already defined in main<java.lang.string [ ] >
Intx = a / < b - c >;
Error2.java:12:x is already defined in main<java.lang.string [ ] >
Int y = a / < b + c >;
2 errors
C:\javaprg>javac Error2.java
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.)
System.out.println("Break Statement\n....................");
for(inti=1;i<=5;i++)
if(i==4) break;
System.out.println(i);
// Illustrating continue statement (execution skipped when value of i becomes to 1.)
System.out.println("Continue Statement\n....................");
for(inti=1;i<=5;i++)
if(i==1) continue;
System.out.println(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";
System.out.println(s.toUpperCase());//SACHIN
System.out.println(s.toLowerCase());//sachin
System.out.println(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);
System.out.println("Good Morning");
t.start();
public void run()
for(inti=0;i<10;i++)
System.out.println("Good Morning"+i);
try
t.sleep(1000);
catch(Exception e)
System.out.println(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);
System.out.println("hello");
t.start();
public void run( )
for(inti=0;i<10;i++)
System.out.println("hello"+i);
try
t.sleep(2000);
catch(Exception e)
System.out.println(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);
System.out.println("welcome");
t.start( );
public void run()
for(int i=0;i<10;i++)
System.out.println("welcome"+i);
try
t.sleep(3000);
catch(Exception e)
System.out.println(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 java.util.LinkedList;
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
{
pc.produce( );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable( )
{
@Override
public void run( )
{
try
{
pc.consume();
}
catch (InterruptedException e)
Dept. of Computer Science and Design 29
Faculty of Engineering & Technology, SUK
OOPS With JAVA Laboratory (22CSL38)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start( );
t2.start( );
// t1 finishes before t2
t1.join( );
t2.join( );
}
// 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 (list.size() == capacity)
wait();
System.out.println("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
list.add(value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
Thread.sleep(1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while (list.size() == 0)
wait();
// to retrive the ifrst job in the list
intval = list.removeFirst();
System.out.println("Consumer consumed-" + val);
// Wake up producer thread
notify();
// and sleep
Thread.sleep(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