JAVA Practical Record
JAVA Practical Record
Certificate
This is to certify that this bonafide record of the work done in JAVA
Programming during the practical lab for the Fifth Semester of the academic
year 2022-23.
Name :
UID :
Class :
Internal Examiner Principal External Examiner
INDEX
S.No. Topic Page Signature
No.
1 Object Creation and Zero Argument 1
Constructor
2 Check whether the number is Prime 2-3
8 Parameterized Constructor 14
9 15-16
Interfaces
10 1D Array printing integer values 17
15 Overloading 26
16 Overriding 27
class Create
Create()
} class
creation
1
{
Javac creation.java
Prime
Scanner
sc=new
2
Scanner(System.in); int
n=sc.nextInt(); primeCal(n);
(int i=1;i<=num;i++)
if(num%i==0)
count++;
if(count==2)
else
Output:
Enter a number :
3
Prime number
3.Swap two
numbers using a
third variable?
import java.util.*;
class Swap_With
public static
void main(String[]
args) { int x,
x = sc.nextInt(); y = sc.nextInt();
4
/*swapping */
t = x; x = y;
y = t;
System.out.println( );
output:
TRANSMatrix
5
Scanner in = new Scanner(System.in);
= in.nextInt();
col = in.nextInt();
if(row >col)
n=row; else n=
col;
6
for ( j= 0 ; j < col ;j++ )
mat1[i][j] = in.nextInt();
System.out.println("\n\nOriginal matrix:-");
for ( j= 0 ; j <col;j++ )
System.out.print(mat1[i][j]+" ");
System.out.println();
7
System.out.println("Transpose of matrix: - ");
temp=mat1[i][j] ;
mat1[i][j] =mat1[j][i] ;
mat1[j][i] =temp;
System.out.print(mat1[i][j]+" ");
System.out.println();
8
}
Output:
1234
Transpose of
matrix:
13
24
5.Write a Program
to a check a
number is
palindrome?
import
9
public static void main(String arg[])
int num,t,s=0,rem;
num=sc.nextInt();
t=num;
while(num>0) {
rem=num%10;
s=(s*10)+rem;
num=num/=10)
} if(s==t)
else
10
System.out.println(t+" is not a palindrome number ");
Output:
121
11
import java.util.Scanner; class
System.out.println("Enter a number");
Perfect
long n,sum=0;
n=sc.nextLong();
12
int i=1;
while(i<=n/2)
if(n%i==0)
sum+=i;
i++;
if(sum==n)
else
Output:
Enter a number
6 is a perfect
number
13
7.Write a Program to a check the sum of numbers?
int n,sum=0;
Syst
em. out.
print ln("
how
for(int i=0;i<n;i++)
14
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
sum+=a[i];
want sum 5
12345
Sum of 5 numbers is 15
class classdemo
{ int a,b;
classdemo(int
x,int y)
disp()
15
{
System.out.println("a="+a);
System.out.println("b="+b);
} } class trial {
interface A { void
print1(); }
interface B { void
print2(); } class ab
implements A,B
16
{ public void print1()
System.out.println("class a: print1");
System.out.println("class b:print2");
}}
Class interfaceDemo
{ ab obj=new
ab();
obj.print1();
obj.print2(); }
Output: class
a: print1 class
b:print2
17
Write a program on 1-
10. D array printing integer values
class Arraydemo1
int a []={10,20,30,40,50};
int i;
for (i=0;i<5;i++)
System.out.println
(a[i]);
}}
Output:
10 20
30
40
50
class Arraydemo1
18
Write a program on 1-
public static void main(String[ ] args)
float a []={10.1f,20.2f,30.3f,40.4f,50.5f};
int i;
for (i=0;i<5;i++)
System.out.println
(a[i]);
}}
Output: 10..........................................................................................................
20........................................................................................................................
30........................................................................................................................
40........................................................................................................................
50........................................................................................................................
Arraydemo1
i;
19
Write a program on 1-
for (i=0;i<10;i++)
System.out.println
(a[i]);
}}
Output:
10.1
20.2
30.3
40.4
50.5
at Arraydemo1.main(Arraydemo1.java:9)
20
-
13. Write a program on 2 d array by accepting from user
{ int
i,j,m,n;
sc= new
Scanner(System.in); m=sc.nextInt();
System.out.println(a[i][j]);
21
}
123
StringDemo
String s1="hello";
System.out.println(s1);
} } Output : hello
22
a) function: Concatenation
class StringDemo
String s1="hello";
String s2="world";
System.out.println(s1.concat(s2));
}}
Output :
hello
world
b) function: toLowerCase
class StringDemo {
String s1="hello";
String s2="WORLD";
}}
Output : world
23
c) function: toUpper case
class StringDemo
String s1="hello";
String s2="WORLD";
}}
Output :
HELLO
d) function: Trim
class StringDemo {
System.out.println (s1);
System.out.println
(s1.trim ( ));
}}
Output :
hello
24
hello
e) function: IsEmpty
class StringDemo {
String s2="WORLD";
Output : false
f) function: equals
class StringDemo
String
s2="WORLD";
System.out.println
(s1.equals(s2));
}}
Output : false
25
g) function: equalsIgnoreCase
class StringDemo
String s1="hello";
String s2="HELLO";
System.out.println (s1.equalsIgnoreCase(s2));
}}
Output :
True
class Sum {
{ return (x + y +
z);
public double
sum(double x,
double y)
{ return (x + y); }
26
{
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10.5, 20.5));
}}
Output:
30
60
31.0
Human
System.out.println("Human is eating");
27
} class Boy extends
System.out.println("Boy is eating"); }
public static void main( String args[]) { Boy obj = new Boy();
Human
obj.eat();
Output:
Boy is eating
28
static void bubbleSort(int[] arr)
{
int n = arr.length;
int temp = 0; for(int
i=0; i < n; i++){
for(int j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j]){
//swap elements
}
}
29
for(int i=0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
} }
Output:
class mythread
extends Thread
30
{ public void
run()
for(int
i=1;i<=5;i++)
System.out.println(i);
} } } class threaddemo
t1.start();
}}
Output:
1
23
45
31
19.Write a program to create Thread Implementing Runnable Interface?
Output
1234
class Example{
a , b = 1;
32
int n = Integer.parseInt(args[0]); for(a
= 1; a<= n ; a++)
{ b=
b*a;
Output:
1234
21.Write A Java
Program on
{ void
disp()
Try {int
a=3,b=0,c;
c=a/b;
33
System.out.println(c);} catch(Exception
e){
System.out.println(e);
t.disp(); }
filename:First.java import
34
java.applet.Applet; import
g.drawString("welcome",150,150);
} }
<html>
<body>
</applet>
</body>
</html>
35
javac First.java
appletviewer First.java
appletviewer
applet.html
b.setBou nds(50,100,80,30);
36
Button b = new Button("Click Here");
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
OUTPUT:
37
24.Write a JAVA Program on Designing a CHECK BOX
class CheckboxExample1
// constructor to initialize
CheckboxExample1() { //
f.add(checkbox1);
f.add(checkbox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
// main method
new CheckboxExample1();
Output:
39
25.Write a JAVA program on Window Listener?
java.applet.*; import
java.awt.event.*;
Label l1,l2;
TextField t1,t2;
Button b1;
public
WindowListenerTest()
40
super("Implementing Window Listener");
Label("Name"); l2=new
Label("Password");
t1=new TextField(10);
t2=new TextField(10);
t2.setEchoChar('*');
b1=new Button("Send");
add(l1); add(t1);
add(l2); add(t2);
add(b1);
addWindowListener(this);
d.setSize(400,400);
41
public void windowClosing(WindowEvent we)
d.setVisible(true);
this.setVisible(false);
System.exit(0);
42
}
{}
{}
}}
Output:
43
26.Write a JAVA program on Action Listener?
java.awt.event.*;
TextField textField;
EventHandle()A
44
textField = new TextField();
textField.setBounds(60,50,170,20); Button
button.setBounds(90,140,75,40);
//1
button.addActionListener(this);
add(button); add(textField);
setSize(250,250);
setLayout(null);
setVisible(true);
//2
{ textField.setText("Keep Learning");
45
public static void main(String args[]){ new
EventHandle();
Output:
46
27.Write a JAVA program on mouse Listener?
java.awt.event.*;
Label l;
MouseListenerExample()
{ addMouseListener(this);
l.setText("Mouse Clicked");
l=new Label();
l.setBounds(20,50,100,20);
47
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
l.setText("Mouse Entered");
l.setText("Mouse Exited");
l.setText("Mouse Pressed");
l.setText("Mouse Released");
{ new MouseListenerExample();
48
OUTPUT:
49
28.Write a JAVA program on mouse wheel Listener?
java.awt.event.MouseWheelListener;
javax.swing.JTextArea;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea.addMouseWheelListener(new MouseWheelListener() {
if (e.getWheelRotation() < 0) {
{ public Main() {
setSize(200, 200);
50
} else {
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
System.out.println("MouseWheelEvent.WHEEL_UNIT_SCROLL");
if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
System.out.println("MouseWheelEvent.WHEEL_BLOCK_SCROLL");
});
getContentPane().add(textArea);
51
public static void
main(String[]
args) { new
Main().setVisible(true);
Output:
java.awt.event.*;
Frame jFrame;
52
Checkbox chkBox1, chkBox2;
Label label_11;
ItemListenerDemo()
jFrame.add(chkBox1); jFrame.add(chkBox2);
chkBox1.addItemListener(this);
chkBox2.addItemListener(this);
jFrame.setLayout(new FlowLayout());
jFrame.setSize(220,150);
jFrame.setVisible(true);
53
}
Checkbox ch =(Checkbox)ie.getItemSelectable();
if(ch.getState()==true)
jFrame.add(label_11); jFrame.setVisible(true);
} else
jFrame.add(label_11); jFrame.setVisible(true);
}}
54
new ItemListenerDemo();
}}
Output :
java.awt.event.*;
Label l;
TextArea area;
// class constructor
55
KeyListenerExample() {
l = new Label();
area.addKeyListener(this);
add(l); add(area);
56
setLayout (null);
setVisible (true);
// overriding the keyPressed() method of KeyListener interface where we set the text
of the label when key is pressed public void keyPressed (KeyEvent e) {
// overriding the keyTyped() method of KeyListener interface where we set the text of
57
l.setText ("Key Typed");
// main method
new KeyListenerExample();
OUTPUT :
58
59