0% found this document useful (0 votes)
183 views

JAVA Practical Record

This document contains a certificate for practical work done in Java programming during the 5th semester of the academic year 2022-23 at Loyola Academy. It includes an index listing 30 Java programs covering topics like object creation, checking for prime numbers, swapping values, matrix transpose, checking for palindromes, perfect numbers, arrays, strings, inheritance, exceptions and more. The programs demonstrate concepts in Java and were likely assignments completed as part of a course.

Uploaded by

Ram Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views

JAVA Practical Record

This document contains a certificate for practical work done in Java programming during the 5th semester of the academic year 2022-23 at Loyola Academy. It includes an index listing 30 Java programs covering topics like object creation, checking for prime numbers, swapping values, matrix transpose, checking for palindromes, perfect numbers, arrays, strings, inheritance, exceptions and more. The programs demonstrate concepts in Java and were likely assignments completed as part of a course.

Uploaded by

Ram Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Loyola Academy

Old Alwal, Secunderabad – 500010


(An Autonomous Degree College Affiliated to Osmania University)
Accredited by NAAC with ’A’ Grade (Third Cycle) A College with
Potential for Excellence by UGC.

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

3 Swap two numbers using 3rd Variable 4

4 Transpose of a Matrix 5-7


5 Check whether the number is a palindrome 8-9

6 Check whether the number is a perfect 10-11


number
7 Sum of numbers 12-13

8 Parameterized Constructor 14

9 15-16

Interfaces
10 1D Array printing integer values 17

11 1D Array printing float values 18


12 1D Array to show ARRAY INDEX 19
OUT OF BOUNDS Exception

13 2D Array by accepting values from user 20-21

14 String Class Methods 22-25

15 Overloading 26

16 Overriding 27

17 Bubble Sort 28-29

18 Create thread using Thread JAVA API 30

19 Thread Implementing Runnable 31


Interface
20 Calculate Factorial using 32
Command Line Arguments
21 Exception Handling 33
22 Program on Applets 34

23 Program on Designing a Button 35-36

24 Program on Designing a Check 37-38


Box
25 Window Listener 39-41

26 Action Listener 42-43

27 Mouse Listener 44-45

28 Mouse Wheel Listener 46-47

29 Item Listener 48-49

30 Key Listener 50-52


JAVA PRACTICAL

1. Program on object creation and zero argument constructor?

class Create

//constructor of the class

Create()

//creating an object using new keyword

Create obj = new Create();

System.out.println("Welcome to object creation");

} class

creation

1
{

public static void main(String[] args)

Javac creation.java

Java creation Output

Welcome to object creation

import java.util.Scanner; class

Prime

public static void main(String arg[])

System.out.println("Enter a number :");

Scanner

sc=new

2
Scanner(System.in); int

n=sc.nextInt(); primeCal(n);

static void primeCal(int num)

int count=0; for

(int i=1;i<=num;i++)

if(num%i==0)

count++;

if(count==2)

System.out.println("Prime number ");

else

System.out.println("Not a prime number ");


}

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,

y, t;// x and y are to swap

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value of X and Y");

x = sc.nextInt(); y = sc.nextInt();

System.out.println("before swapping numbers: "+x + " "+ y);

4
/*swapping */

t = x; x = y;

y = t;

System.out.println("After swapping: "+x +" " + y);

System.out.println( );

output:

before swapping numbers: 10 20

After swapping numbers:20 10

4.Java Program on Tranpose of a matrix

import java.util.Scanner; class

TRANSMatrix

public static void main(String args[])

int row, col,i,j,temp,n;

5
Scanner in = new Scanner(System.in);

System.out.println("Enter the number columns");

System.out.println("Enter the number of rows"); row

= in.nextInt();

col = in.nextInt();

if(row >col)

n=row; else n=

col;

int mat1[][] = new int[n][n];

System.out.println("Enter the elements of matrix");

for ( i= 0 ; i < row ; i++ )

6
for ( j= 0 ; j < col ;j++ )

mat1[i][j] = in.nextInt();

System.out.println("\n\nOriginal matrix:-");

for ( i= 0 ; i < row ; i++ )

for ( j= 0 ; j <col;j++ )

System.out.print(mat1[i][j]+" ");

System.out.println();

7
System.out.println("Transpose of matrix: - ");

for ( i= 0 ; i < col ; i++ )

for ( i= 0 ; i < n; i++ )

for ( j= i+1 ; j < n;j++ )

temp=mat1[i][j] ;

mat1[i][j] =mat1[j][i] ;

mat1[j][i] =temp;

for ( j= 0 ; j < row;j++ )

System.out.print(mat1[i][j]+" ");

System.out.println();

8
}

Output:

Enter the number of rows

Enter the number of rows

Enter the elements of matrix

1234

Transpose of
matrix:

13

24

5.Write a Program

to a check a

number is

palindrome?

import

java.util.Scanner; class Palindrome

9
public static void main(String arg[])

int num,t,s=0,rem;

Scanner sc=new Scanner(System.in);

System.out.println("Enter any number ");

num=sc.nextInt();

t=num;

while(num>0) {

rem=num%10;

s=(s*10)+rem;

num=num/=10)

} if(s==t)

System.out.println(t+" is a palindrome number ");

else

10
System.out.println(t+" is not a palindrome number ");

Output:

Enter any number

121

121 is palindrome number

6.Write a Program to a check a number is perfect?

11
import java.util.Scanner; class

public static void main(String arg[])

Scanner sc=new Scanner(System.in);

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)

System.out.println(n+" is a perfect number");

else

System.out.println(n+" is not a perfect number");

Output:

Enter a number

6 is a perfect
number

13
7.Write a Program to a check the sum of numbers?

import java.util.Scanner; class sum

public static void main(String arg[])

int n,sum=0;

Scanner sc=new Scanner(System.in);

Syst

em. out.

print ln("

ente System.out.println("enter the "+n+" numbers "); r

how

System.out.println("enter number "+(i+1)+":");

many numbers you want sum"); n=sc.nextInt();

int a[]=new int[n];

for(int i=0;i<n;i++)

14
a[i]=sc.nextInt();

for(int i=0;i<n;i++)

sum+=a[i];

System.out.println("sum of "+n+" numbers is ="+sum);

Output: enter how many numbers you

want sum 5

Enter the 5 numbers

12345

Sum of 5 numbers is 15

8. Program on PARAMETERIZED constructor?

class classdemo

{ int a,b;

classdemo(int

x,int y)

{ a=x; b=y; } void

disp()

15
{

System.out.println("a="+a);

System.out.println("b="+b);

} } class trial {

public static void main(String[]args)

classdemo c1=new classdemo(2,3); c1.disp();

O/p: a=2 b=3

9)Write a program on on Interfaces?

interface A { void

print1(); }

interface B { void

print2(); } class ab

implements A,B

16
{ public void print1()

System.out.println("class a: print1");

} public void print2()

System.out.println("class b:print2");

}}

Class interfaceDemo

public static void main(String []args)

{ 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

public static void main(String[ ] args)

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

11. D array printing float values

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........................................................................................................................

12. D array to show ARRAY INDEX OUT OF BOUNDS


Exception class

Arraydemo1

public static void main(String[ ] args)

float a []={10.1f,20.2f,30.3f,40.4f,50.5f}; int

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

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

at Arraydemo1.main(Arraydemo1.java:9)

20
-
13. Write a program on 2 d array by accepting from user

import java.util.Scanner; class scanner {

public static void main(String[]args)

{ int

i,j,m,n;

System.out.println("enter row size"); Scanner

sc= new

Scanner(System.in); m=sc.nextInt();

System.out.println("enter column size");

n=sc.nextInt(); int a[ ][ ]=new int [m]

[n]; System.out.println("enter the

values"); for ( i=0;i<m;i++) for(j=0;

j<n; j++) a[i][j]= sc.nextInt(); for

( i=0;i<m;i++) for(j=0; j<n; j++)

System.out.println(a[i][j]);

21
}

Output : enter row size 2 enter

column size 2 enter the values 1 2 3

123

14) Write a program to Demonstare String class methods class

StringDemo

public static void


main(String [ ]
args)

String s1="hello";

System.out.println(s1);

} } Output : hello

22
a) function: Concatenation

class StringDemo

public static void


main(String [ ]
args)

String s1="hello";

String s2="world";

System.out.println(s1.concat(s2));

}}

Output :

hello

world

b) function: toLowerCase

class StringDemo {

public static void main(String [ ] args)

String s1="hello";

String s2="WORLD";

System.out.println (s2.toLowerCase ( ));

}}

Output : world

23
c) function: toUpper case

class StringDemo

public static void


main(String [ ]
args)

String s1="hello";

String s2="WORLD";

System.out.println (s1.toUpperCase ( ));

}}

Output :

HELLO

d) function: Trim

class StringDemo {

public static void main(String [ ] args)

String s1=" hello";

System.out.println (s1);

System.out.println
(s1.trim ( ));

}}

Output :

hello

24
hello

e) function: IsEmpty

class StringDemo {

public static void main(String [ ] args)

String s1=" hello";

String s2="WORLD";

System.out.println (s1.isEmpty( ));

Output : false

f) function: equals

class StringDemo

public static void main(String [ ] args)

String s1=" hello";

String
s2="WORLD";

System.out.println
(s1.equals(s2));

}}

Output : false

25
g) function: equalsIgnoreCase

class StringDemo

public static void main(String [ ] args)

String s1="hello";

String s2="HELLO";

System.out.println (s1.equalsIgnoreCase(s2));

}}

Output :

True

15)Write a program on overloading public

class Sum {

public int sum(int x, int y) { return (x + y); } public

int sum(int x, int y, int z)

{ return (x + y +

z);

public double
sum(double x,
double y)

{ return (x + y); }

public static void


main(String
args[])

26
{

Sum s = new Sum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

}}

Output:

30

60

31.0

16) Write a program on overriding class

Human

//Overridden method public void eat()

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

{ //Overriding method public void eat()

obj.eat();

Output:

Boy is eating

17) Write a program on Bubble sort

public class BubbleSortExample


{

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

public static void main(String[] args)

int arr[] ={3,60 ,35,2,45,320,5};

temp = arr[j-1]; arr[j-


1] = arr[j]; arr[j] =
temp;
}

}
}

System.out.println("Array Before Bubble Sort");

29
for(int i=0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort");


for(int i=0; i < arr.length; i++)
{

System.out.print(arr[i] + " ");


}

} }

Output:

Array Before Bubble Sort


3 60 35 2 45 320 5
Array After
Bubble Sort
2 3 5 35 45 60 320

18.Write a program to create


Thread using Thread JAVA
API?

class mythread
extends Thread

30
{ public void

run()

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

System.out.println(i);

} } } class threaddemo

public static void main(String args[])

mythread t1=new mythread();

t1.start();

}}

Output:
1

23

45

31
19.Write a program to create Thread Implementing Runnable Interface?

class mythread implements Runnable


{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
}
}}
class threaddemo
{
public static void main(String args[])
{
mythread t1=new
mythread();
Thread t=new
Thread(t1);
t.start();
}
}

Output

1234

20.Write a Program to calculate Factorial using Command Line Arguments?

class Example{

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

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

Exceptional handling? class mythread

{ void

disp()

Try {int

a=3,b=0,c;

c=a/b;

33
System.out.println(c);} catch(Exception

e){

System.out.println(e);

}}} class threaddemo1

public static void main(String[]args)

mythread t=new mythread();

t.disp(); }

O/P: NumberFormatExceptioin:/ by zero

22)Write A Java Program on applets

filename:First.java import

34
java.applet.Applet; import

java.awt.Graphics; public class

First extends Applet{

public void paint(Graphics g){

<applet code="First.class" width="300" height="300">

g.drawString("welcome",150,150);

} }

file name: applet.html

<html>

<body>

</applet>

</body>

</html>

35
javac First.java

appletviewer First.java

appletviewer

applet.html

23.Write a JAVA Program on Designing a BUTTON

import java.awt.*; public class

ButtonExample { public static void

main (String[] args) {

// create instance of frame with the label

Frame f = new Frame("Button Example");

// set the position for the button in frame

b.setBou nds(50,100,80,30);

// create instance of button with label

36
Button b = new Button("Click Here");

// add button to the frame

f.add(b);

// set size, layout and visibility of frame

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

OUTPUT:

37
24.Write a JAVA Program on Designing a CHECK BOX

import java.awt.*; public

class CheckboxExample1

// constructor to initialize

CheckboxExample1() { //

creating the frame with the title

Frame f = new Frame("Checkbox Example");

// creating the checkboxes

Checkbox checkbox1 = new Checkbox("C++");

checkbox1.setBounds(100, 100, 50, 50);


38
Checkbox checkbox2 = new Checkbox("Java", true);
checkbox2.setBounds(100, 150, 50, 50);

// adding checkboxes to frame

f.add(checkbox1);

f.add(checkbox2);

// setting size, layout and visibility of frame

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

// main method

public static void main (String args[])

new CheckboxExample1();

Output:

39
25.Write a JAVA program on Window Listener?

import java.awt.*; import

java.applet.*; import

java.awt.event.*;

public class WindowListenerTest extends Frame implements WindowListener

Label l1,l2;

TextField t1,t2;

Button b1;

public

WindowListenerTest()

40
super("Implementing Window Listener");

setLayout(new GridLayout(4,2)); l1=new

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);

public static void main(String args[])

WindowListenerTest d=new WindowListenerTest();

d.setSize(400,400);

41
public void windowClosing(WindowEvent we)

d.setVisible(true);

this.setVisible(false);

System.exit(0);

public void windowActivated(WindowEvent we)

public void windowDeactivated(WindowEvent we)

public void windowOpened(WindowEvent we)

42
}

public void windowClosed(WindowEvent we)

{}

public void windowIconified(WindowEvent we)

{}

public void windowDeiconified(WindowEvent we)

}}

Output:

43
26.Write a JAVA program on Action Listener?

import java.awt.*; import

java.awt.event.*;

class EventHandle extends Frame implements ActionListener{

TextField textField;

EventHandle()A

44
textField = new TextField();

textField.setBounds(60,50,170,20); Button

button = new Button("Quote");

button.setBounds(90,140,75,40);

//1

button.addActionListener(this);

add(button); add(textField);

setSize(250,250);

setLayout(null);

setVisible(true);

//2

public void actionPerformed(ActionEvent e)

{ textField.setText("Keep Learning");

45
public static void main(String args[]){ new

EventHandle();

Output:

46
27.Write a JAVA program on mouse Listener?

import java.awt.*; import

java.awt.event.*;

public class MouseListenerExample extends Frame implements MouseListener{

Label l;

MouseListenerExample()

{ addMouseListener(this);

public void mouseClicked(MouseEvent e) {

l.setText("Mouse Clicked");

l=new Label();

l.setBounds(20,50,100,20);

47
add(l);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseEntered(MouseEvent e) {

l.setText("Mouse Entered");

public void mouseExited(MouseEvent e) {

l.setText("Mouse Exited");

public void mousePressed(MouseEvent e) {

l.setText("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released");

public static void main(String[] args)

{ new MouseListenerExample();

48
OUTPUT:

49
28.Write a JAVA program on mouse wheel Listener?

import java.awt.event.MouseWheelEvent; import

java.awt.event.MouseWheelListener;

import javax.swing.JFrame; import

javax.swing.JTextArea;

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextArea textArea = new JTextArea();

textArea.addMouseWheelListener(new MouseWheelListener() {

public void mouseWheelMoved(MouseWheelEvent e) {

if (e.getWheelRotation() < 0) {

System.out.println("Up... " + e.getWheelRotation());

public class Main extends JFrame

{ public Main() {

setSize(200, 200);

50
} else {

System.out.println("Down... " + e.getWheelRotation());

System.out.println("ScrollAmount: " + e.getScrollAmount());

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:

29.Write a JAVA program on ItemListener?

import java.awt.*; import

java.awt.event.*;

public class ItemListenerDemo implements ItemListener

Frame jFrame;

52
Checkbox chkBox1, chkBox2;

Label label_11;

jFrame= new Frame("Java Item Listener Example");

chkBox1 = new Checkbox("ANDROID");

chkBox2 = new Checkbox("NETWORKING");

label_11 = new Label();

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
}

public void itemStateChanged(ItemEvent ie)

Checkbox ch =(Checkbox)ie.getItemSelectable();
if(ch.getState()==true)

label_11.setText(ch.getLabel ()+ " is checked");

jFrame.add(label_11); jFrame.setVisible(true);

} else

label_11.setText(ch.getLabel ()+ " is unchecked");

jFrame.add(label_11); jFrame.setVisible(true);

}}

public static void main(String... ar)

54
new ItemListenerDemo();

}}

Output :

30.Write a JAVA program on KeyListener

import java.awt.*; import

java.awt.event.*;

// class which inherits Frame class and implements KeyListener interface

public class KeyListenerExample extends Frame implements KeyListener { //

creating object of Label class and TextArea class

Label l;

TextArea area;

// class constructor

55
KeyListenerExample() {

// creating the label

l = new Label();

// setting the location of the label in frame

l.setBounds (20, 50, 100, 20); //

creating the text area area = new

TextArea(); // setting the location of

text area area.setBounds (20, 80,

300, 300); // adding the KeyListener

to the text area

area.addKeyListener(this);

// adding the label and text area to the frame

add(l); add(area);

// setting the size, layout and visibility of frame

setSize (400, 400);

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) {

* l.setText ("Key Pressed");

// overriding the keyTyped() method of KeyListener interface where we set the text of

// overriding the keyReleased() method of KeyListener interface where we set the


text of the label when key is released public void keyReleased (KeyEvent e) {

l.setText ("Key Released");

the label when a key is typed public

void keyTyped (KeyEvent e) {

57
l.setText ("Key Typed");

// main method

public static void main(String[] args) {

new KeyListenerExample();

OUTPUT :

58
59

You might also like