Java Record2
Java Record2
TALIPARAMBA
( Affiliated to Kannur University )
PRACTICAL RECORD
JAVA PROGRAMMING
2023-2024
NAME : …………………………………………
REG. NO : …………………………………………
SSITS -TALIPARMBA
1
SIR SYED INSTITUTE FOR TECHNICAL STUDIES
TALIPARAMBA
( Affiliated to Kannur University )
PRACTICAL RECORD
JAVA PROGRAMMING
2023-2024
Certified that this is bonfide record of the work done in the laboratory Sir Syed Institute
for Technical Studies, Taliparamba
By………………………………………………
External Examiner :
1.
2.
2
SL.NO DATE PROGRAM PAGE.NO
3
1. Write a program implement inheritance
import java.io.*;
class student
{
int rollno;
String name;
float m1,m2,m3;
void read() throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter rollno,name and three marks:");
rollno=Integer.parseInt(d.readLine());
name=d.readLine();
m1=Float.parseFloat(d.readLine());
m2=Float.parseFloat(d.readLine());
m3=Float.parseFloat(d.readLine());
}
}
class result extends student
{
float total,avg;
void show()
{
total=m1+m2+m3;
avg=total/3;
System.out.println("Rollno:"+rollno);
System.out.println("Name:"+name);
System.out.println("Total mark="+total);
System.out.println("Average mark:"+avg);
}
}
class inheritance
{
public static void main(String arg[]) throws IOException
{
result ob=new result();
ob.read();
ob.show();
}
}
4
OUTPUT
D:\java\javac inheritance.java
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 java.io.*;
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)
{
System.out.println("Area of circle:"+3.14*r*r);
}
public void square(int s)
{
System.out.println("Area of square:"+s*s);
}
public void rectangle(int l,int b)
{
System.out.println("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(System.in);
System.out.println("Enter the radius of circle:");
r=Float.parseFloat(d.readLine());
System.out.println("Enter the side of square:");
s=Integer.parseInt(d.readLine());
System.out.println("Enter the length and breadth of rectangle:");
l=Integer.parseInt(d.readLine());
b=Integer.parseInt(d.readLine());
ob.circle(r);
ob.square(s);
ob.rectangle(l,b);
}
}
6
OUTPUT
D:\java\javac inter.java
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 java.io.*;
class exception
{
public static void main(String arg[])
{
int a,b,res;
DataInputStream d=new DataInputStream(System.in);
try
{
System.out.println("Enter the first number:");
a=Integer.parseInt(d.readLine());
System.out.println("Enter the second number:");
b=Integer.parseInt(d.readLine());
res=a/b;
System.out.println("Result:"+res);
}
catch(ArithmeticException e1)
{
System.out.println("Division by zero");
}
catch(NumberFormatException e2)
{
System.out.println("you must enter integer value");
}
catch(Exception e3)
{
System.out.println("Something is wrong");
}
}
}
8
OUTPUT
D:\java\javac exception.java
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
OUTPUT
D:\java\javac App.java
D:\java\appletviewer App.java
10
5. Write a program to create a simple GUI calculator.
11
}
else if(ae.getSource()==b3)
{
c=a*b;
t3.setText(c+"");
}
else if(ae.getSource()==b4)
{
c=a/b;
t3.setText(c+"");
}
}
}
OUTPUT
javac calculator.java
appletviewer calculator.java
12
6. Write a program demonstrate thread.
import java.io.*;
class mythread implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println("i="+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Error");
}
}
}
class threadex
{
public static void main(String arg[])
{
mythread ob=new mythread();
Thread t=new Thread(ob);
t.start();
}
}
OUTPUT
D:\java\javac threadex.java
D:\java\java threadex
i=1
i=2
i=3
i=4
i=5
13
7. Write a program implement multi threading
import java.io.*;
class one extends Thread
{
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
System.out.println("Good Morning");
}
}
catch(InterruptedException e)
{
}
}
}
class two extends Thread
{
public void run()
{
try
{
while(true)
{
Thread.sleep(2000);
System.out.println("Hello");
}
}
catch(InterruptedException e)
{
}
}
14
OUTPUT
D:\java\javac multithread.java
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:array.java
package p1;
import java.io.*;
public class array
{
int x[]=new int[20];
int n,i;
public void read()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter the limit");
n=Integer.parseInt(d.readLine());
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
x[i]=Integer.parseInt(d.readLine());
}
}
public void show()
{
System.out.println("Entered elements:");
for(i=0;i<n;i++)
{
System.out.println(x[i]);
}
}
}
16
//main file:mainp.java
import java.io.*;
import p1.*;
class mainp
{
public static void main(String arg[])throws IOException
{
array ob=new array();
ob.read();
ob.show();
}
}
OUTPUT
D:\java\p1\javac array.java
D:\java\:javac mainp.java
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 java.io.*;
class A
{
void fact()throws IOException
{
System.out.println("I am base class Function");
}
}
class B extends A
{
void fact()throws IOException
{
int n,f=1,i;
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a number:");
n=Integer.parseInt(d.readLine());
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial is"+f);
}
}
class override
{
public static void main(String arg[])throws IOException
{
B ob=new B();
ob.fact();
}
}
OUTPUT
D:\java\javac override.java
D:\java\java override
Enter a number:
8
Factorial is40320
18
10.Write a program implement abstract class.
import java.io.*;
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;
}
System.out.println("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(System.in);
System.out.println("Enter a number:");
n=Integer.parseInt(d.readLine());
ob.rev(n);
}
}
OUTPUT
D:java\javac Abstr.java
D:\java\java Abstr
Enter a number:
7864
Reverse of a number is4687
19
11.Write a program to draw different figures.
20
}
else if(f==3)
{
g.drawLine(100,100,250,250);
}
}
}
OUTPUT
21
12. Write a program implement JDBC
import java.io.*;
import java.sql.*;
class DB
{
public static void main(String arg[])
{
try
{
int r,m;
String n;
DataInputStream d= new DatalnputStream(System.in);
System.out.println("Enter rollno, name and mark");
r=Integer.parseInt(d.readLine());
n=d.readLine();
m-Integer.parseInt(d.readLine());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsname"," "," ");
Statement st=con.createStatement();
st.execute("insert into student values("+r+","+n+", "+m+")");
System.out.println("Record inserted...");
ResultSet rs=st.executeQuery("select * from student");
System.out.println("Rollno\tName\tMark");
while(rs.next())
r=rs.getInt("rollno");
n=rs.getString("name");
m=rs.getInt("mark");
System.out.println(r + "\t" + n + "\t" + m);
}
st.close();
con.close();
}
22
OUTPUT
D:\java>javac DB.java
D:\java>java DB
Enter rollno,name and mark
105
Amina
34
Record inserted….
23