SIR SYED INSTITUTE FOR TECHNICAL STUDIES
TALIPARAMBA
( Affiliated to Kannur University )
DEPARTMENT OF COMPUTER SCIENCE
PRACTICAL RECORD
JAVA PROGRAMMING
[Link] SCIENCE (MAIN)
2023-2024
NAME : …………………………………………
REG. NO : …………………………………………
SSITS -TALIPARMBA
1
SIR SYED INSTITUTE FOR TECHNICAL STUDIES
TALIPARAMBA
( Affiliated to Kannur University )
DEPARTMENT OF COMPUTER SCIENCE
PRACTICAL RECORD
JAVA PROGRAMMING
[Link] SCIENCE (MAIN)
2023-2024
Certified that this is bonfide record of the work done in the laboratory Sir Syed Institute
for Technical Studies, Taliparamba
By………………………………………………
TALIPARAMBA KHADEEJA K T KHADEEJA K T
19/02/2024 Lecturer in Charge Head of the Department
Submitted at the university practical examination………………….2024
University [Link]:……………………………………………………..
External Examiner :
1.
2.
2
[Link] DATE PROGRAM [Link]
1 07/11/23 To implement inheritance 04
2 10/11/23 To implement interface 06
3 13/11/23 To implement Exception handling 08
4 14/11/23 To implement applet life cycle 10
5 05/12/23 To create a simple gui calculator 11
6 12/12/23 To demonstrate thread 13
7 12/12/23 To implement multi threading 14
8 20/12/23 To implement package 16
9 21/12/23 To implement method overriding 18
10 21/12/23 To implement abstract class 19
11 09/01/24 To draw different figures 20
12 09/01/24 To implement jdbc 22
3
1. Write a program implement inheritance
import [Link].*;
class student
{
int rollno;
String name;
float m1,m2,m3;
void read() throws IOException
{
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter rollno,name and three marks:");
rollno=[Link]([Link]());
name=[Link]();
m1=[Link]([Link]());
m2=[Link]([Link]());
m3=[Link]([Link]());
}
}
class result extends student
{
float total,avg;
void show()
{
total=m1+m2+m3;
avg=total/3;
[Link]("Rollno:"+rollno);
[Link]("Name:"+name);
[Link]("Total mark="+total);
[Link]("Average mark:"+avg);
}
}
class inheritance
{
public static void main(String arg[]) throws IOException
{
result ob=new result();
[Link]();
[Link]();
}
}
4
OUTPUT
D:\java\javac [Link]
D:\java\java inheritance
Enter rollno,name and three marks:
01
amina
33
34
24
Rollno:1
Name:amina
Total mark=91.0
Average mark:30.333334
5
2. Write a program implement interface.
import [Link].*;
interface shape
{
public void circle(float r);
public void square(int s);
public void rectangle(int l,int b);
}
class area implements shape
{
public void circle(float r)
{
[Link]("Area of circle:"+3.14*r*r);
}
public void square(int s)
{
[Link]("Area of square:"+s*s);
}
public void rectangle(int l,int b)
{
[Link]("Area of rectangle:"+l*b);
}
}
class inter
{
public static void main(String ard[]) throws IOException
{
float r;
int s,l,b;
area ob=new area();
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the radius of circle:");
r=[Link]([Link]());
[Link]("Enter the side of square:");
s=[Link]([Link]());
[Link]("Enter the length and breadth of rectangle:");
l=[Link]([Link]());
b=[Link]([Link]());
[Link](r);
[Link](s);
[Link](l,b);
}
}
6
OUTPUT
D:\java\javac [Link]
D:\java\java inter
Enter the radius of circle:
4
Enter the side of square:
8
Enter the length and breadth of rectangle:
6
19
Area of circle:50.24
Area of square:64
Area of rectangle:114
7
3. Write a program implement exception handling.
import [Link].*;
class exception
{
public static void main(String arg[])
{
int a,b,res;
DataInputStream d=new DataInputStream([Link]);
try
{
[Link]("Enter the first number:");
a=[Link]([Link]());
[Link]("Enter the second number:");
b=[Link]([Link]());
res=a/b;
[Link]("Result:"+res);
}
catch(ArithmeticException e1)
{
[Link]("Division by zero");
}
catch(NumberFormatException e2)
{
[Link]("you must enter integer value");
}
catch(Exception e3)
{
[Link]("Something is wrong");
}
}
}
8
OUTPUT
D:\java\javac [Link]
D:\java\ java exception
Enter the first number:
3
Enter the second number:
0
Division by zero
D:\java\ java exception
Enter the first number:
6
Enter the second number:
9.7
you must enter integer value
D:\java\java exception
Enter the first number:
5
Enter the second number:
4
Result:1
9
4. Write a program implement Applet life cycle
//<applet code="[Link]" width="900" height="500">
//</applet>
import [Link].*;
import [Link].*;
import [Link].*;
public class App extends Applet
{
public void init()
{
[Link](null,"Applet initialized..");
}
public void start()
{
[Link](null,"Applet started..");
}
public void paint(Graphics g)
{
[Link]("Applet running...",200,100);
}
public void stop()
{
[Link](null,"Applet stopped..");
}
public void destroy()
{
[Link](null,"Applet destroyed...");
}
}
OUTPUT
D:\java\javac [Link]
D:\java\appletviewer [Link]
10
5. Write a program to create a simple GUI calculator.
//<applet code="[Link]" width=1000 height=500>
//</applet>
import [Link].*;
import [Link].*;
import [Link].*;
public class calculator extends Applet implements ActionListener
{
Label l1=new Label("Enter the first number:");
Label l2=new Label("Enter the second number:");
Label l3=new Label("Result");
TextField t1=new TextField(15);
TextField t2=new TextField(15);
TextField t3=new TextField(15);
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
public void init()
{
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(b3);
add(b4);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
float a,b,c;
a=[Link]([Link]());
b=[Link]([Link]());
if([Link]()==b1)
{
c=a+b;
[Link](c+"");
}
else if([Link]()==b2)
{
c=a-b;
[Link](c+"");
11
}
else if([Link]()==b3)
{
c=a*b;
[Link](c+"");
}
else if([Link]()==b4)
{
c=a/b;
[Link](c+"");
}
}
}
OUTPUT
javac [Link]
appletviewer [Link]
12
6. Write a program demonstrate thread.
import [Link].*;
class mythread implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
[Link]("i="+i);
[Link](1000);
}
}
catch(InterruptedException e)
{
[Link]("Error");
}
}
}
class threadex
{
public static void main(String arg[])
{
mythread ob=new mythread();
Thread t=new Thread(ob);
[Link]();
}
}
OUTPUT
D:\java\javac [Link]
D:\java\java threadex
i=1
i=2
i=3
i=4
i=5
13
7. Write a program implement multi threading
import [Link].*;
class one extends Thread
{
public void run()
{
try
{
while(true)
{
[Link](1000);
[Link]("Good Morning");
}
}
catch(InterruptedException e)
{
}
}
}
class two extends Thread
{
public void run()
{
try
{
while(true)
{
[Link](2000);
[Link]("Hello");
}
}
catch(InterruptedException e)
{
}
}
14
OUTPUT
D:\java\javac [Link]
D:\java\ java multithread
press CTRL+C to stop..
Good Morning
Hello
Good Morning
Welcome
Good Morning
Hello
Good Morning
Good Morning
Hello
Welcome
Good Morning
Good Morning
Hello
Good Morning
Welcome
Good Morning
15
8. Write a program implement packages.
//package file:[Link]
package p1;
import [Link].*;
public class array
{
int x[]=new int[20];
int n,i;
public void read()throws IOException
{
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the limit");
n=[Link]([Link]());
[Link]("Enter the elements:");
for(i=0;i<n;i++)
{
x[i]=[Link]([Link]());
}
}
public void show()
{
[Link]("Entered elements:");
for(i=0;i<n;i++)
{
[Link](x[i]);
}
}
}
16
//main file:[Link]
import [Link].*;
import p1.*;
class mainp
{
public static void main(String arg[])throws IOException
{
array ob=new array();
[Link]();
[Link]();
}
}
OUTPUT
D:\java\p1\javac [Link]
D:\java\:javac [Link]
D:\java\java mainp
Enter the limit
3
Enter the elements:
5
7
8
Entered elements:
5
7
8
17
9. Write a program implement method overriding.
import [Link].*;
class A
{
void fact()throws IOException
{
[Link]("I am base class Function");
}
}
class B extends A
{
void fact()throws IOException
{
int n,f=1,i;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter a number:");
n=[Link]([Link]());
for(i=1;i<=n;i++)
{
f=f*i;
}
[Link]("Factorial is"+f);
}
}
class override
{
public static void main(String arg[])throws IOException
{
B ob=new B();
[Link]();
}
}
OUTPUT
D:\java\javac [Link]
D:\java\java override
Enter a number:
8
Factorial is40320
18
[Link] a program implement abstract class.
import [Link].*;
abstract class Reverse
{
abstract void rev(int num);
}
class Number extends Reverse
{
int x,r=0;
void rev(int num)
{
while(num!=0)
{
x=num%10;
r=r*10+x;
num=num/10;
}
[Link]("Reverse of a number is"+r);
}
}
class Abstr
{
public static void main(String arg[])throws IOException
{
{
Number ob=new Number();
int n;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter a number:");
n=[Link]([Link]());
[Link](n);
}
}
OUTPUT
D:java\javac [Link]
D:\java\java Abstr
Enter a number:
7864
Reverse of a number is4687
19
[Link] a program to draw different figures.
//<applet code="[Link]" width="800" height="500">
//</applet>
import [Link].*;
import [Link].*;
import [Link].*;
public class figure extends Applet implements ItemListener
{
CheckboxGroup cbg=new CheckboxGroup();
Checkbox rect=new Checkbox("Rectangle",cbg,false);
Checkbox circle=new Checkbox("Circle",cbg,false);
Checkbox line=new Checkbox("Line",cbg,false);
int f=0;
public void init()
{
add(rect);
add(circle);
add(line);
[Link](this);
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
if([Link]()==rect)
{
f=1;
repaint();
}
else if([Link]()==circle)
{
f=2;
repaint();
}
else if([Link]()==line)
{
f=3;
repaint();
}
}
public void paint(Graphics g)
{
if(f==1)
{
[Link](100,100,200,100);
}
else if(f==2)
{
[Link](100,100,100,100);
20
}
else if(f==3)
{
[Link](100,100,250,250);
}
}
}
OUTPUT
D:.\java\ javac [Link]
D;\java\appletviewer [Link]
21
12. Write a program implement JDBC
import [Link].*;
import [Link].*;
class DB
{
public static void main(String arg[])
{
try
{
int r,m;
String n;
DataInputStream d= new DatalnputStream([Link]);
[Link]("Enter rollno, name and mark");
r=[Link]([Link]());
n=[Link]();
[Link]([Link]());
[Link]("[Link]");
Connection con=[Link]("jdbc:odbc:dsname"," "," ");
Statement st=[Link]();
[Link]("insert into student values("+r+","+n+", "+m+")");
[Link]("Record inserted...");
ResultSet rs=[Link]("select * from student");
[Link]("Rollno\tName\tMark");
while([Link]())
r=[Link]("rollno");
n=[Link]("name");
m=[Link]("mark");
[Link](r + "\t" + n + "\t" + m);
}
[Link]();
[Link]();
}
22
OUTPUT
D:\java>javac [Link]
D:\java>java DB
Enter rollno,name and mark
105
Amina
34
Record inserted….
Rollno Name Mark
101 Muthu 25
102 Kamala 36
105 Amina 34
23