Java - Lab - Manual - Sit
Java - Lab - Manual - Sit
OF
JAVA LABORATORY
(5CSL02)
for
V Semester,
Department of CSE
Siddaganga Institute of Technology
Tumkur 572103
1. Classes and objects
a. Write a program in Java with class Rectangle with the data fields
width, length, area and color. The length, width, area are of
double type and color is of string type. The methods are set_length
(), set_width (), set_color (), and find_area (). Create two objects of
Rectangle and compare their area and color. If area and color
both are the same for the objects then display “Matching
Rectangles”, otherwise display “Non Matching Rectangles”.
PROGRAM:
import java.util.Scanner;
class Rectangle
{
double width, length, area;
String color;
void set_length ()
{
Scanner ob= new Scanner(System.in);
System.out.println("\n Enter the Length:");
length=ob.nextDouble();
}
void set_width ()
{
Scanner ob= new Scanner(System.in);
System.out.println("\n Enter the Width:");
width=ob.nextDouble();
}
void set_color ()
{
Scanner ob= new Scanner(System.in);
System.out.println("\n Enter the Color:");
color=ob. next ();
}
double find_area ()
{
area=length*width;
return area;
}
void compare (Rectangle ob)
{
if(area==ob.area && color.equals(ob.color))
System.out.println("Matching Rectangle\n");
else
System.out.println("Non Matching Rectangles\n");
}
}
public class rect
{
public static void main (String args [])
{
double area;
Rectangle r1=new Rectangle ();
Rectangle r2=new Rectangle ();
r1. compare(r2);
}
}
STEPS FOR EXECUTION:
1. Go to your workspace, Right-click->Open terminal here
2. Create a file named rect.java (vi rect.java)
3. Type the program given above and save it (esc+Shift+: wq!)
4. Compile the program using the command javac rect.java
5. Run the program using the command java rect
OUTPUT:
Enter the 1st Rectangle dimension
Red
Red
PROGRAM:
class Shape
{
double lnt, bdt, a;
Shape (double l)
{
lnt=l;
}
Shape (double l, double b)
{
lnt=l;
bdt=b;
}
s1.area(s1.lnt);
s2. area (s2.lnt, s2.bdt);
OUTPUT:
Area of Rectangle (only length is given) =5000.0
PROGRAM:
class Player
String name;
name=n;
age=a;
matches=m;
ranking=r;
super(n,a,m,r);
hscore=hs;
bataverage=ba;
baverage=balla;
void disp ()
System.out.println("Name: "+name);
System.out.println("Age: "+age);
Football_Player (String n,int a,int m,int r,int g,int gaverage, int passeff)
super(n,a,m,r);
goals=g;
gavg=gaverage;
pass=passeff;
void disp ()
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("No. of Matches: "+matches+"\n");
Hockey_Player (String n,int a,int m,int r,int g,int gaverage, int passeff)
super(n,a,m,r);
goals=g;
gavg=gaverage;
pass=passeff;
void disp()
System.out.println("Name: "+name);
System.out.println("Age: "+age);
}
public class Inheritance
C.disp ();
F. disp ();
H. disp ();
OUTPUT:
Name: Sachin Tendulkar
Age: 38
No of Matches: 600
Batting Average: 55
Bowling Average: 60
Player Ranking: 8
Age=32
No of Matches: 120
No of Goals: 3
Goals Average: 80
Passing Efficiency:94%
Player Ranking: 90
Age=32
No of Matches: 120
No of Goals: 3
Goals Average: 80
Passing Efficiency:94%
Player Ranking: 90
ii) Consider the trunk calls of a telephone exchange. A trunk call can be
ordinary, urgent or lightning. The charges depend on the duration and the
type of the call. Write a program using the concept of polymorphism in
Java to calculate the charges
PROGRAM:
class TrunkCall
{
int duration;
TrunkCall (int sec)
{
duration=sec;
}
double charge ()
{
System.out.println("Charge is undefined");
return 0;
}
}
OUTPUT:
Charges for Ordinary call=50
PROGRAM:
OUTPUT:
Principal Amount: 5000.0Rs
Time: 2Years
Current Balance: 1200.0Rs
ii. Create the dynamic stack by implementing the interfaces that
defines Push() and Pop() methods.
PROGRAM:
interface instack
{
void push (int item);
int pop ();
}
class dstack implements instack
{
private int stk [];
private int tos;
OUTPUT:
Elements in stack1 -> 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Elements in stack2 -> 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
4.Exception handling
On a single track two vehicles are running. As vehicles are going on same
direction there is no problem. If the vehicles are running in different
direction, there is a chance of collision. To avoid collision, write a java
program using Exception handling.
PROGRAM:
class collision
{
String i, j;
void check ()
{
try
{
if(i==j)
{
System.out.println("The two vehicles are moving in same
direction, hence no problem");
}
else
{
throw new Exception ("The two vehicles are moving in
different directions, so collision occurs");
}
} catch (Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
PROGRAM:
class NewThread
{ Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try
Thread.sleep(500);
catch(InterruptedException e)
m1.t.join();
m2.t.join();
m3.t.join();
m4.t.join();
m5.t.join();
catch(InterruptedException e)
System.out.println();
System.out.println("priority of one:"+m1.t.getPriority());
System.out.println("priority of two:"+m2.t.getPriority());
System.out.println("priority of three:"+m3.t.getPriority());
System.out.println("priority of four:"+m4.t.getPriority());
System.out.println("priority of five:"+m5.t.getPriority());
System.out.println();
String name;
Thread t;
MulThread(String n,int p)
name=n;
t.setPriority(p);
System.out.println(name+" started");
t.start();
try
if((t.getPriority()==9)||(t.getPriority()==10))
Thread.sleep(1000);
}
for(int i=0;i<5;i++)
System.out.println(name+":"+i);
Thread.sleep(500);
catch(InterruptedException e)
last=name;
System.out.println(name+" exiting");
PROGRAM:
class Q
int n;
boolean valueSet=false;
while(!valueSet)
try
Thread.sleep(1000);
wait();
catch(InterruptedException e)
System.out.println("Got:"+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
while(valueSet)
try
Thread.sleep(1000);
wait();
catch(InterruptedException e)
this.n=n;
valueSet=true;
System.out.println("Put :"+n);
notify();
Q q;
prod(){}
prod(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
int i=0;
while(true)
q.put(i++);
Q q;
cons(){}
cons(Q q)
this.q=q;
new Thread(this,"Consumer").start();
while(true)
{
q.get();
Q q=new Q();
new prod(q);
new cons(q);
PROGRAM :
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
</applet>
*/
JPanel cardPanel;
JPanel firstp,secondp,thirdp,fourthp;
JPanel buttonp;
JButton first,second,third;
CardLayout ourLayout;
cardPanel=new JPanel();
ourLayout=new CardLayout();
cardPanel.setLayout(ourLayout);
firstp=new JPanel();
firstp.setBackground(Color.blue);
secondp=new JPanel();
secondp.setBackground(Color.yellow);
thirdp=new JPanel();
thirdp.setBackground(Color.green);
fourthp=new JPanel();
first=new JButton("blue");
first.addActionListener(this);
second=new JButton("yellow");
second.addActionListener(this);
third=new JButton("green");
third.addActionListener(this);
buttonp=new JPanel();
buttonp.add(first);
buttonp.add(second);
buttonp.add(third);
this.setLayout(new BorderLayout());
this.add(buttonp,BorderLayout.SOUTH);
this.add(cardPanel,BorderLayout.CENTER);
cardPanel.add(fourthp,"Fourth");
cardPanel.add(firstp,"First");
cardPanel.add(secondp,"Second");
cardPanel.add(thirdp,"Third");
if(e.getSource()==first)
ourLayout.show(cardPanel,"First");
if(e.getSource()==second)
ourLayout.show(cardPanel,"Second");
if(e.getSource()==third)
ourLayout.show(cardPanel,"Third");
PROGRAM :
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
String msg="";
int mouseX=0,mouseY=0;
addMouseListener(this);
addMouseMotionListener(this);
mouseX=0;
mouseY=10;
msg="Mouse Clicked";
repaint();
mouseX=0;
mouseY=10;
msg="Mouse Entered";
repaint();
mouseX=0;
mouseY=10;
msg="Mouse exited";
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="Down";
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="Up";
repaint();
mouseX=me.getX();
mouseY=me.getY();
msg="*";
repaint();
g.drawString(msg,mouseX,mouseY);
PROGRAM :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
response.setContentType("text/html");
name=request.getParameter("uname");
addr=request.getParameter("address");
PrintWriter out=response.getWriter();
out.println("<html><body-bgcolor='#ffffff' text='#000000'>");
out.close();
}
HTML FILE:
<html>
<head>
<title>Greeting...</title>
</head>
<body-bgcolor='#ffffff' text='#000000'>
<hr>
<table>
<tr>
<td align="right"><b>NAME:</b></td>
</tr>
<tr>
<td><b>ADDRESS:</b></td>
</tr>
</table>
<hr>
</form>
</body>
</html>
OUTPUT:
2. Program to request server information viz Request Method URL,
Protocol and Remote address
PROGRAM :
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html><head>");
out.println("<title>Server Information</title>");
out.println("</head>");
out.println("<body bgcolor='#ffffff',text='#000000'>");
out.println("<hr><br><center><table border=5><tr>");
out.println("<td><b>Request Method</b></td>");
out.println("<td>");
out.println(request.getMethod());
out.println("</td></tr>");
out.println("<tr>");
out.println("<td><b> URL</b></td>");
out.println("<td>");
out.println(request.getRequestURL());
out.println("</td></tr>");
out.println("<tr>");
out.println("<td><b>Protocol </b></td>");
out.println("<td>");
out.println(request.getProtocol());
out.println("</td></tr>");
out.println("<tr>");
out.println("<td><b>Remote Address<b></td>");
out.println("<td>");
out.println(request.getRemoteAddr());
out.println("</td></tr></table>");
out.println("<br><hr>");
out.println("</body></html>");
OUTPUT:
8. SWINGS AND JDBC
Write a Java program to implement Client Server interaction (Client
requests a file, Server responds to client with contents of that file which is
then displayed on the screen by Client) –Socket programming
PROGRAM :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton SUMBIT;
JPanel panel;
JLabel label1,label2;
login()
label1=new JLabel();
label1.setText("username");
text1=new JTextField(15);
label2=new JLabel();
label2.setText("password");
text2=new JTextField(15);
SUMBIT=new JButton("SUMBIT");
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUMBIT);
add(panel,BorderLayout.CENTER);
SUMBIT.addActionListener(this);
setTitle("login form");
String value1=text1.getText();
String value2=text2.getText();
java.sql.Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/sit?
user=root&password=");
catch(ClassNotFoundException e)
System.out.println("errorindriverloader"+e);
System.exit(1);
catch(Exception e)
System.out.println("error inconnction"+e);
System.exit(0);
System.out.println("connection established");
try
{
java.sql.Statement s=conn.createStatement();
java.sql.ResultSet r=s.executeQuery(query);
r.next();
int x=r.getRow();
if (x>0)
{
JOptionPane.showMessageDialog(null,"HELLOOOOO"); }
else
{
JOptionPane.showMessageDialog(this,"incoreect login of
password","error",JOptionPane.ERROR_MESSAGE); }
} catch(Exception e)
System.out.println(e);
System.exit(0); }
}
class Demo
try
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
9.Create table using: create table table1(uname char (30), password char (20);
10.Insert data into table using: insert into table1 values(‘abc’,’pass1’); repeat this
step to enter several data
OUTPUT:
9.NETWORKING
Write a Java Program to implement Client Server interaction.
Program:
DailyAdviceServer.java:
import java.io.*;
import java.net.*;
try {
while (true)
writer.println(advice);
writer.close();
System.out.println(advice);
ex.printStackTrace();
}
}
return adviceList[random];
server.go();
}}
DailyAdviceClient.java:
import java.io.*;
import java.net.*;
try {
reader.close();
ex.printStackTrace();
client.go();
DailyAdviceServer:
DailyAdviceClient: