Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Practical

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

DEPARTMENT OF COMPUTER SCIENCE

Government Arts and Science College for Women,


Sathankulam

CERTIFICATE
This is to certify that the bonafide record of the practical work
done in ………………………………………………………………………….....
by………………………………………………… Reg. No…………………………
of …………………………………. during the academic year 2021-2022.

Head of the Department Staff In-Charge

Submitted for the practical examination held on ……………………

External Examiner
1.

2.
CONTENTS
Page Staff
Ex.No Date Title
No Sign
1. MULTIPLE CONSTRUCTOR

Aim:
To write a Java program using multiple constructor.

Algorithm:

Step 1 : Define a class Room.


Step 1.2 : Declare member variables length and breadth as a float
type.
Step 1.3 : Define a method constructor Room with no arguments
and assign value for length and breadth.
Step 1.4 : Define a constructor Room() with two float argument
and assign the values x to length and y to breadth.
Step 1.5 : Define a method area() with no argument and return
type as float and return length*breadth.
Step 2 : Define a main class “prg1”.
Step 2.1 : Creat object r1 call area(); print the value area 1.
Step 2.2 : Creat object r2 call area(); print the value area 2.
Step 2.3 : Creat object r3 call area(); print the value area 3.
Step 3 : Save and execute the program.
PROGRAM
class Room
{
float length, breadth;
Room()
{
length=0;
breadth=0;
}
Room(float x)
{

length=x;
breadth=x;
}
Room(float x, float y)
{

length=x;
breadth=y;
}
float area()
{
return(length*breadth);
}
}
class prg1
{
public static void main(String args[])
{
Room r1=new Room();
Room r2=new Room(10.0F);
Room r3=new Room(20.0F,25.0F);
System.out.println("Area 1="+r1.area());
System.out.println("Area 2="+r2.area());
System.out.println("Area 3="+r3.area());
}
}
OUTPUT

RESULT
Thus the Java program using multiple constructor is executed
successfully.
________________________________________________________
2. DIFFERENT TYPES OF INHERITANCE

Aim:
To write a Java program using different types of inheritance.

Algorithm:
Step 1 : Define a class “Student”.
Step 1.1 : Declare member variable rollno as integer.
Step 1.2 : Define a method getno() with one argument and with
return type void and assign value of rollno.
Step 1.3 : Define a method putno() with no argument and with
return type void to print the rollno.
Step 2 : Define a class “Test” which is inheritance from class “Student”.
Step 2.1 : Define a method getmark() with two arguments and
assign the value to m1 and m2.
Step 2.2 : Define a method putmark() with no arguments to
display m1,m2.
Step 3 : Define a interface “Sports”.
Step 3.1 : Declare a member variable Sportswt with initial value
60F with type float.
Step 3.2 : Declare a method putno() with no arguments.
Step 4 : Define a class “Result” which is inherited from the class “Test”
and interface “Sports”.
Step 4.1 : Declare a member variable total as float.
Step 4.2 : Define a method putwt() which is implement from
interface “Sports” with access specifier ”public” to
display the sportswt.
Step 4.3 : Define a method display() with return type void .
(a) Display total.
(b) Call putno(), putmark(), putwt().
Step 5 : Define a class “prg2”
Step 5.1 : Define a main method.
Step 5.2 : Creat a object “r” invoke the method getno(), getmark()
and display().
Step 6 : Save and execute the program.
PROGRAM

class student
{
int roll_no;
void getno(int n)
{
roll_no=n;
}
void putno()
{
System.out.println("ROLL_NO="+roll_no);
}
}
class test extends student
{
float m1,m2;
void getmark(float a1, float a2)
{
m1=a1;
m2=a2;
}
void putmark()
{
System.out.println("MARK 1="+m1);

System.out.println("MARK 2="+m2);
}
}
interface sports
{
float sportswt=6.0F;
void putwt();
}
class result extends test implements sports
{
float total;
public void putwt()
{
System.out.println("Sports weight="+sportswt);
}
void display()
{
total=m1+m2+sportswt;
putno();
putmark();
putwt();
System.out.println("Total="+total);
}
}
class prg2
{
public static void main(String args[])
{
result r=new result();
r.getno(111);
r.getmark(85.0F,90.0F);
r.display();
}
}
OUTPUT

RESULT
Thus the Java program using different types of inheritance is executed
successfully.
________________________________________________________________
3.OVERRIDING METHODS

Aim:
To write a Java program using overriding method.

Algorithm:

Step 1 : Define a super class “A”.


Step 1.1 : Declare a member variable x as integer type.
Step 1.2 : Define a constructor A() with one integer argument
and assign the value for x.
Step 1.3 : Define a method display() with no argument and
display the values of x.
Step 2 : Define a sub class “B” which is inherited from super class “A”.
Step 2.1 : Declare a member variable y as integer type.
Step 2.2 : Define a constructor B() with two integer arguments.
Step 2.2.1 : Invoke superclass constructor A().
Step 2.2.2 : Assign value of y1 to y.
Step 2.3 :.Define a method display() with no argument.
Step 2.3.1 : Invoke display() of super class-super.display().
Step 2.3.2 : Print the value of y.
Step 3 : Define a main class overrideTest
Step 3.1 : Creat object “b” for sub class “B”.
Step 3.2 : Using object b to call the display().
Step 4 : Save and execute the program.
PROGRAM
class A
{
int x;
A(int x1)
{
x=x1;
}
void display()
{
System.out.println("In super class A-x="+x);
}
}
class B extends A
{
int y;
B(int x1,int y1)
{
super(x1);
y=y1;
}
void display()
{
super.display();
System.out.println("In sub class B-y="+y);
}
}
class prg3
{
public static void main(String args[])
{
B bobj=new B(100,200);
bobj.display();
}
}
OUTPUT

RESULT

Thus the Java program using overriding method is executed


successfully.
________________________________________________________________
4. ONE DIMENSIONAL ARRAY

Aim:
To write a Java program using one dimensional array.

Algorithm:

Step 1 : Define a class “prg4”.


Step 1.1 :.Define a main() method.
Step 1.1.1 : Declare member variables i,j,t,n as integer.
Step 1.1.2 : Declare integer array a[],t,n and assign five
values.
Step 1.1.3 : Assign the length of array a to n.
Step 1.1.4 : Repeat step(a) for i=0 to n-1
Step a : Repeat step(b) for j=i+1 to n-1
Step b : if(a[i]<a[j])
t=a[i]
a[i]=a[j]
a[j]=t
Step 1.1.5 : Repeat step(c) for i=0 to n-1
Step c : Display a[i]
Step 2 : Save and execute the program.
PROGRAM
class prg4
{
public static void main(String args[])
{
int i,j,t,n;
int a[]={56,74,65,42,24};
n=a.length;
System.out.println("The given sort");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
for(i=0;i<=n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}

System.out.println("Sorted array");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT

RESULT
Thus the Java program using one dimentional array is executed
successfully.
5. TWO DIMENSIONAL ARRAY

Aim:
To write a Java program using Two Dimensional array

Algorithm:

Step 1 : Import the package scanner.


Step 2 : Define a class “prg5”.
Step 2.1 : Define a main() method.
Step 2.1.1 : Creat a object ‘sc’ for class “scanner”.
Step 2.1.2 : Declare member variable i,j,r,c as integer.
Step 2.1.3 : Declare two dimensional array a[][] as integer
type.
Step 2.1.4 : Read the number of rows and columns.
Step 2.1.5 : Read the value of rows and columns.
Step 2.1.6 : Display the transpose of matrix a[i][j].
Step 3 : Save and execute the program.
PROGRAM
import java.util.Scanner;
class prg5
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[5][5];
int r,c,i,j;
System.out.println("Enter rand c");
r=sc.nextInt();
c=sc.nextInt();
System.out.println("Enter the value");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=sc.nextInt();
System.out.println("The given matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.println(a[j][i]+"");
}
System.out.println();
}
}
}
OUTPUT

Result
Thus a Java Program using Two Dimensional array is executed
successfully.
________________________________________________________
6. IMPLEMENTING INTERFACE

Aim:
To write a Java program to implementing interfaces.

Algorithm:

Step 1 : Define an interface “Area”


Step 1.1 : Declare member variable p1 as static final float type and
assign the value 3.14F.
Step 1.2 : Declare method compute() with two float type
arguments and with return type float.
Step 2 : Define a class “Rectangle” which is implemented from interface
Area.
Step 2.1 : Declare a method compute() with two arguments and
return the area of rectangle(x*y).
Step 3 : Define a class “circle” which is implemented from interface
“Area”.
Step 3.1 : Declare a method compute() with two arguments and
return area of circle(pi*x*x).
Step 4 : Define a class “prg6”
Step 4.1 : Define a method main()
Step 4.1.1 : Creat object “r1” for class Rectangle.
Step 4.1.2 : Creat object “c1” for class circle.
Step 4.1.3 : Using object “r1” call the method
compute(10.0F,20.0F) and display the Area of
Rectangle.
Step 4.1.4 : Using object “c1” call the method
compute(10.0F) and display the Area of circle.
Step 5 : Save and execute the program.
PROGRAM:

interface Area
{
final public float pi=3.14F;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class prg6
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
Circle c1=new Circle();
System.out.println("Area opf
Rectangle="+r1.compute(10.0F,20.0F));
System.out.println("Area of circle="+c1.compute(10.0F,0.0F));
}
}
OUTPUT:

RESULT:
Thus the Java program using implementing interface is executed
successfully.
________________________________________________________________
7.IMPORT PACKAGE

Aim:
To write a Java program using creating a package.

Algorithm:

Step 1 : Create a package “p1”


Step 2 : Define a class “Add” with access specifier public.
Step 2.1 : Declare member variables x,y,z as integer.
Step 2.2 : Define constructor Add() with two integer arguments
assign the values for x and y and store if int z.
Step 2.3 : Define a method display() with access specifies public
and displays the values of x.t and z.
Step 3 : Save the package in the folder p1 as Add java.

Accessing package

Step 1 : Create a new file and import the package p1.


Step 2 : Define a class “prg7”.
Step 2.1 : Define a main method().
Step 2.1.1 : Create object “aobj” for class Add.
Step 2.1.2 : Using object aobj call display A().
Step 3 : Save and execute the program.
PROGRAM:
package p1
public class add
{
int x,y,z;
public Add(int x1,int y1)
{
x=x1;
y=y1;
z=x+y;
}
public void display()
{
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("Add x+y="+z);
}
}

Accessing package:

import p1.Add;
class prg7
{
public static void main(String args[])
{
Add aobj=new Add(10,20);
aobj.display();
}
}
OUTPUT:

RESULT:
Thus the Java program using import package is executed successfully.
________________________________________________________________
8. MULTIPLE THREADS

Aim:
To write a Java program to create and deal multiple threads.

Algorithm:
Step 1 : Define a class “A” which is inherited from class thread.
Step 1.1 : Define a method run() with no argument and with
access specifier public.
Step 1.1.1 : Repeat step1.1.2 for i=1 to 5.
Step 1.1.2 : Print the value of i.
Step 2 : Define a class “B” which is inherited from class Thread.
Step 2.1 : Define a method run() with no arguments and
with access specifier public.
Step 2.1.1 : Repeat step 2.1.2 for i=5.
Step 2.1.2 : Print the value of j.
Step 3 : Define a class “C” which is inherited from class b Thread.
Step 3.1 : Define a method run() no argument and with
access specifier public.
Step 3.1.1 : Repeat step 3.1.2 for k=1 to 5.
Step 3.1.2 :
Step 3.1.3 : Print the value of k.
Step 4 : Define a main class “prg8”.
Step 4.1 : Create object for class-A aobj.
Step 4.2 : Create object for class-B bobj.
Step 4.3 : Create object for class-C cobj.
Step 4.4 : Using aobj call start().
Step 4.5 : Using bobj call start().
Step 4.6 : Using cobj call start().
Step 5 : Save and execute the program.

PROGRAM
class A extends Thread
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println("Class A thread i="+i);
}
System.out.println("Exit from thread A");
}
}
class B extends Thread
{
public void run()
{
for(int j=0;j<=5;j++)
{
System.out.println("class B thread j="+j);
}
System.out.println("Exit from thread B");
}
}
class C extends Thread
{
public void run()
{
for(int k=0;k<=5;k++)
{
System.out.println("class C thread k="+k);
}
System.out.println("Exit from thread C");
}
}
class prg8
{
public static void main(String args[])
{
A aobj=new A();
B bobj=new B();
C cobj=new C();
aobj.start();
bobj.start();
cobj.start();
}
}
OUTPUT

RESULT
Thus the Java program using multiple threads is executed successfully.
________________________________________________________________
9. THROWING YOUR OWN EXCEPTION

Aim:
To write a java program using with throwing your own exception.

Algorith:

Step 1 : Import exception class from package “lang”.


Step 2 : Define a class called “Myexception” which is inherited from class
exception.
Step 2.1 : Define a constructor called “Myexception” with one
string argument.
Step 2.1.1 : Invoke super(msg).
Step 3 : Define a class called “prg9”.
Step 3.1 : Define a method main().
Step 3.2 : Declare member variables x and y as integer type and
assign 5 to x and 1000 to y.
Step 3.3 : Define a try block.
Step 3.3.1 : Declare float variable “z” ;z=(float)x/(float)y.
Step 3.3.2 : repeat step 3.3.1; for k=1 to 5.
Step 3.3.3 : Print the value k.
Step 4 : Define a main class “prg8”.
Step 4.1 : Create object for class A-aobj.
Step 4.2 : Create object for class-B bobj.
Step 4.3 : Create object for class-C cobj.
Step 4.4 : Using aobj call start().
Step 4.5 : Using bobj call start().
Step 4.6 : Using cobj call start().
Step 5 : Save and execute the program.
PROGRAM
import java.lang.Exception;
class MyException extends Exception
{
MyException(String msg)
{
super(msg);
}
}
class prg9
{
public static void main(String args[])
{
int x=5, y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new MyException("The number is too small");
}
}
catch(Exception e)
{
System.out.println("This message is:"+ e.getMessage());
}
}
}
OUTPUT

RESULT
Thus the Java program using throwing your own exception is executed
successfully.
10. DESIGNING A WEB PAGE

Aim:
To write a Java program using applet to design a web page.

Algorithm:

Step 1 : Import classes from awt package.


Step 2 : Import classes from applet package.
Step 3 : Import classes from awt.event package.
Step 4 : Insert applet code “prg10.class” and set the width and height.
Step 5 : Define a class “prg10” which is inherited from the superclass
applet.
Step 5.1 : Declare a font object f1.
Step 5.2 : Define an init() method.
Step 5.2.1 : Sets the font –style and size.
Step 5.2.2 : Declare a label object “la” with the label
“Student details”.
Step 5.2.3 : Set the layout as GridLayout(9,2).
Step 5.2.4 : Declare label objects l,l1,l2,l3,l4,l5,l6and l7
with label “ “,”Name”,”Regno”,”Age”,”Adharno”
“Contact”,”Semester” and “Address”.
Step 5.2.5 : Declare TextField object t1,t2,t3,t4,t5,t6 and
then set the size(50).
Step 5.2.6 : Declare TextArea object t7 and set the
size(15,20).
Step 5.2.7 : add l,la,l1,l2,l3,l4,l5,l6,l7 and t7 one by one
properly.
Step 6 : Save and execute the program.
PROGRAM
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="program10.class" height=400 width=500>
</applet>*/
public class program10 extends Applet
{
Font f1;
public void init()
{
f1=new Font("Arial",Font.BOLD,35);
setLayout(new GridLayout(9,2));
Label l=new Label("Student details");
l.setFont(f1);
Label ll=new Label("");
Label l1=new Label("Name");
Label l2=new Label("rollno");
Label l3=new Label("Age");
Label l4=new Label("Aadhaar");
Label l5=new Label("Semester");
Label l6=new Label("contact no");
Label l7=new Label("Address");
TextField t1=new TextField(50);
TextField t2=new TextField(50);
TextField t3=new TextField(50);
TextField t4=new TextField(50);
TextField t5=new TextField(50);
TextField t6=new TextField(50);
TextArea t7=new TextArea(12,50);
add(l);
add(ll);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(l5);
add(t5);
add(l6);
add(t6);
add(l7);
add(t7);
}
}
OUTPUT

RESULT

Thus the Java program using applet to designing a web page is executed
successfully
________________________________________________________________
11. APPLET TO DISPLAY

Aim:
To write a Java program using Applet to display.

Algorithm:

Step 1 : Import Applet class from package applet.


Step 2 : Import awt package.
Step 3 : Import Event class from package awt.
Step 5 : Define a class prg11 with access specifier public which is
inherited from Applet class and implements Runnable interface.
Step 5.1 : Declare a string object S and assign a string.
Step 5.2 : Create object for class Thread, t and initialize it with null
Step 5.3 : Define a init() method.
Step 5.3.1 : Set the background color.
Step 5.3.2 : Set the foreground color with using
setForeground() method.
Step 5.3.3 : Create object for thread ‘t’.
Step 5.3.4 : Using object t call start() method.
Step 6 : Define a run() method with reurn type void and with access
specifier public.
Step 6.1 : Declare a variable ch as a character type.
Step 6.2 : Define a infinite for loop.
Step 6.2.1 : Define a try block.
Step 6.2.1.1 : Call repaint() method.
Step 6.2.1.2 : Call the sleep() method.
Step 6.2.1.3 : ch msg.CharAt(0).
Step 6.2.1.4 : msg  msg.substring(l,msg.length()).
Step 6.2.1.5 : msg+ch.
Step 6.3 : Define a catch block.
Step 7 : Define a paint() method.
Step 7.1 : Using the object ‘g’ call the drawstring() and display msg
Step 8 : Save and execute the program.
PROGRAM
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<APPLET CODE="prg11.class" WIDTH=400 HEIGHT=200>
</APPLET>*/
public class prg11 extends Applet implements Runnable
{
String msg="Welcome to java programming language....";
Thread t=null;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
t=new Thread(this);
t.start();
}
public void run()
{
char ch;
for(;;)
{
try
{
repaint();
Thread.sleep(400);
ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg+=ch;
}
catch(InterruptedException e)
{}
}
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}
OUTPUT

RESULT
Thus the java program using the applet to display in marquee the text is
executed successfully.
________________________________________________________________
12. MOUSE HANDLING

Aim:
To write a Java program using applet for handling mouse.

Alogorithm:

Step 1 : import package awt.


Step 2 : Import package awt from event.
Step 3 : Define a class “MouselistenerExample” which is inherited from
the class “Frame” and interface “MouseListener”.
Step 3.1 : Declare a label l.
Step 3.2 : Define a constructor MouselistenerExample.
Step 3.3 : Register Mouselistener.
Step 3.4 : Create label l.
Step 3.5 : Set the position for label using setBounds.
Step 3.6 : Add l.
Step 3.7 : Set the size of the window using setSize.
Step 3.8 : Set the layout in null.
Step 4 : Define method mouseclicked to set the text for label l.
Step 5 : Define method mouse entered to set the text for label l.
Step 6 : Define method mouse exited to set the text for label l.
Step 7 : Define method mouse pressed to set the text for label l.
Step 8 : Define method mouse released to set the text for label l.
Step 9 : Define a main() method.
Step 9.1 : Create a object for MouseListenerExample.
Step 10 : Save and execute the program.
PROGRAM
import java.awt.*;
import java.awt.event.*;
/*<applet code="MouseListenerExample.class" width=500
height=200></applet>*/
public class MouseListenerExample extends Frame implements MouseListener
{ Label l;
public MouseListenerExample()
{ addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
l.setText("Mouse Clicked");
}
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();
}}
OUTPUT

RESULT
Thus the Java program using applet in mouse handling is executed
successfully.
13 .KEYBOARD HANDLING
Aim:
To write a Java program using applet in keyboard handling.

Algorithm:
Step 1 : Import package awt.
Step 2 : Import package awt from event.
Step 3 : Import package applet.
Step 4 : Include the applet code.
Step 5 : Define a class “impkey” which is inherited from the class Applet
and implements interface keylistener.
Step 5.1 : Declare a textobject t for TextField.
Step 5.2 : Declare a text ta for TextArea.
Step 6 : Define a init() method.
Step 6.1 : Create object for TextField t.
Step 6.2 : Craete object for TextField ta.
Step 6.3 : add t,ta.
Step 6.4 : Register KeyListener.
Step 7 : Define method KeyPressed if(ke. Getsource()=t)
ta”KeyPressed”.
Step 8 : Define method KeyReleased if(ke. Getsource()=t)
ta”KeyReleased”.
Step 9 : Define method KeyTyped if(ke. Getsource()=t)
ta”KeyTyped”.
Step 10 : Save and execute the program.
PROGRAM
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="prg13.class" width=500 height=200></applet>*/
public class prg13 extends Applet implements KeyListener
{
TextField t;
TextArea ta;
public void init()
{
t=new TextField(20);
ta=new TextArea();
add(t);
add(ta);
t.addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("Key Presssed"+"\n");
}
public void keyReleased(KeyEvent ke)
{
if(ke.getSource()==t)
ta.append("Key Released"+"\n\n");
}
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()==t)
{
char c=ke.getKeyChar();
ta.append("Key Typed "+c+"\n");
}
}
}
OUTPUT

RESULT
Thus the Java program using applet in mouse handling is executed
successfully.

You might also like