Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 54

FACULTY OF ENGINEERING AND TECHNOLOGY

BACHELOR OF TECHNOLOGY

COMPUTER SCIENCE AND ENGINEERING

OBJECT ORIENTED PROGRAMMING WITH


JAVA LABORATORY
203105334

5TH SEMESTER

Laboratory Manual

190303105391 1|Page
CERTIFICATE

This is to certify that

Mr./Ms Sai Harsha Vardhan Ghantasala

with enrolment no 190303105391 has

successfully completed his/her laboratory experiments in the

OBJECT ORIENTED PROGRAMMINF WITH JAVA


LABORATORY

(203105334) from the department

Of Computer science & Engineering


during the academic year 2021-2022

Date of Submission: ......................... Staff In charge: ...........................

Head of Department: ...........................................

190303105391 2|Page
Sr. Experiment Title Page Date of Marks
No No Performance
(out of 10)

1. WRITE A PROGRAM TO
COUNT THE NUMBER OF 7
WORDS THAT START WITH A
1 CAPITAL LETTER

1.1WRITE A JAVA PROGRAM TO


TAKE AN ARRAY OF STRINGS
9
AS AN INPUT, AND ARRANGE
STRINGS IN ASCENDING
ORDER.

2. WRITE A PROGRAM TO FIND


THE LARGEST NUMBER IN AN 11
ARRAY OF NUMBERS USING
2 COMMAND LINE ARGUMENTS

2.1WRITE A PROGRAM TO FIND


FACTORIAL OF NUMBER.
13
HERE, TAKE NUMBER AS
COMMAND LINE ARGUMENT.

3. WRITE A PROGRAM TO
DEMONSTRATE CLASS AND 14
OBJECTS USING THE CONCEPT
OF AN ARRAY OBJECT
3
3.1DECLARE A CLASS BOX.
OVERLOAD BOX
CONSTRUCTORS WITH ZERO
ARGUMENT, ONE ARGUMENT 16
AND THREE ARGUMENTS TO
INITIALIZE THE MEMBERS OF
THE CLASS. DECLARE A
METHOD TO FIND VOLUME
OF THE BOX.

4. WRITE A PROGRAM TO

190303105391 3|Page
DEMONSTRATE GARBAGE 18
COLLECTION USING
SYSTEM.GC() OR
4 RUNTIME.GC()

4.1WRITE A PROGRAM TO SHOW 19


THE USE OF FINALIZE
METHOD FOR GARBAGE
COLLECTION.

5. WRITE A PROGRAM TO
DEMONSTRATE STATIC
20
CONSTANTS AND FINAL
CONSTANTS.
5
5.1WRITE A PROGRAM TO
CREATE A CLASS NAMED AS
BIKE WHICH CONSIST ONE
22
FINAL METHOD CALLED AS
RUN(),DECLARE A SUBCLASS
BIKE & DEMONSTRATE THE
USE OF FINAL METHOD.

6. WRITE A PROGRAM TO
EXPLAIN STATIC 23
POLYMORPHISM IN JAVA.
6
6.1WRITE A PROGRAM TO FIND
VOLUME OF BOX USING
24
CONCEPT OF METHOD
OVERLOADING.

7. WRITE A PROGRAM TO FIND


THE FACTORIAL OF A NUMBER 26
USING INTERFACE.
7
7.1WRITE A PROGRAM TO
28
IMPLEMENT MULTIPLE
INHERITANCE IN JAVA USING
INTERFACE.

190303105391 4|Page
7.2 CREATE A PACKAGE CALLED
MATHSOPERATION1, WHICH
MUST CONTAIN CLASSES TO
PERFORM ADDITION,
SUBTRACTION, CREATE
ANOTHER PACKAGE CALLED
MATHSOPERATION2, WHICH
MUST CONTAIN CLASSES TO
PERFORM MULTIPLICATION
AND DIVISION OPERATION.
30
CREATE A MAIN CLASS AND
IMPORT THE
MATHSOPERATION1,
MATHSOPERATION1 PACKAGE
IN IT TO PERFORM ALL THE
OPERATIONS ON THE INPUT
NUMBERS PROVIDED BY THE
USER. FINALLY, DISPLAY THE
RESULT OF EACH OPERATION
ON THE CONSOLE.

8. WRITE A PROGRAM TO
DESIGN STUDENT
33
REGISTRATION FORM USING T
COMPONENTS.
8
8.1 WRITE A JAVA PROGRAM FOR
CALCULATOR OPERATIONS
USING AWT CONTROLS & 38
APPROPRIATE LAYOUT
MANAGER.

9. WRITE A PROGRAM TO
DEMONSTRATE ARRAY INDEX
44
OUT OF BOUNDS EXCEPTION.
9
9.1 CREATE AN INTERFACE
ACCOUNT WITH TWO
METHODS DEPOSIT AND

190303105391 5|Page
WITHDRAW. CREATE CLASS
SAVINGSACCOUNT WHICH
IMPLEMENTS THE INTERFACE.
WRITE A CUSTOM EXCEPTION 45
HANDLER FOR SAVINGS
ACCOUNT TO HANDLE THE
SCENARIOS WHEN
WITHDRAWN AMOUNT IS
LARGER THAN THE BALANCE
IN THE ACCOUNT.

10.WRITE A PROGRAM TO
DEMONSTRATE CLASS OBJECT
47
LOCKING USING METHOD
LEVEL SYNCHRONIZATION.
10
10.1 WRITE A PROGRAM
THAT EXECUTES TWO
THREADS. ONE THREAD WILL 50
PRINT THE EVEN NUMBERS
AND ANOTHER THREAD WILL
PRINT ODD NUMBERS FROM 1
TO 50.

190303105391 6|Page
Pratical-1
Write a program to count the number of words that start
with a capital letter.

 CODE:
import java.util.*;
public class CountCapitalLetter{
public static void main(String args[]){
String s1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
s1=sc.nextLine();
int count=0,i=0,n;
n=s1.length();
System.out.println("Size of the String is :"+n);
if(null==s1 || s1.isEmpty()){
System.out.println("Text Empty");
}
else{
if(Character.isUpperCase(s1.charAt(0)))
{
count++;
}
for(i=1;i<n;i++)
{
if(Character.isWhitespace(s1.charAt(i-1)) && Character.isUpperCase(s1.charAt(i)))

190303105391 7|Page
{
count++;
}
}
}
System.out.println("Number of Word which Start With Capital Letter :"+count);
}
}

 OUTPUT:

190303105391 8|Page
Pratical-1.1
Write a java program to take an array of strings as an input,
and arrange strings in ascending order.

 CODE:
import java.util.Scanner; public class Ascending
{
public static void main(String args[])
{
int n;
String temp;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Number of String you would like to Enter :");
n=sc.nextInt();
String str[]=new String[n];
Scanner sc1=new Scanner(System.in); System.out.println("Enter the String one by
one :"); for(int i=0;i<n;i++)
{
str[i]=sc1.nextLine();
}
sc.close();
sc1.close(); for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{

if(str[i].compareTo(str[j])>0)

190303105391 9|Page
{
temp=str[i]; str[i]=str[j]; str[j]=temp;
}
}
}
System.out.println("Strings in Sorted Order :"); for(int i=0;i<n;i++)
{
System.out.print(str[i]+" ");
}
}
}

 OUTPUT

190303105391 10 | P a g e
Pratical-2
Write a program to find the largest number in an array of
numbers using command line arguments.

 CODE:
class Array3

{
public static void main(String args[])
{
int array[]=new int[20]; for(int i=0;i<args.length;i++)
{
array[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
for(int j=0;j<args.length;j++)
{
if(array[i]>array[i+1])
{
int temp=array[i]; array[i]=array[i+1]; array[i+1]=temp;
}
}
}

System.out.println("Large Number is :"+array[args.length]);

190303105391 11 | P a g e
}
}

 OUTPUT:

190303105391 12 | P a g e
Pratical-2.1
Write a program to find factorial of number. Here,
take number as command line argument.

 CODE:
class Factorial
{
public static void main(String args[])
{
int fact=1;
int n=Integer.parseInt(args[0]); for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial of this Number is:"+fact);
}
}

 OUTPUT:

190303105391 13 | P a g e
Pratical-3
Write a program to demonstrate class and objects using the
concept of an array object.

 CODE:
import java.util.Scanner;

class Person
{
String name;
int sal,da,hra,eno; void getData()
{
Scanner ob=new Scanner(System.in); System.out.println("Enter Name :");
name=ob.nextLine(); System.out.println("Enter Employee no :"); eno=ob.nextInt();
System.out.println("Enter Basic Salary :"); sal=ob.nextInt();
System.out.println("Enter Da :"); da=ob.nextInt(); System.out.println("Enter HRA :");
hra=ob.nextInt();
}

void display()
{
int tot; tot=sal+da+hra;
System.out.println("Total salary is :"+tot);
}
}

190303105391 14 | P a g e
public class Employee {
public static void main(String[] args) {

System.out.println("How Many Employees :"); Scanner sc=new Scanner(System.in);


int n=sc.nextInt();
Person[] abc=new Person[n]; for(int i=0;i<n;i++)
{
abc[i]=new Person();
abc[i].getData();
abc[i].display();
}
}
}

 OUTPUT:

190303105391 15 | P a g e
Pratical-3.1
Declare a class box. Overload Box constructors with zero
argument, one argument and three Argument to
initialize the members of the class. Declare a method to
find volume of the box.

 CODE:
class Box

double width,height,depth;

Box()

width=height=depth=0;

Box(double a){ width=height=depth=a;

Box(double w,double h,double d){ width=w;

height=h;

depth=d;

double volume(){

return width*height*depth;

public static void main(String args[])

190303105391 16 | P a g e
Box box1=new Box();

Box cube=new Box(4);

Box box=new Box(11,14,19);

double vol;

vol=box.volume();

System.out.println("Volume of Box is :"+vol);

vol=box1.volume();

System.out.println("Volume of Box1 is :"+vol);

vol=cube.volume();

System.out.println("Volume of cube is :"+vol);

 OUTPUT:

190303105391 17 | P a g e
Pratical-4
Write a program to demonstrate garbage collection using
System.gc() or Runtime.gc().

 CODE:
class Gc

{
public static void main(String args[])throws InterruptedException
{
Gc ob1=new Gc(); Gc ob2=new Gc(); ob1=null; System.gc(); ob2=null;
Runtime.getRuntime().gc();
}
protected void finalize()throws Throwable
{
System.out.println("Garbage collector called"); System.out.println("Object garbage
collected : " + this);
}
}

 OUTPUT:

190303105391 18 | P a g e
Pratical-4.1
Write a program to show the use of finalize method for
garbage collection.

 CODE:
public class Example1 {
public static void main(String[] args)
{
Example1 obj = new Example1();
System.out.println(obj.hashCode());
obj = null;
System.gc();
System.out.println("end of garbage collection");
}
protected void finalize()
{
System.out.println("finalize method called");
}
}

 OUTPUT:

190303105391 19 | P a g e
Pratical-5
Write a program to demonstrate static constants and final
constants.

 CODE:
class sconst

{
final float pi=3.14f;
static int r=5;
static int b=2;
static int c;
static void display()

{
System.out.println("Value Of r is:" );
}
static void add()

{
c=r+b;
System.out.println(c);
}
public static void main(String args[])

190303105391 20 | P a g e
sconst obj=new sconst(); obj.display();
add(); System.out.println(sconst.r);
}
}

 OUTPUT:

190303105391 21 | P a g e
Pratical-5.1
Write a program to create a class named as Bike which
consist one final method called as run(), Declare a subclass
Bike & demonstrate the use of final method.

 CODE:
class Bike
{
final void run(){
System.out.println("Bike");
}
}
class Bike1 extends Bike
{
public static void main(String args[])
{
Bike1 ob=new Bike1(); ob.run();
}
}

 OUTPUT:

190303105391 22 | P a g e
190303105391 23 | P a g e
Pratical-6
Write a program to explain static polymorphism in java.

 CODE:
class Polymorphism { int sum(int x,int y)

{
return x+y;
}
int sum(int x,int y,int z)
{
return x+y+z;
}
public static void main(String[] args) {
Polymorphism ob=new Polymorphism();
System.out.println("Sum of two Number is :"+ob.sum(18,29));
System.out.println("Sum of Three Number is :"+ob.sum(18, 29, 40));
}
}

 OUTPUT:

190303105391 24 | P a g e
Pratical-6.1
Write a program to find volume of Box using concept of
method overloading.

class xyz
{
int a,b,c,vol; void vol(){
System.out.println("Cannot find the Volume");
}
void vol(int a){
System.out.println("volume is :"+(a*a*a));
}
void vol(int a,int b,int c){
System.out.println("volume is :"+(a*b*c));
}
void vol(float a,float b,float c){
System.out.println("volume is :"+(a*b*c));
}
void vol(int a,float b,float c){
System.out.println("volume is :"+(a*b*c));
}
void vol(float a,int b,float c){
System.out.println("volume is :"+(a*b*c));
}
void vol(float a,float b,int c){
System.out.println("volume is :"+(a*b*c));

190303105391 25 | P a g e
}
}
class overload
{
public static void main(String args[])
{
xyz ob=new xyz(); ob.vol();
ob.vol(4); ob.vol(3,4,5);
ob.vol(3.1f,4.2f,5.7f);
ob.vol(9,3.3f,7.4f);
ob.vol(6.6f,7,1.2f);
ob.vol(2.3f,4.5f,9);
}
}

 OUTPUT:

190303105391 26 | P a g e
Pratical-7
Write a program to find the factorial of a number using
interface.

 CODE:
interface fact
{
void show_fact(int n);
}
class p2 implements fact
{
public void show_fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial of number is :"+fact);
}
public static void main(String args[])
{
p2 ob=new p2(); ob.show_fact(7);
}
}

190303105391 27 | P a g e
 OUTPUT:

190303105391 28 | P a g e
Pratical-7.1
Write a program to implement multiple inheritance in java
using interface.

 CODE:
interface add
{
void sum(int a,int b);
}
interface subtract
{
void sub(int a,int b);
}
interface multiply
{
void mul(int a,int b);
}
interface division
{
void div(int a,int b);
}
class p3 implements add,subtract,multiply,division
{
public void sum(int a,int b)
{
System.out.println("Sum is :"+(a+b));

190303105391 29 | P a g e
}
public void sub(int a,int b)
{
System.out.println("Subtraction is :"+(a-b));
}
public void mul(int a,int b)
{
System.out.println("multiply is :"+(a*b));
}
public void div(int a,int b)
{
System.out.println("division is :"+(a/b));
}
public static void main(String args[])
{
p3 ob=new p3(); ob.sum(2,9);
ob.sub(9,5);
ob.mul(3,9);
ob.div(16,4);
}
}

 OUTPUT:

190303105391 30 | P a g e
Pratical-7.2
Create a package called Mathsoperation1, which must
contain classes to perform addition, subtraction, Create
another package called Mathsoperation2, which must
contain classes to perform multiplication and division
operation. Create a main class and import the
Mathsoperation1, Mathsoperation1 package in it to
perform all the operations on the input numbers provided
by the user. Finally, display the result of each operation on
the console.

 CODE:
Code for Mathsoperation1 package:

package
Mathsoper
ation1;
public class
Add

public vocid sum(int a,int b)

System.out.println("Sum of two number is :"+(a+b));

public void subtract(int a,int b)

System.out.println("Subtract of two number is :"+(a-b));

190303105391 31 | P a g e
}

Code for Mathsoperation2 package:

package
Mathsoper
ation2;
public
class

public void mul(int a,int b)

System.out.println("Multiplication of two number is :"+(a*b));

public void div(int a,int b)

System.out.println("Division of two number is :"+(a/b));

Code for Main class:

package Mathsoperation1;

import Mathsoperation2.*;

public class muldiv

public static void main(String args[])

190303105391 32 | P a g e
{

Add ob1=new Add();

ob1.sum(6,3); ob1.subtract(10,8);

abc ob2=new abc(); ob2.mul(2,5);

ob2.div(10,5);

 OUTPUT:

190303105391 33 | P a g e
Pratical-8
Write a program to design student registration form using
AWT components.

 CODE:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class student extends Frame implements ActionListener
{
String msg;
Button b1=new Button("save");
Label l11=new Label("Student details",Label.CENTER);
Label l1=new Label("Name:",Label.LEFT);
Label l2=new Label("age:",Label.LEFT);
Label l3=new Label("Sex(M/F):",Label.LEFT);
Label l4=new Label("Address:",Label.LEFT);
Label l5=new Label("Course:",Label.LEFT);
Label l6=new Label("Semester:",Label.LEFT);
Label l7=new Label("",Label.RIGHT);
TextField t1=new TextField();
Choice c1=new Choice();

CheckboxGroup cbg=new CheckboxGroup(); Checkbox ck1=new


Checkbox("Male",false,cbg); Checkbox ck2=new Checkbox("Female",false,cbg);
TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
Choice course=new Choice();
190303105391 34 | P a g e
Choice sem=new Choice();

Choice age=new Choice(); public student()


{addWindowListener(new myWindowAdapter()); setLayout(null);
add(l11);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(t1);
add(t2);
add(ck1);
add(ck2);

add(course);
add(sem);
add(age);

add(b1);
b1.addActionListener(this);
add(b1);
course.add(" c.s");
course.add(" mechanical");
course.add("civil");
course.add("it");

190303105391 35 | P a g e
course.add("electrical");

sem.add("1");
sem.add("2");
sem.add("3");
sem.add("4");
sem.add("5");
sem.add("6");
age.add("17");
age.add("18");
age.add("19");
age.add("20");
age.add("21");

l1.setBounds(25,65,90,20);
l2.setBounds(25,90,90,20);
l3.setBounds(25,120,90,20);
l4.setBounds(25,185,90,20);
l5.setBounds(25,260,90,20);
l6.setBounds(25,290,90,20);
l7.setBounds(25,260,90,20);
l11.setBounds(10,40,280,20);
t1.setBounds(120,65,170,20);
t2.setBounds(120,185,170,60);
ck1.setBounds(120,120,50,20);
ck2.setBounds(170,120,60,20);
course.setBounds(120,260,100,20);
sem.setBounds(120,290,50,20);

190303105391 36 | P a g e
age.setBounds(120,90,50,20);
b1.setBounds(120,350,50,30);
}

public void paint(Graphics g)


{g.drawString(msg,200,450);}
public void actionPerformed(ActionEvent ae)
{if(ae.getActionCommand().equals("save"))
{msg="Student details saved!";
}
}

public static void main(String g[])


{student stu=new student();
stu.setSize(new Dimension(500,500));
stu.setTitle("student registration");
stu.setVisible(true);
}
}

class myWindowAdapter extends WindowAdapter


{public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

190303105391 37 | P a g e
 OUTPUT:

190303105391 38 | P a g e
Pratical-8.1
Write a Java Program for Calculator Operations Using AWT
Controls & appropriate layout manager.

 CODE:
import javax.swing.*; import java.awt.event.*;
class Calculator1 extends JFrame implements ActionListener
{
double a,b,result; int operator; JTextField t1;
JButton b0 , b1 , b2 , b3 , b4, b5 , b6, b7 , b8 , b9 , bsum , bsub, bmul , bdiv , bequal ,
reset;
Calculator1()
{
t1 = new JTextField("");
t1.setBounds(50 , 50 ,250 , 30);
b0 = new JButton("0");
b0.setBounds(50 , 100 , 50 , 50);
b1 = new JButton("1");
b1.setBounds(100, 100 , 50 ,50);
b2 = new JButton("2");
b2.setBounds(150, 100 , 50 ,50);
b3 = new JButton("3");
b3.setBounds(200, 100 , 50 ,50);
b4 = new JButton("4");
b4.setBounds(250, 100 , 50 ,50);
b5 = new JButton("5");

190303105391 39 | P a g e
b5.setBounds(50 , 160 , 50 , 50);
b6 = new JButton("6");
b6.setBounds(100, 160 , 50 ,50);
b7 = new JButton("7");
b7.setBounds(150, 160 , 50 ,50);
b8 = new JButton("8");
b8.setBounds(200, 160 , 50 ,50);
b9 = new JButton("9");
b9.setBounds(250, 160 , 50 ,50);
reset = new JButton("reset");
reset.setBounds(175, 300 , 100 ,50);
bsum = new JButton("+");
bsum.setBounds(50 , 220 , 50 , 50);
bsub = new JButton("-");
bsub.setBounds(100, 220 , 50 ,50);
bmul = new JButton("*");
bmul.setBounds(150, 220 , 50 ,50);
bdiv = new JButton("/");
bdiv.setBounds(200, 220 , 50 ,50);
bequal = new JButton("=");
bequal.setBounds(250, 220 , 50 ,50);
setLayout(null);
t1.setBounds(50 , 50 ,250 , 30);
add(t1);
add(b0);
add(b1);
add(b2);
add(b3);

190303105391 40 | P a g e
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(bsum);
add(bsub);
add(bmul);
add(bdiv);
add(bequal);
add(reset);
setSize(500,500);
setLayout(null);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bsum.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);

190303105391 41 | P a g e
bequal.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0) t1.setText(t1.getText().concat("0"));
if(e.getSource()==b1) t1.setText(t1.getText().concat("1"));
if(e.getSource()==b2) t1.setText(t1.getText().concat("2"));
if(e.getSource()==b3) t1.setText(t1.getText().concat("3"));
if(e.getSource()==b4) t1.setText(t1.getText().concat("4"));
if(e.getSource()==b5) t1.setText(t1.getText().concat("5"));
if(e.getSource()==b6) t1.setText(t1.getText().concat("6"));
if(e.getSource()==b7) t1.setText(t1.getText().concat("7"));
if(e.getSource()==b8) t1.setText(t1.getText().concat("8"));
if(e.getSource()==b9) t1.setText(t1.getText().concat("9"));
if(e.getSource()==reset) t1.setText("");
if(e.getSource()==bsum)
{
a=Double.parseDouble(t1.getText());
operator=1;
t1.setText("");
}
if(e.getSource()==bsub)
{
a=Double.parseDouble(t1.getText());
operator=2;
t1.setText("");
}

190303105391 42 | P a g e
if(e.getSource()==bmul)
{
a=Double.parseDouble(t1.getText());
operator=3;
t1.setText("");
}
if(e.getSource()==bdiv)
{
a=Double.parseDouble(t1.getText());
operator=4;
t1.setText("");
}
if(e.getSource()==bequal)
{
b=Double.parseDouble(t1.getText());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
t1.setText(""+result);

190303105391 43 | P a g e
}
}
public static void main(String args[])
{
Calculator1 jf = new Calculator1(); jf.setVisible(true);
}
}

 OUTPUT:

190303105391 44 | P a g e
Pratical-9
Write a program to demonstrate array index out of bounds
exception.

 CODE:
public class arrayindexoutofbounds {

public static void main(String[] args) { try


{
int ar[]=new int[5]; ar[6]=5;
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Exception thrown :"+e); System.out.println("system-error-
msg"+e.getMessage());
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException"); System.out.println("system-error-
msg"+e.getMessage()); System.out.println("user message : divided by Zero");
}
}
}

190303105391 45 | P a g e
 OUTPUT:

Pratical-9.1
Create an interface Account with two methods
deposit and withdraw. Create class Savings Account
which implements the interface. Write a custom
Exception handler for Savings Account to handle the
scenarios when withdrawn amount is larger than
the balance in the account.

 CODE:

interface Account
{
double getBalance();
void deposit(double amount);
void withdraw(double amount);
}
public class SavingAccount implements Account{

190303105391 46 | P a g e
double deposits; double withdrawals;
public double getBalance()
{
return deposits - withdrawals;
}
public void deposit(double amount)
{
deposits += amount;
System.out.println("Total Balance after depositing:"+deposits);
}
public void withdraw(double amount)
{
if(deposits<amount)
{
System.out.println("Enter Valide Amount");
}
else {
withdrawals += amount;
System.out.println("Total Balance after Withdrowing :"+(deposits-
withdrawals));
}
}

190303105391 47 | P a g e
 OUTPUT:

190303105391 48 | P a g e
Pratical-10
Write a program to demonstrate class object locking using
method level synchronization.

 CODE:
class Table {
synchronized void printTable(int n)
{
for(int i=1;i<=10;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}

}
}

class MyThread1 extends Thread


{

190303105391 49 | P a g e
Table t;
MyThread1(Table t)
{

this.t=t;
}
public void run()
{
t.printTable(5);
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String[] args) {
Table t1=new Table();
MyThread1 m1=new MyThread1(t1);
MyThread2 m2=new MyThread2(t1);

190303105391 50 | P a g e
m1.start();
m2.start();
}
}

 OUTPUT:

190303105391 51 | P a g e
Pratical-10.1
Write a program that executes two threads. One thread will
print the even numbers and another thread will print odd
numbers from 1 to 50.

class Number
{
synchronized void printEvenNumber(int n)
{
for(int i=1;i<=n;i++)
{ if(i%2==0)
System.out.println(i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
synchronized void printOddNumber(int n)
{
for(int i=1;i<=n;i++)
{ if(i%2!=0)

190303105391 52 | P a g e
System.out.println(i);
try
{
}
Thread.sleep(400);
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Even extends Thread
{
Number t;
Even(Number t)
{
this.t=t;
}
public void run()
{
t.printEvenNumber(10);
}
}
class Odd extends Thread
{
Number t;
Odd(Number t)

190303105391 53 | P a g e
{
this.t=t;
}
public void run()
{
t.printOddNumber(10);
}
}
public class p10 {
public static void main(String[] args) {
Number t1=new Number();
Even m1=new Even(t1);
Odd m2=new Odd(t1);
m1.start();
m2.start();
}
}

 OUTPUT:

190303105391 54 | P a g e

You might also like