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

Java Record - 2023-24

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

THIRUVALLUVAR UNIVERSITY

INDO-AMERICAN COLLEGE

CHEYYAR-604 407

DEPARTMENT OF COMPUTER SCIENCE

2023-2024

Reg. No : ……………………………………………

Name : ……………………………………………

Class : ……………………………………………

Subject : ……………………………………………..
INDO-AMERICAN COLLEGE
CHEYYAR-604 407

DEPARTMENT OF COMPUTER SCIENCE

Certified to be the bonafide record of practical work done by


With register number in this department during the Academic year
2023-2024

Staff–in-charge Head of the Department

Submitted by .Sc (CS) degree practical examination held on


Indo-American college, Cheyyar -604 407.

Examiners

Place : 1.

Date: 2.
LIST OF PRACTICALS

S.NO DATE TITLE OF EXPERIMENT PAGE NO SIGNATURE

1
Implementation of Classes and Objects

2 Implementation of Inheritance and


Polymorphism

3 Implementation of Interface and Packages

4
Implementation of Flow, Border ,Grid

5 Implementation of Tic-Tac Toe Application


Using Applets
6
Implementation of Frames, Menus, Dialog

7 Implementation of Swing concepts

8
Implementation of Exception Handling

9
Implementation of Multi Threading

10
Implementation of I/O Streams

11 Implementation of Java Networking


concepts

12 Implementation of Java Servlets


( Connecting Database)
13 Implementation of RMI

14 Implementation of Java Beans


Classes and Objects
CODING:

/* Java program to create class to calculate


area and perimeter of circle. */

import java.util.*;

class AreaOfCircle {
private float radius = 0.0f;
private float area = 0.0f;
private float perimeter = 0.0f;

//function to read radius


public void readRadius() {
//Scanner class - to read value from keyboard
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius:");
radius = sc.nextFloat(); //to read float value from keyboard
}

//funtction to calculate area


//return value - will return calculated area

public float getArea() {


area = (float)Math.PI*radius*radius;
return area;
}

//funtction to calculate perimeter


//return value - will return calculated perimeter
public float getPerimeter() {
perimeter = 2 * (float)Math.PI * radius;
return perimeter;
}
}
public class circle {
public static void main(String[] s) {
AreaOfCircle area = new AreaOfCircle();

area.readRadius();
System.out.println("Area of circle:" + area.getArea());
System.out.println("Perimeter of circle:" + area.getPerimeter());
}
}
//Compile C:\jdk1.8\bin>javac circle.java
//Run C:\jdk1.8\bin>java circle
OUTPUT:

Enter radius:15.50

Area of circle:754.385

Perimeter of circle:97.340004

Result:

Thus the above Program has been successfully executed.


Inheritance and Polymorphism
CODING:

//Runtime polymorphism with multilevel inheritance.

class Shape
{
void draw()
{
System.out.println("Drawing...");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing rectangle...");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("Drawing circle...");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Drawing triangle...");
}
}

class TestPolymorphism2
{
public static void main(String args[])
{
Shape s;
s=new Rectangle();
s.draw();
s=new Circle();
s.draw();
s=new Triangle();
s.draw();
}
}
OUTPUT:
Drawing rectangle…
Drawing circle…
Drawing triangle…

Result:

Thus the above Program has been successfully executed.


Interfaces and Package
CODING:

import java.util.*;
interface Exam
{
void percent_cal();
}
class Student
{
String name;
int rno,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
rno=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+rno);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage: "+percent+"%");
}
void display()
{
super.display();
}
}
class lab3
{
public static void main(String args[])
{
Random ran = new Random();
int n = ran.nextInt();
Result R = new Result("Nidharshana",1,93,84);
R.display();
R.percent_cal();
}
}
OUTPUT:

Name of Student: Nidharshana


Roll No. of Student: 1
Marks of Subject 1: 93
Marks of Subject 2: 84
Percentage: 88.0%

Result:

Thus the above Program has been successfully executed.


Flow, Border, Grid Layouts
CODING:
import java.awt.*;
import java.awt.event.*;
class PanelExample extends Frame
{
Panel panel1,panel2,panel3,panel4 ;
PanelExample()
{
setLayout(new GridLayout(2,2,10,10));
panel1 = new Panel();
Button bt1 = new Button("1");
Button bt2 = new Button("2");
Button bt3 = new Button("3");
Button bt4 = new Button("4");
Button bt5 = new Button("5");
panel1.setLayout(new BorderLayout());
panel1.add(bt1,"North");
panel1.add(bt2,"South");
panel1.add(bt3,"East");
panel1.add(bt4,"West");
panel1.add(bt5,"Center");
panel1.setBackground(Color.gray);
add(panel1);
panel2=new Panel();
panel2.setLayout(new FlowLayout());
Button bt6 = new Button("6");
Button bt7 = new Button("7");
Button bt8 = new Button("8");
panel2.add(bt6);
panel2.add(bt7);
panel2.add(bt8);
panel2.setBackground(Color.cyan);
add(panel2);
panel3 = new Panel();
panel3.setBackground(Color.green);
add(panel3);
panel4 = new Panel();
panel4.setBackground(Color.red);
add(panel4);

}
}
class lab4
{
public static void main(String[] args)
{
PanelExample frame = new PanelExample();
frame.setTitle("B.Sc,. Computer Science");
frame.setSize(300,300);
frame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
} );
frame.pack();
frame.setVisible(true);
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Tic-Tac Toe Application Using Applets
CODING:

//Following Code extends Applet and Allows the users to play the TicTacToe Game.

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class TicTacToe extends Applet implements MouseListener


{
Frame f;
int flag=2,n,m,i=0;
char ch[]=new char[9];

public TicTacToe()
{
f=new Frame("Tic Tac Toe");
f.setLayout(null);
f.setVisible(true);
f.setSize(600,600);
f.addMouseListener(this);
for(i=0;i<9;i++)
ch[i]='B';
}
public void mouseClicked(MouseEvent e)
{
Graphics g=f.getGraphics();
g.drawLine(200,0,200,600);
g.drawLine(400,0,400,600);
g.drawLine(0,200,600,200);
g.drawLine(0,400,600,400);
flag--;
int x=e.getX();
int y=e.getY();
if(flag==1)
{
if(x<200&&y<200){m=0;n=0;ch[0]='R';}
if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='R';}
if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='R';}
if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='R';}
if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='R';}
if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='R';}
if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='R';}
if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='R';}
if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='R';}
g.setColor(Color.red);
g.drawLine(m,n,m+199,n+199);
g.drawLine(m+199,n,m,n+199);
}

if(flag==0)
{

if(x<200&&y<200){m=0;n=20;ch[0]='P';}
if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='P';}
if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='P';}
if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='P';}
if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='P';}
if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='P';}
if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='P';}
if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='P';}
if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='P';}
g.setColor(Color.green);
g.drawOval(m+10,n+10,169,169);
// g.drawLine(m,n,m+189,n+189);
// g.drawLine(m+199,n,m,n+199);
flag=flag+2;
}

for(i=0;i<9;i++) // for draw


{
if(ch[i]!='B')
{
if(i==8)
draw();
}
else
break;
}

for(i=0;i<3;i++) //for vertical


{
// System.out.print(ch[i]);
if(ch[i]!='B')
{
if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))
win();
}
}
for(i=0;i<7;i++) //for horizontal
{

if(ch[i]!='B')
{
if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
win();
i=i+2;
}
else
i=i+2;
}

if(ch[4]!='B') //for diagonals


{
if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))
win();
}
}
public Frame win()
{
Frame m=new Frame("Result");
Label l=new Label("you win");
m.setLayout(null);
m.add(l);
l.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;
}
public Frame draw()
{
Frame m=new Frame("Result");
Label l1=new Label("Stalemate");
m.setLayout(null);
m.add(l1);
l1.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;

}
public void mouseReleased(MouseEvent e)
{
System.out.print("");
}

public void mouseEntered(MouseEvent e)

{
System.out.print("");
}
public void mouseExited(MouseEvent e)
{
System.out.print("");
}
public void mousePressed(MouseEvent e)
{
System.out.print("");
}

public static void main(String args [])


{
new TicTacToe();
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Frames, Menus, Dialog
CODING:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ThirdExercise implements ActionListener
{
Frame frame;
MenuBar menuBar;
Menu menu1, menu2;
MenuItem mItem1, mItem2, mItem3, mItem4, mItem5;
FileDialog fg;
Label label1;
ThirdExercise()
{
frame = new Frame("JDialog");
menuBar= new MenuBar();
menu1 = new Menu("File");
mItem1 = new MenuItem("New");
mItem2 = new MenuItem("Open");
mItem3= new MenuItem("Save");
mItem4 = new MenuItem("Exit");
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);
menu1.add(mItem4);
menu2 = new Menu("Save-as");
mItem5 = new MenuItem(".jpeg");
menu2.add(mItem5);
menu1.add(menu2);
menu1.add(mItem4);
menuBar.add(menu1);
mItem2.addActionListener(this);
mItem4.addActionListener(this);
label1 = new Label("", Label.CENTER);
frame.setMenuBar(menuBar);
frame.add(label1,BorderLayout.CENTER);
frame.setSize(370,270);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Open"))
{

fg = new FileDialog(frame, "Open a file");


fg.setVisible(true);
String file = fg.getDirectory()+ fg.getFile();
label1.setText("File to Open - " + file);
}

if(ae.getActionCommand().equals("Exit"))
{
System.exit(0);
}
}

public static void main(String... ar)


{
new ThirdExercise();
}

}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Calculator using Swing
CODING:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class SeventhExercise implements ActionListener


{
JFrame f;
JTextField t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;

static double a=0,b=0,result=0;


static int operator=0;

SeventhExercise()
{
f=new JFrame("Calculator");
t=new JTextField(30);
bdel=new JButton("DEL");
bclr=new JButton("CL");
beq=new JButton("=");
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
f.add(t);
f.add(bdel);
f.add(bclr);
f.add(badd);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(bmul);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(bdiv);
f.add(b8);
f.add(b8);
f.add(b9);
f.add(b0);
f.add(bsub);
f.add(bdec);
f.add(beq);
f.setLayout(new GridLayout(5,4));
f.setVisible(true);
f.setSize(350,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bdel.addActionListener(this);
bclr.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==b1)
t.setText(t.getText().concat("1"));

if(e.getSource()==b2)
t.setText(t.getText().concat("2"));

if(e.getSource()==b3)
t.setText(t.getText().concat("3"));

if(e.getSource()==b4)
t.setText(t.getText().concat("4"));

if(e.getSource()==b5)
t.setText(t.getText().concat("5"));

if(e.getSource()==b6)
t.setText(t.getText().concat("6"));

if(e.getSource()==b7)
t.setText(t.getText().concat("7"));

if(e.getSource()==b8)
t.setText(t.getText().concat("8"));

if(e.getSource()==b9)
t.setText(t.getText().concat("9"));

if(e.getSource()==b0)
t.setText(t.getText().concat("0"));

if(e.getSource()==bdec)
t.setText(t.getText().concat("."));

if(e.getSource()==badd)
{
a=Double.parseDouble(t.getText());
operator=1;
t.setText("");
}

if(e.getSource()==bsub)
{
a=Double.parseDouble(t.getText());
operator=2;
t.setText("");
}

if(e.getSource()==bmul)
{
a=Double.parseDouble(t.getText());
operator=3;
t.setText("");
}

if(e.getSource()==bdiv)
{
a=Double.parseDouble(t.getText());
operator=4;
t.setText("");
}

if(e.getSource()==beq)
{
b=Double.parseDouble(t.getText());

switch(operator)
{
case 1: result=a+b;
break;

case 2: result=a-b;
break;
case 3: result=a*b;
break;

case 4: result=a/b;
break;

default: result=0;
}

t.setText(""+result);
}

if(e.getSource()==bclr)
t.setText("");

if(e.getSource()==bdel)
{
String s=t.getText();
t.setText("");
for(int i=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));
}
}

public static void main(String...s)


{
new SeventhExercise();
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Exception Handling
CODING:

import java.util.Scanner;

class OddNumber extends Exception


{
OddNumber()
{
super("Odd number exception");
}
OddNumber(String msg)
{
super(msg);
}
}

class FourthExercise
{
public static void main(String[] args)
{
int num;

Scanner Sc = new Scanner(System.in);

System.out.print("\n\tEnter any number : ");


num = Integer.parseInt(Sc.nextLine());

try
{
if(num%2 != 0)
throw(new OddNumber());
else
System.out.print("\n\t" + num + " is an even number");
}
catch(OddNumber Ex)
{
System.out.print("\n\tError : " + Ex.getMessage());
}

System.out.print("\n\tEnd of program");
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Multi Threading
CODING:

class MultiThread extends Thread


{
public void run()
{
System.out.println("Running Thread Name: "+ this.currentThread().getName());
System.out.println("Running Thread Priority: "+ this.currentThread().getPriority());
}
}
public class FifthExercise
{
public static void main(String[] args)
{
MultiThread M1 = new MultiThread();
M1.setName("Thread1");
M1.setPriority(Thread.MIN_PRIORITY);

MultiThread M2 = new MultiThread();


M2.setName("Thread2");
M2.setPriority(Thread.MAX_PRIORITY);

MultiThread M3 = new MultiThread();


M3.setName("Thread3");
M1.start();
M2.start();
M3.start();
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Input Output stream File Handling
CODING:

import java.io.*;
import java.util.*;
class SixthExercise
{
public static void main(String []args)throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter few words");
String source= scan.nextLine();
int lines=0,char1=0,c=0,words =0;
boolean temp=true;
String whitespace =" \t\n\r";
OutputStream fos = new FileOutputStream("mno.txt");
byte b[] = source.getBytes();
fos.write(b);
fos.close();
System.out.println("File Created ");
InputStream fis = new FileInputStream("mno.txt");
while((c=fis.read()) != -1)
{
char1 ++;
if(c=='\n')
{
lines ++;
}
int index = whitespace.indexOf(c);
if(index == -1)
{
if(temp==true)
{
++words;
}
temp=false;
}
else
{
temp=true;
}
}
if(char1 != 0)
{
++ lines;
++ lines;
}
System.out.println("Number of lines: "+lines + "\n Number of Characters :" +char1+ "\n
Number of words: " +words);
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Networking concepts
CODING:

TCPServer.java

import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
System.out.println("Client Connected");
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}
catch(Exception e){System.out.println(e);}
}
}

TCPClient.java

import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
OUTPUT:

Result:

Thus the above Program has been successfully executed.


Java Servlets (Connecting Database)
CODING:

import java.io.*;
import java.sql.*;
class EighthExercise
{
public static void main(String args[])
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:bsc","scott","tiger");
Statement st=con.createStatement();
System.out.println("\t\t Connection established...\n");

String qry="create table bsc(sno number,sname varchar(30))";


st.execute(qry);
System.out.println("\t\t Table Create...\n");

qry="insert into bsc values(101,'deepa')";


int rows=st.executeUpdate(qry);
System.out.println("\n\t\t Record inserted sucessfully..."+rows);

qry="insert into bsc values(102,'kanaka')";


rows=st.executeUpdate(qry);
System.out.println("\n\t\t Record inserted sucessfully..."+rows);

qry="update bsc set sname='karthika'where sno=101";


rows=st.executeUpdate(qry);
System.out.println("\n\t\t Record is Update sucessfully..."+rows);

qry="delete from bsc where sno=102";


rows=st.executeUpdate(qry);
System.out.println("\n\t\t Record is Deleted sucessfully..."+rows);

qry="select * from bsc";


ResultSet rs=st.executeQuery(qry);
System.out.println("\t\tThe Records is...\n");
System.out.println("\t\t sno \t\tsname\n");
System.out.println("\t\t ................. \n");
while(rs.next())
{
System.out.print("\t\t"+rs.getInt(1));
System.out.println("\t\t"+rs.getString(2));
}
st.close();
con.close();
}

catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Result:

Thus the above Program has been successfully executed.


RMI (Remote Method Invocation)
CODING:
import java.rmi.*;

public class MyClient{

public static void main(String args[]){


try{

Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));

}catch(Exception e){System.out.println(e);}
}

import java.rmi.*;
import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements Adder{

AdderRemote()throws RemoteException{
super();
}

public int add(int x,int y){return x+y;}

import java.rmi.*;
public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException;


}

import java.rmi.*;
import java.rmi.registry.*;

public class MyServer{

public static void main(String args[]){


try{

Adder stub=new AdderRemote();


Naming.rebind("rmi://localhost:5000/sonoo",stub);

}catch(Exception e){System.out.println(e);}
}

}
/*For running this rmi example,

1) compile all the java files

javac *.java

2)create stub and skeleton object by rmic tool

rmic AdderRemote

3)start rmi registry in one command prompt

rmiregistry 5000

4)start the server in another command prompt

java MyServer

5)start the client application in another command prompt

java MyClient
*/
Output

Result

Thus the above Program has been successfully executed.


Java Beans
CODING:

/ Java Program of JavaBean class

package geeks;

public class Student implements java.io.Serializable

private int id;

private String name;

public Student()

public void setId(int id)

this.id = id;

public int getId()

return id;

public void setName(String name)

{
this.name = name;

public String getName()

return name;

// Java program to access JavaBean class

package geeks;

public class Test {

public static void main(String args[])

Student s = new Student(); // object is created

s.setName("GFG"); // setting value to the object

System.out.println(s.getName());

}
OUTPUT:

GFG

Result

Thus the above Program has been successfully executed.

You might also like