You are on page 1of 68

DEPARTMENT OF COMPUTER APPLICATIONS

(SELF FINANCED SECTION)


WOMEN’S CHRISTIAN COLLEGE
CHENNAI – 600 006.

(An Autonomous Institution-Affiliated to the University of Madras)

JAVA PRACTICAL

Name : I.KEZIA MERCY

Reg. No. : 20BCA52

1
Women’s Christian College, Chennai-6.
An Autonomous Institution-Affiliated to the University of Madras

This is to certify that this is the record work done by


I.KEZIA MERCY(20BCA52) of II BCA during
JUNE 2021-NOV 2021.

Internal Examiner External Examiner

2
INDEX
PAGE
Sno DATE PROGRAM SIGN
NO
CLASSES AND OBJECTS
GENERATING STUDENTS MARK
1 3.08.21 DETAILS

2 16.08.21 FACTORIAL OF A GIVEN NUMBER

INHERITANCE
BANK TRANSACTION USING
3. 23.08.21
INHERITANCE
EVENT HANDLING

4. 25.08.21 KEY EVENTS

AWT CONTROLS

5. 02.09.21 IMPLEMENTING BUTTON CONTROL

IMPLEMENTING CHECKBOX
6. 07.09.21
CONTROL

7. 09.09.21 IMPLEMENTING LIST CONTROL

APPLET CLASS
DISPLAYING VARIOUS SHAPES USING
8. 01.10.21
APPLET CLASS
PACKAGES
GENERATING EMPLOYEE SALARY
9. 07.10.21
DETAILS USING PACKAGES
INTERFACE
GENERATING ELECTRICITY BILL
10 09.10.21
USING JNTERFACE
EXCEPTION HANDLING
IMPLEMENTING BUILT-IN
11 16.10.21
EXCEPTIONS.
SJMPLE THREADS
IMPLEMENTING THREADS USING
12 20.10.21
THREAD CLASS

COMPARING TWO TEXT FILES IN


13 25.10.21
JAVA

INSERTING AND DELETING AN


14 15.11.21
ELEMENT IN BINARY SEARCH TREE

3
EX.NO:01
DATE: 25.08.21

GENERATING STUDENTS MARK DETAILS


USING CLASSES AND OBJECTS
AIM AND PROCEDURE:

4
5
PROGRAM CODE:

import java.io.*;
class calculate
{
int total(int s1,int s2,int s3,int s4,int s5)
{
return(s1+s2+s3+s4+s5);
}
int average(int sum)
{
return(sum/5); }
}
public class progrm1 {

public static void main(String[] args) throws Exception {

String name;
int sub1,sub2,sub3,sub4,sub5,tot,regno;
float avg;
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the name of the student:");
name=new String(br.readLine());
System.out.println("enter the register number:");
regno=Integer.parseInt(br.readLine());
System.out.println("enter the sub 1 mark:");
sub1=Integer.parseInt(br.readLine());

6
System.out.println("enter the sub 2 mark:");
sub2=Integer.parseInt(br.readLine());
System.out.println("enter the sub 3 mark:");
sub3=Integer.parseInt(br.readLine());
System.out.println("enter the sub 4 mark:");
sub4=Integer.parseInt(br.readLine());
System.out.println("enter the sub 5 mark:");
sub5=Integer.parseInt(br.readLine());
calculate obj1=new calculate(); // object creation
tot=obj1.total(sub1,sub2,sub3,sub4,sub5);

avg=obj1.average(tot);
System.out.println("\t\t STUDENT MARK SHEET");

System.out.println("\t\t -------------------------------------");
System.out.println("NAME:"+name);
System.out.println("REG NO:"+regno);
System.out.println("MARK IN SUBJECT 1:"+sub1);
System.out.println("MARK IN SUBJECT 2:"+sub2);
System.out.println("MARK IN SUBJECT 3:"+sub3);
System.out.println("MARK IN SUBJECT 4:"+sub4);
System.out.println("MARK IN SUBJECT 5:"+sub5);
System.out.println("AVERAGE SCORE:"+avg);

7
}

OUTPUT SCREENSHOT:

8
EX.NO:02
DATE: 16.08.2021
FINDING FACTORIAL OF A GIVEN NUMBER

AIM AND PROCEDURE:

9
10
PROGRAM CODE:

import java.io.BufferedReader;
import java.io.InputStreamReader;

class calculatefactorial
{
int prod=1;
int fact (int num)
{
int i = num;
while(i>0)
{
prod = prod * i;
i--;
}
return prod;
}
}
public class factorial
{

public static void main(String[] args) throws Exception


{
int number,res;
BufferedReader br = new BufferedReader (new
InputStreamReader(System.in));
System.out.println("\t\t factorial of number");
System.out.println("\t\t ====================");
System.out.println("enter the number: ");

11
number=Integer.parseInt(br.readLine());
calculatefactorial obj = new calculatefactorial ();
res = obj.fact(number);

System.out.println(" the factorial of "+number+" is "+res);


}

OUTPUT SCREENSHOT:

12
EX.NO:03
DATE: 23.08.2021

BANK TRANSACTION USING INHERITANCE

AIM AND PROCEDURE:

13
14
PROGRAM CODE:

import java.io.*;
class transaction
{
String customer;
int accnum,withdraw,deposit;
int balance=50000;

int display() throws Exception


{
int res;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(" hello Enter your name:");
customer=new String(br.readLine());
System.out.println(" Enter your account number:");
accnum=Integer.parseInt(br.readLine());
System.out.println("what do you want to do???? \n 1)withdraw money.
\n 2)deposit money.");
res=Integer.parseInt(br.readLine());
return res;
}
}
class bankfunction extends transaction
{
void withdraw() throws Exception
{

15
BufferedReader br=new BufferedReader( new
InputStreamReader(System.in));
System.out.println("avilable balance in your account : "+balance);
System.out.println("enter the amount to be withdraw : ");
withdraw=Integer.parseInt(br.readLine());
balance=balance-withdraw;
System.out.println("transaction successfull :) \n available balance in
your accont is "+balance);
System.out.println("thank you for using our service!!!!");
}
void deposit() throws Exception
{
BufferedReader br=new BufferedReader( new
InputStreamReader(System.in));
System.out.println("avilable balance in your account : "+balance);
System.out.println("enter the amount to be deposited : ");
deposit=Integer.parseInt(br.readLine());
balance=balance+deposit;
System.out.println("transaction successfull :) \n available balance in
your accont is "+balance);
System.out.println("thank you for using our service!!!!");
}

}
public class banktransaction
{
public static void main(String[] args) throws Exception
{
int number;
bankfunction obj1=new bankfunction();
number = obj1.display();
if(number==1)

16
{
obj1.withdraw();
}
else if(number==2)
{
obj1.deposit();
}
else
{
System.out.println("invalid entry :( \n please try tp entry 1 or 2");
}
}
}

OUTPUT SCREENSHOT:

17
EX.NO:04
DATE: 25.08.2021

KEYEVENT CLASS

AIM AND PROCEDURE:

18
PROGRAM CODE:

import java.awt.*;
import java.awt.event.*;
public class keyevent extends Frame implements KeyListener
{

private static final long serialVersionUID = 1L;


Label l;
TextArea area;
keyevent()
{
l = new Label();
l.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300,300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
l.setText("key typed");
}
@Override

19
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
l.setText("key pressed");
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
l.setText("key released");
}
public static void main(String[]args) {
new keyevent();
}

}
OUTPUT SCREENSHOT:-1

20
OUTPUT SCREENSHOT:-2

21
EX.NO:05
DATE:02.09.2021
IMPLEMENTING BUTTON CONTROL

22
AIM AND PROCEDURE:

PROGRAM CODE:

23
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="buttonprogram.class" height=600 width=600>
</applet>
*/
public class buttonprogram extends Applet implements
ActionListener{
String msg=" ";
Button yes,no,maybe;
public void init() {
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);

}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Yes")){
msg="You pressed Yes";

24
}
else if(str.equals("No")){
msg="You pressed No";
}
else{
msg="You pressed Undecided";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,6,100);
}

}
OUTPUT SCREENSHOT:

25
EX.NO:06
DATE: 07.09.2021
IMPLEMENTING CHECKBOX CONTROL
AIM AND PROCEDURE:

26
PROGRAM CODE:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Checkboxdemo extends Applet implements


ItemListener
{
String msg=" ";
Checkbox winXP,winVista,solaris,mac;

public void init(){


winXP=new Checkbox("Windows XP ",null,true);
winVista = new Checkbox("Windows Vista");
solaris = new Checkbox("Solaris");
mac=new Checkbox("Mac OS");

add(winXP);
add(winVista);
add(solaris);
add(mac);

winXP.addItemListener(this);
winVista.addItemListener(this);

27
solaris.addItemListener(this);
mac.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie){
repaint();
}
public void paint (Graphics g){
msg="Current state: ";
g.drawString(msg, 6, 80);
msg=" Windows XP: " + winXP.getState();
g.drawString(msg, 6, 100);
msg=" Windows Vista: " +winVista.getState();
g.drawString(msg, 6, 120);
msg= " Solaris: " + solaris.getState();
g.drawString(msg, 6, 140);
msg=" Mac OS: " + mac.getState();
g.drawString(msg, 6, 160);
}
}
OUTPUT SCREENSHOT:

28
EX.NO:07
DATE: 09.09.2021
IMPLEMENTING LIST CONTROL
AIM AND PROCEDURE:

29
30
PROGRAM CODE:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="listprogram.class" width=500 height=700>
</applet>
*/
public class list extends Applet implements ActionListener{
List os,browser;
String msg="";
public void init(){
os=new List(4,true);
browser=new List(4,false);
os.add("Windows 98/XP");
os.add("Windows NT/2000");
os.add("Solaris");
os.add("MacOS");
browser.add("Netscape 3.x");
browser.add("Netscape 4.x");
browser.add("Netscape 5.x");
browser.add("Netscape 6.x");
browser.add("Internet Explorer 4.0");
browser.add("Internet Explorer 5.0");
browser.add("Internet Explorer 6.0");

31
browser.add("Lynx 2.4");
browser.select(1);
add(os);
add(browser);
os.addActionListener(this);
browser.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
repaint();
}
public void paint(Graphics g) {
int idx[];
msg="Current OS : ";
idx=os.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=os.getItem(idx[i])+" ";
g.drawString(msg,6,120);
msg="Current Browser : ";
msg+=browser.getSelectedItem();
g.drawString(msg,6,140);
}
}

32
OUTPUT SCREENSHOT:

33
EX.NO:08
DATE: 01.10.2021
IMPLEMENTING GRAPHICS

AIM AND PROCEDURE:

PROGRAM CODE:

import java.applet.*;
import java.awt.*;
/*<applet code="applet 1" width=400 height=500></applet>*/
public class demo extends Applet{

34
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawString("circle",10,10);
g.drawOval(20,20,50,50);
g.drawLine(10,80,300,80);
g.setColor(Color.pink);
g.drawString("oval",10,100);
g.fillOval(20,120,75,50);
g.drawLine(10,190,300,190);
g.setColor(Color.red);
g.drawString("rectangle",10,210);
g.drawRect(20,240,100,50);
g.drawLine(10,310,300,310);
g.setColor(Color.green);
g.drawString("round rectangle",10,350);
g.drawRoundRect(20,370,100,60,30,30);
g.drawLine(10,450,300,450);
g.setColor(Color.yellow);
g.drawString("polygon",10,500);
int n[]= {500,400,300,200,100};
int m[]= {500,400,600,500,600};
g.fillPolygon(n,m,5);
}}

35
OUTPUT SCREENSHOT:

36
EX.NO:09
DATE: 07.10.2021
EMPLOYEE DETAILS
AIM AND PROCEDURE:

37
38
PROGRAM CODE:
Code 1:

package package1;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class personal {

static String name,des,dob;


static int empid,age,years;
static long phonenum;

public static void main(String[] args) throws Exception


{
BufferedReader br= new BufferedReader (new
InputStreamReader(System.in));
System.out.println("\t\t EMPLOYEE DETAILS \n \t\t
==================");
System.out.println("enter your name:");
name=new String(br.readLine());

if(name.equals("vinitha")||name.equals("linda")||name.equals
("roshini")||name.equals("uthra")||name.equals("shirely"))
{
System.out.println("enter your id: ");

empid=Integer.parseInt(br.readLine());
if(empid==1001||empid==1002||empid==1003||empid==1004||
empid==1005)
{

39
System.out.println("enter your designation: ");
des=br.readLine();
System.out.println("enter your date of birth: ");
dob=br.readLine();
System.out.println("enter your age: ");
age=Integer.parseInt(br.readLine());
System.out.println("enter your mobile number: ");
phonenum=Long.parseLong(br.readLine());
System.out.println("enter your year of experience:
");
years=Integer.parseInt(br.readLine());
}
else
{
System.out.println("INVALID ID.....PLEASE ENTER A
VALID ID :)");
}}
else
{
System.out.println("SORRY YOU CANNOT ACCESS THE
EMPLOYEE DETAILS!!!!!");
System.exit(1);
}

}
public void displaydetails()
{
System.out.println("---------------------------------\n\t\tSALARY
DETAILS \n\t\t================");
System.out.println("---------------------------------");
System.out.println("NAME: "+name);
System.out.println("EMPLOYEE ID: "+empid);

40
System.out.println("DESINGNATION: "+des);
System.out.println("YEARS OF EXPERIANCE: "+years);
System.out.println("PHONE NUMBER: "+phonenum);

Code 2:

package package2;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import package1.personal;
public class salary
{
static int bpay,da,hra,ta,ma,epf,netpay,grosspay;
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception
{
personal obj1=new personal();
obj1.main(null);
BufferedReader br= new BufferedReader (new
InputStreamReader(System.in));
System.out.println("enter the amount of basic pay: ");
bpay=Integer.parseInt(br.readLine());
da=(bpay*10)/100;
hra=(bpay*15)/100;
ta=(bpay*10)/100;
ma=(bpay*10)/100;
epf=(bpay*20)/100;

41
netpay=bpay+da+hra+ma;
grosspay=netpay-epf;

obj1.displaydetails();
displaydetails();
}
static void displaydetails()
{
System.out.println("DEARNESS ALLOWANCE: "+da);
System.out.println("HOUSE RENT ALLOWANCE: "+hra);
System.out.println("TRAVELLING ALLOWANCE: "+ta);
System.out.println("MEDICAL ALLOWANCE: "+ma);
System.out.println("EMPLOYEE PROVIDENT FUND: "+epf);
System.out.println("NETPAY: "+netpay);
System.out.println("GROSSPAY: "+grosspay);
System.out.println("-----------------------------------------------------------");
}}
OUTPUT SCREENSHOT:

Output 1:

42
Output 2:

43
EX.NO:10
DATE: 09.10.2021
ELECTRICITY BILL

AIM AND PROCEDURE:

44
PROGRAM CODE:

import java.io.*;
import java.util.*;
interface billcalculation
{
void calculate();
}

class ebill1
{
int billnum,pre,curr,units;
float amt;
String address,name;
void customerdetails()throws Exception
{
BufferedReader br=new BufferedReader (new
InputStreamReader (System.in));
System.out.println("Enter Your Name:");
name=new String(br.readLine());
System.out.println("Enter Your Address:");
address=new String(br.readLine());
System.out.println("Enter your Bill number:");
billnum= Integer.parseInt(br.readLine());
System.out.println("Enter your current reading:");
curr= Integer.parseInt(br.readLine());

45
System.out.println("Enter your previous reading:");
pre=Integer.parseInt(br.readLine());

}
void display()
{
System.out.println("__________________________");
System.out.println(" ELECTRICITY BILL ");
System.out.println("NAME: "+name);
System.out.println("BILL NUMBER: "+billnum);
System.out.println("ADDRESS: "+address);
System.out.println("CURRENT READING: "+curr);
System.out.println("PREVIOUS READING: "+pre);
System.out.println("UNITS CONSUMED: "+units);
System.out.println("TOTAL AMOUNT: "+amt);
Date date=java.util.Calendar.getInstance().getTime();
System.out.println("AMOUNT PAID AT: "+date);
System.out.println("______________________");
}
}

class ebill2 extends ebill1 implements billcalculation


{
public void calculate()
{
units=curr-pre;
if (units<=100)
amt=0;

46
else if(units>100 && units<=300)
{
amt=(100*2)+(units-100)*5;

}
else if(units>300 && units<=500)
{
amt=(100*2)+200*5+(units-300)*7;
}
else
{
amt=(100*2)+(200*5)+(200*7)+((units-5)*10);
}
}
}

public class electricitybill1


{
public static void main(String[]args)throws Exception
{
ebill2 obj1=new ebill2();
obj1.customerdetails();
obj1.calculate();
obj1.display();
}
}

47
OUTPUT SCREENSHOT:

48
EX.NO:11
DATE:16.10.2021
EXCEPTION

AIM AND PROCEDURE:

49
50
PROGRAM CODE:
public class exceptionhandeling2
{
public static void main(String[] args)
{
int a[]= {5,10};
int b=5;
try
{
@SuppressWarnings("unused")
int x=a[2]/(b-a[1]);
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}
catch(ArrayIndexOutOfBoundsException e )
{
System.out.println("array index error");
}
catch(ArrayStoreException e )
{
System.out.println("wrong data type");
}

int y=a[1]/a[0];
System.out.println("y= "+y);
}

51
}

OUTPUT SCREENSHOT:

52
EX.NO:12
DATE: 20.10.2021 THREADS

AIM AND PROCEDURE:

53
54
PROGRAM CODE:

class A extends Thread


{

public void run()


{

for(int i=1;i<=5;i++)
{

System.out.println("from thread A="+i);

System.out.println("Exit from thread A");

}
}
class B extends Thread
{
public void run()
{

55
for(int j=6;j<=10;j++)
{

System.out.println("from thread B="+j);

System.out.println("Exit from thread B");

}
}
class C extends Thread
{

public void run()


{

for(int k=11;k<=15;k++)
{

System.out.println("from thread C="+k);


}

System.out.println("Exit from thread C");


}
}
public class threads
{

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

A a1=new A();
B b1=new B();
C c1=new C();
a1.setPriority(9);
b1.setPriority(5);
c1.setPriority(2);
a1.start();
b1.start();
c1.start();
}
}
OUTPUT SCREENSHOT:

57
EX.NO:13
DATE: 25.10.2021
COMPARING TWO TEXT FILES
AIM AND PROCEDURE:

58
PROGRAM CODE:

package file;
import java.io.*;
public class comparetext
{
public static void main(String[] args) throws IOException
{
System.out.println("\t\tCOMPARING TWO TEXT
FILES\n\t\t==================");
BufferedReader br1=new BufferedReader(new
FileReader("C:\\Users\\User\\Desktop\\c1"));
BufferedReader br2=new BufferedReader(new
FileReader("C:\\Users\\User\\Desktop\\c2"));
String line1=br1.readLine();
String line2=br2.readLine();
boolean areequal=true;
int linenum=1;
while(line1!=null||line2!=null)
{
if(line1==null||line2==null)
{
areequal=false;
break;
}
else if(!line1.equalsIgnoreCase(line2))
{
areequal=false;
break;
}
line1=br1.readLine();
line2=br2.readLine();

59
linenum++;
}
if(areequal)
{
System.out.println("two files have same content");
}
else
{
System.out.println("two files have different content.they differ at
line"+linenum);
System.out.println("file 1 has "+line1+"and file 2 has "+line2+"at line
"+linenum);
}
br1.close();
br2.close();
}

OUTPUT SCREENSHOT:

60
Ex.no: 14 INSERTING AND DELETING AN ELEMENT IN A
BINARYSEARCH TREE
DATE: 15.11.2021

AIM AND PROCEDURE:

61
62
PROGRAM CODE:
public class binarysearchtree {
class Node
{
int key;
Node left,right;
public Node(int item)
{
key=item;
left=right=null;
}
}
Node root;
binarysearchtree()
{
root=null;
}
void deletekey(int key)
{
root=deleteRec(root,key);
}
Node deleteRec(Node root,int key)
{
if(root==null)
return root;
if(key<root.key)
root.left=deleteRec(root.left,key);
else if (key>root.key)
root.right=deleteRec(root.right,key);
else
{
if(root.left==null)

63
return root.right;
else if(root.right==null)
return root.left;
root.key=minValue(root.right);
root.right=deleteRec(root.right,root.key);

}
return root;
}
int minValue(Node root)
{
int minv=root.key;
while(root.left!=null)
{
minv=root.left.key;
root=root.left;
}
return minv;
}
void insert(int key)
{
root=insertRec(root,key);
}

Node insertRec(Node root,int key)


{
if(root==null)
{
root=new Node(key);
return root;
}
if(key<root.key)

64
{
root.left=insertRec(root.left,key);
}
else if(key>root.key)
{
root.right=insertRec(root.right,key);
}
return root;
}

void inorder()
{
inorderRec(root);
}
void inorderRec(Node root)
{
if(root!=null)
{
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("\t\tBINARY SEARCH
TREE\n\t\t==================");
binarysearchtree tree=new binarysearchtree();
tree.insert(50);
tree.insert(30);
tree.insert(20);

65
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.println("The Inorder traversal of the given tree is ");
tree.inorder();
System.out.println("Delete 20\n---------");
tree.deletekey(20);
System.out.println("Inorder traversal of the modified tree :- ");
tree.inorder();
System.out.println("Delete 30\n---------");
tree.deletekey(30);
System.out.println("Inorder traversal of the modified tree :- ");
tree.inorder();
System.out.println("Delete 50\n---------");
tree.deletekey(50);
System.out.println("Inorder traversal of the modified tree :- ");
tree.inorder();
}
}

66
OUTPUT SCREENSHOT:

67
68

68

You might also like