Java Practical
Java Practical
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.
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:
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:
RESULT
Aim:
To write a Java program using one dimensional array.
Algorithm:
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:
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:
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:
Accessing package
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:
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:
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:
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:
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.