Java Record - 2023-24
Java Record - 2023-24
Java Record - 2023-24
INDO-AMERICAN COLLEGE
CHEYYAR-604 407
2023-2024
Reg. No : ……………………………………………
Name : ……………………………………………
Class : ……………………………………………
Subject : ……………………………………………..
INDO-AMERICAN COLLEGE
CHEYYAR-604 407
Examiners
Place : 1.
Date: 2.
LIST OF PRACTICALS
1
Implementation of Classes and Objects
4
Implementation of Flow, Border ,Grid
8
Implementation of Exception Handling
9
Implementation of Multi Threading
10
Implementation of I/O Streams
import java.util.*;
class AreaOfCircle {
private float radius = 0.0f;
private float area = 0.0f;
private float perimeter = 0.0f;
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:
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:
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:
Result:
}
}
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:
//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 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;
}
if(ch[i]!='B')
{
if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
win();
i=i+2;
}
else
i=i+2;
}
}
public void mouseReleased(MouseEvent e)
{
System.out.print("");
}
{
System.out.print("");
}
public void mouseExited(MouseEvent e)
{
System.out.print("");
}
public void mousePressed(MouseEvent e)
{
System.out.print("");
}
Result:
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"))
{
if(ae.getActionCommand().equals("Exit"))
{
System.exit(0);
}
}
}
OUTPUT:
Result:
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);
}
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));
}
}
Result:
import java.util.Scanner;
class FourthExercise
{
public static void main(String[] args)
{
int num;
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:
Result:
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:
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:
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");
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Result:
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.*;
AdderRemote()throws RemoteException{
super();
}
import java.rmi.*;
public interface Adder extends Remote{
import java.rmi.*;
import java.rmi.registry.*;
}catch(Exception e){System.out.println(e);}
}
}
/*For running this rmi example,
javac *.java
rmic AdderRemote
rmiregistry 5000
java MyServer
java MyClient
*/
Output
Result
package geeks;
public Student()
this.id = id;
return id;
{
this.name = name;
return name;
package geeks;
System.out.println(s.getName());
}
OUTPUT:
GFG
Result