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

Java Record2

Uploaded by

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

Java Record2

Uploaded by

naseefak319
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

SIR SYED INSTITUTE FOR TECHNICAL STUDIES

TALIPARAMBA
( Affiliated to Kannur University )

DEPARTMENT OF COMPUTER SCIENCE

PRACTICAL RECORD

JAVA PROGRAMMING

B.Sc.COMPUTER 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

B.Sc.COMPUTER 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 Reg.No:……………………………………………………..

External Examiner :
1.
2.

2
SL.NO DATE PROGRAM PAGE.NO

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

//<applet code="App.class" width="900" height="500">


//</applet>
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class App extends Applet
{
public void init()
{
JOptionPane.showMessageDialog(null,"Applet initialized..");
}
public void start()
{
JOptionPane.showMessageDialog(null,"Applet started..");
}
public void paint(Graphics g)
{
g.drawString("Applet running...",200,100);
}
public void stop()
{
JOptionPane.showMessageDialog(null,"Applet stopped..");
}
public void destroy()
{
JOptionPane.showMessageDialog(null,"Applet destroyed...");
}
}

OUTPUT

D:\java\javac App.java
D:\java\appletviewer App.java

10
5. Write a program to create a simple GUI calculator.

//<applet code="calculator.class" width=1000 height=500>


//</applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
float a,b,c;
a=Float.parseFloat(t1.getText());
b=Float.parseFloat(t2.getText());
if(ae.getSource()==b1)
{
c=a+b;
t3.setText(c+"");
}
else if(ae.getSource()==b2)
{
c=a-b;
t3.setText(c+"");

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.

//<applet code="figure.class" width="800" height="500">


//</applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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);
rect.addItemListener(this);
circle.addItemListener(this);
line.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==rect)
{
f=1;
repaint();
}
else if(ie.getSource()==circle)
{
f=2;
repaint();
}
else if(ie.getSource()==line)
{
f=3;
repaint();
}
}
public void paint(Graphics g)
{
if(f==1)
{
g.drawRect(100,100,200,100);
}
else if(f==2)
{
g.drawOval(100,100,100,100);

20
}
else if(f==3)
{
g.drawLine(100,100,250,250);
}
}
}

OUTPUT

D:.\java\ javac figure.java


D;\java\appletviewer figure.java

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

Rollno Name Mark


101 Muthu 25
102 Kamala 36
105 Amina 34

23

You might also like