GOVERNMENT ARTS AND SCIENCE
COLLEGE
KOVILPATTI
DEPARTMENT OF COMPUTER SCIENCE
JAVA PROGRAMMING LAB
II [Link] Computer Science
2022 - 2023
Name : .........................................................
Reg. No. : ..........................................................
GOVERNMENT ARTS AND SCIENCE COLLEGE
KOVILPATTI
DEPARTMENT OF COMPUTER SCIENCE
BONAFIDE CERTIFICATE
Certified that this is the bonafide record of the work done by
________________________________________________________________
Register No. _________________________ for “JAVA PROGRAMMING
LAB” Practical in the laboratory during the academic year 2023-
2024.
Place:
Date: Professor In-charge.
Submitted for the Bachelor of Computer Science Practical
Examination held on ______________at Department of Computer
Science, Government Arts and Science College, Kovilpatti.
Head of the Department External Examiners:
1.
2.
INDEX
Ex. No Date Name of The Program Page No Staff Sign
Write a JAVA program using
1 03-07-2023
Multiple Constructors. 1
Write a JAVA program using
2 11-07-2023
different types of inheritance. 6
Write a JAVA program using
3 19-07-2023
Overriding Methods. 11
Write a JAVA program using
4 27-07-2023
one-dimensional arrays. 15
Write a JAVA program using
5 04-08-2023
Two-dimensional arrays. 20
Write a JAVA program
6 14-08-2023
implementing interface(s). 25
Write a JAVA program to
7 23-08-2023
create and import package. 31
Write a JAVA program to
8 31-08-2023 create and deal multiple
36
threads.
Write a JAVA program with
9 11-09-2023
throwing your own exception. 41
Write a JAVA program using
10 20-09-2023
Applet to Design a Web Page. 46
Write a JAVA program for
11 10-10-2023
handling mouse events. 51
Write a JAVA program for
12 18-10-2023
handling keyboard events. 57
1
1. Write a JAVA program using Multiple Constructors.
package first;
public class Student {
String name;
int age;
String dept;
Student()
{
name="Abi";
age=18;
dept="CS";
}
Student (String n,int a)
{
name=n;
age=a;
dept="CS";
}
Student(String n,int a,String d)
{
name=n;
age=a;
dept=d;
}
void display()
{
[Link]("Student Details");
[Link]("******* *******");
[Link]("Name of the Student= "+name);
[Link]("Age of the Student= "+age);
[Link]("Department of the Student= "+dept);
[Link]("\n");
2
}
public static void main(String[] args) {
Student s=new Student();
[Link]();
Student s1=new Student("Pavi",19);
[Link]();
Student s2=new Student("Gowshi",19,"CS");
[Link]();
}
3
Output
Student Details
******* *******
Name of the Student= Abi
Age of the Student= 18
Department of the Student= CS
Student Details
******* *******
Name of the Student= Pavi
Age of the Student= 19
Department of the Student= CS
Student Details
******* *******
Name of the Student= Gowshi
Age of the Student= 19
Department of the Student= CS
4
5
6
2. Write a JAVA program using different types of inheritance.
package second;
import [Link].*;
import [Link];
class employ
{
String name;
int eid,bpay;
void getdata()
{
[Link]("Enter the employe name");
Scanner sc=new Scanner([Link]);
name=[Link]();
[Link]("Enter employee id");
eid=[Link]();
[Link]("Enter basic salary");
bpay=[Link]();
}
}
class calc extends employ
{
double hra,da,esi,pf,gpay,npay,ded,ta;
void salarycalc()
{
hra=0.05*bpay;
da=0.02*bpay;
ta=0.01*bpay;
esi=0.02*bpay;
pf=0.04*bpay;
ded=esi+pf;
gpay=bpay+hra+da+ta;
npay=gpay-ded;
}
}
class display extends calc
{
void display()
7
{
[Link]("The name is:"+name);
[Link]("The employe id is:"+eid);
[Link]("The basic salary is:"+bpay);
[Link]("The hra is:"+hra);
[Link]("The ta is:"+ta);
[Link]("The da is:"+da);
[Link]("The esi is:"+esi);
[Link]("The pf is:"+pf);
[Link]("The gpay is:"+gpay);
[Link]("The npay is:"+npay);
}
public static void main(String args[])
{
display d=new display();
[Link]();
[Link]();
[Link]();
}
}
8
Output
Enter the employe name
Raja
Enter employee id
126
Enter basic salary
10000
The name is:Raja
The employe id is:126
The basic salary is:10000
The hra is:500.0
The ta is:100.0
The da is:200.0
The esi is:200.0
The pf is:400.0
The gpay is:10800.0
The npay is:10200.0
9
10
11
3. Write a JAVA program using Overriding Methods.
package third;
import [Link].*;
import [Link];
class supercls
{
int a;
private Scanner sc;
void get()
{
[Link]("Enter the value of a:");
sc=new Scanner([Link]);
a=[Link]();
}
void display()
{
int area;
area=a*a;
[Link]("area of square:"+area);
}
}
class subcls extends supercls
{
int perimeter;
void display()
{
perimeter=4*a;
[Link]("perimeter of square is:"+perimeter);
[Link]();
}
public static void main(String args[])
{
subcls obj=new subcls();
[Link]();
[Link]();
}}
12
Output
Enter the value of a:
5
perimeter of square is:20
area of square:25
13
14
15
4. Write a JAVA program using one-dimensional arrays.
package arrays;
import [Link].*;
import [Link];
class Numbersorting
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int num[],i,j;
[Link]("How many numbers to be sorting");
int n=[Link]();
num=new int[n];
[Link]("Enter the numbers:");
for(i=0;i<n;i++)
{
num[i]=[Link]();
}
[Link]("Given list");
for(i=0;i<n;i++)
{
[Link](+num[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
int temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
16
[Link]("Sorted list");
for(i=0;i<n;i++)
{
[Link](+num[i]);
}
}
}
17
Output
How many numbers to be sorting
5
Enter the numbers:
50
60
88
78
85
Given list
50
60
88
78
85
Sorted list
88
85
78
60
50
18
19
20
5. Write a JAVA program using Two-dimensional arrays.
package two;
import [Link].*;
import [Link];
class addtwomatrix
public static void main(String args[])
int m,n,i,j;
Scanner in=new Scanner([Link]);
[Link]("Enter the number of rows columns of matrix");
m=[Link]();
n=[Link]();
int first[][]=new int[m][n];
int second[][]=new int[m][n];
int sum[][]=new int[m][n];
[Link]("enter the elements of first matrix");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
first[i][j]=[Link]();
[Link]("Enter the elements of second matrix");
for(i=0;i<m;i++)
21
for(j=0;j<n;j++)
second[i][j]=[Link]();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
sum[i][j]=first[i][j]+second[i][j];
[Link]("Sum of the matrices");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
[Link](sum[i][j]+"\t");
[Link]();
22
Output
Enter the number of rows columns of matrix
2
2
enter the elements of first matrix
2
3
4
5
Enter the elements of second matrix
6
7
8
9
Sum of the matrices
8 10
12 14
23
24
25
6. Write a JAVA program implementing interface(s).
package implementing;
import [Link].*;
import [Link];
class income
float iamount;
void get()throws IOException
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the income:");
iamount=[Link]([Link]());
interface expenditure
public void get1();
class netincome extends income implements expenditure
{
26
float eamount,namount;
public void get1()
try
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter expenditure:");
eamount=[Link]([Link]());
catch(IOException e)
{}
void print()
[Link]("income:"+iamount);
[Link]("Expenditure:"+eamount);
[Link]("Net income:"+(iamount-eamount));
class interface6
public static void main(String args[])throws IOException
{
27
netincome n=new netincome();
[Link]();
n.get1();
[Link]();
28
Output
Enter the income:
15000
Enter expenditure:
1000
income:15000.0
Expenditure:1000.0
Net income:14000.0
29
30
31
7. Write a JAVA program to create and import package.
package marksheet;
import [Link].*;
import [Link].*;
public class Student1
int regno,sub1,sub2,sub3;
float total,avg;
String name;
Scanner sc=new Scanner([Link]);
Scanner sc1=new Scanner([Link]);
public void getdata()
[Link]("Enter the Roll No:");
regno=[Link]();
[Link]("Enter the Name:");
name=[Link]();
[Link]("Enter the three subject marks:");
sub1=[Link]();
sub2=[Link]();
sub3=[Link]();
total=sub1+sub2+sub3;
32
avg=total/3;
public void putdata()
[Link]("Name:"+name);
[Link]("Reg No:"+regno);
[Link]("First subjiect mark:"+sub1);
[Link]("Secont subjiect mark:"+sub2);
[Link]("Third subjiect mark:"+sub3);
[Link]("Total:"+total);
[Link]("Average:"+avg);
Main Program: Package
package marksheet;
public class studentpakage
{
public static void main(String[]args)
{
Student1 s=new Student1();
[Link]();
[Link]();
}
}
33
Output
Enter the roll no:
111
Enter the name:
Asha
Enter the three subject marks:
70
80
90
Name:Asha
Roll no:111
First subject mark:70
Second subjrct mark:80
Third subject mark:90
Total:240.0
Average:80.0
34
35
36
8. Write a JAVA program to create and deal multiple threads.
import [Link];
class A extends Thread
{
public void run()
{
[Link]("Thread A is started:");
for(int i=1;i<=5;i++)
{
[Link]("\t from thread A:i="+i);
}
[Link]("Exit from thread A:");
}
}
class B extends Thread
{
public void run()
{
[Link]("Thread B is started:");
for(int j=1;j<=5;j++)
{
[Link]("\t from thread B:j="+j);
}
[Link]("Exit from thread B:");
}
}
class C extends Thread
{
public void run()
{
[Link]("Thread C is started:");
for(int k=1;k<=5;k++)
{
[Link]("\t from thread C:k="+k);
}
[Link]("Exit from thread C:");
}
37
}
class Threadtest
{
public static void main(String arg[])
{
new A().start();
new B().start();
new C().start();
}
}
38
Output
Thread A is started:
from thread A:i=1
from thread A:i=2
from thread A:i=3
from thread A:i=4
from thread A:i=5
Exit from thread A:
Thread B is started:
from thread B:j=1
from thread B:j=2
from thread B:j=3
from thread B:j=4
from thread B:j=5
Exit from thread B:
Thread C is started:
from thread C:k=1
from thread C:k=2
from thread C:k=3
from thread C:k=4
from thread C:k=5
Exit from thread C:
39
40
41
9. Write a JAVA program with throwing your own exception.
package exception;
import [Link];
import [Link].*;
import [Link];
import [Link];
class MyException extends Exception
MyException(String message)
super(message);
class userdef
public static void main(String args[])
int age;
DataInputStream ds=new DataInputStream([Link]);
try
[Link]("Enter the age:");
42
age=[Link]([Link]());
if(age<18|| age> 25)
throw new MyException("you are not eligiple");
[Link]("you are eligiple");
catch(MyException e)
[Link]("Caught MyException");
[Link]([Link]());
catch(Exception e)
[Link](e);
43
Output
Enter the age:
20
you are eligiple
Enter the age:
17
Caught MyException
you are not eligiple
44
45
46
10. Write a JAVA program using Applet to Design a Web Page.
import [Link].*;
import [Link].*;
import [Link].*;
public class Myapp extends Applet
{
TextField name,pass;
Button b1,b2;
public void init()
{
Label n=new Label("Name:",[Link]);
Label p=new Label("password:",[Link]);
name=new TextField(20);
pass=new TextField(20);
[Link]('*');
b1=new Button("submit");
b2=new Button("cancel");
add(n);
add(name);
add(p);
add(pass);
add(b1);
add(b2);
[Link](70,90,90,60);
[Link](70,130,90,60);
[Link](280,100,90,20);
[Link](200,140,90,20);
[Link](100,260,70,40);
[Link](180,260,70,40);
}
public void paint(Graphics g)
{
repaint();
}
}
47
/*<html>
<applet code="[Link]" width=300 height=300</applet>
</html>
*/
48
Output
49
50
51
11. Write a JAVA program for handling mouse events
package handling;
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width=1000 height=1000> </applet> */
public class MouseEvents extends Applet implements
MouseListener,MouseMotionListener
String msg =" ";
int x=0,y=0;
public void init
()
addMouseListener(this);
addMouseMotionListener(this);
public void mouseClicked(MouseEvent m)
x=10;
y=10;
msg ="mouse clicked";
52
repaint();
public void mouseEntered(MouseEvent m)
x=10;
y=10;
msg ="mouse Entered";
repaint();
public void mouseExited(MouseEvent m)
x=10;
y=10;
msg ="mouse Exited";
repaint();
public void mousePressed(MouseEvent m)
x=[Link]();
y=[Link]();
msg ="Down";
repaint();
}
53
public void mouseReleased(MouseEvent m)
x=[Link]();
y=[Link]();
msg ="Up";
repaint();
public void mouseDragged(MouseEvent m)
x=[Link]();
y=[Link]();
msg ="*";
showStatus("Draggeg mouse at"+x+"&"+y);
repaint();
public void mouseMoved(MouseEvent m)
showStatus("Moving mouse at " +[Link]()+ " & "+[Link]());
public void paint(Graphics g)
[Link](msg,x,y);
} }
54
Output
55
56
57
12. Write a JAVA program for handling keyboard events.
package keyboard;
import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width=300 height=200> </applet> */
public class KeyEvents extends Applet implements KeyListener
String msg =" ";
int x=10,y=20;
public void init()
addKeyListener(this);
requestFocus();
public void keyPressed(KeyEvent k)
showStatus("key down");
public void keyReleased(KeyEvent k)
{
58
showStatus("key up");
public void keyTyped(KeyEvent k)
msg +=[Link]();
repaint();
public void paint(Graphics g)
[Link](msg,x,y);
59
Output
60
61