Java Programming Lab
PART-A
1. Write a Java program to demonstrate method overloading.
class overloadDemo
{
int volume(int s)
{
return(s*s*s);
}
float volume (float r, int h)
{
return(3.14f*r*r*h);
}
long volume(long l,int b,int h)
{
return (l*b*h);
}
}
class overload
{
public static void main(String args[])
{
overloadDemo ob=new overloadDemo();
[Link]("volume of cube: " + [Link](10));
[Link]("volume of cylinder: "+ [Link](2.5f,8));
[Link]("volume of rectangular box: " + [Link](100l,75,15));
}}
output:
volume of cube: 1000
volume of cylinder: 157.0
volume of rectangular box: 11250
1
2. Write a Java program to sort a list of numbers.
class Numbersorting
{
public static void main(String args[])
{
int number[]={55,40,80,65,71};
int n=[Link];
[Link]("Given list:");
for(int i=0;i<n;i++)
[Link](" "+ number[i]);
[Link]("\n");
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(number[i]<number[j])
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
[Link]("Sorted list:");
for(int i=0;i<n;i++)
[Link](" " + number[i]);
[Link](" ");
}
}
output:
Given list:
55 40 80 65 71
Sorted list:
80 71 65 55 4
2
3. Write a Java program to demonstrate manipulation of strings.
class stringManipulation
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Object language");
[Link]("Original string: " + str);
//obtaining string length
[Link]("Length of string: " + [Link]());
//Accessing characters in a string
for(int i=0;i<[Link]();i++)
{
int p=i+1;
[Link]("Character at position: " + p + " is " + [Link](i));
}
//Inserting a string in the middle
String astring = new String ([Link]());
int pos= [Link](" language");
[Link](pos," Orient ");
[Link]("Modified string:" + str);
//Modifying characters
[Link](6,'_');
[Link]("String now:" + str);
//Appending a string at the end
[Link](" improves security. ");
[Link]("Appended string :" + str);
}
}
3
4. Write a Java program to demonstrate single inheritance.
class room
{
int length,breadth;
room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class newroom extends room
{
int height;
newroom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class singleinheritance
{
public static void main(String args[])
{
newroom room1=new newroom(14,12,10);
int area1=[Link]();
int volume1=[Link]();
[Link]("area1= " + area1);
[Link]("volume1= " + volume1);
}
}
4
5. Write a Java program to sort the names using vectors.
import [Link].*;
class vectorsort
{
public static void main(String args[])
{
Vector list=new Vector();
[Link](“adcdfd”);
int length=[Link];
for(int i=0;i<length;i++)
[Link](args[i]);
int size=[Link]();
String listArray[]=new String[size];
[Link](listArray);
String temp=null;
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++)
if(listArray[j].compareTo(listArray[i])<0)
{
temp=listArray[i];
listArray[i]=listArray[j];
listArray[j]=temp;
}
[Link]("sorted list is ");
for (int i=0;i<[Link]();i++)
[Link](listArray[i]);
}
}
5
6. Write a Java program to demonstrate Arrayindexoutofbounds and arithmetic Exceptions.
class TryCatch
{
public static void main(String args[])
{
int array[]={0,0};
int num1,num2,result=0;
num1=100;
num2=0;
[Link]("Exceptions caught are:-");
try
{
result=num1/num2;
}
catch(ArithmeticException e)
{
[Link]("Error..... Division by zero");
}
try
{
[Link](num1/array[2]);
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Error..........ArrayIndexOutOfBounds");
}
}
}
6
7. Write a Java program to demonstrate Multiple Threading.
class ThreadDemo implements Runnable
{
Thread t;
ThreadDemo()
{
t=new Thread(this);
[Link]("ChildThread:" );
[Link]();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
[Link]("ChildThread:" + i);
[Link](500);
}
}
catch(InterruptedException e)
{
[Link]("ChildThread Interrupted ");
}
[Link]("Exiting ChildThread");
}
}
class MultipleThread
{
public static void main(String arg[])
{
7
new ThreadDemo();
try
{
for(int i=5;i>0;i--)
{
[Link]("MainThread:" + i);
[Link](1000);
}
}
catch(InterruptedException e)
{
[Link]("MainThread Interrupted");
}
[Link]("Exiting MainThread ");
}
8
8. Write an applet to display the sum of two digits.
/* <applet code="[Link]" width=600 height=200 > </applet> */
import [Link].*;
import [Link].*;
public class UserIn extends Applet
{
TextField text1,text2;
public void init()
{
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
[Link]("0");
[Link]("0");
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
[Link]("Input a number in each box" ,10, 50);
try
{
s1=[Link]();
x=[Link](s1);
s2=[Link]();
y=[Link](s2);
}
catch(Exception ex)
{}
z=x+y;
9
s=[Link](z);
[Link]("The sum is : " ,10,75);
[Link](s,100,75);
}
public boolean action(Event event, Object object)
{
repaint();
return true;
}
}
9. Write a Java program to display the IP address of your working machine.
import [Link].*;
class Inteladd
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress address=[Link]();
[Link](address);
}
}
10
10. Write a Java program to demonstrate free hand writing.
/*<applet code="[Link]" width=200 height=300 ></applet>*/
import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleKey extends Applet implements KeyListener
{
String msg=" ";
int x=10,y=20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("key up");
}
public void keyTyped(KeyEvent ke)
{
msg+=[Link]();
repaint();
}
public void paint(Graphics g)
{
[Link](msg, x, y);
}
}
11
PART-B
1. Write a Java program to draw line, rectangle, circle ,oval and polygon with the help of
java graphic class.
//<applet code="[Link]" width=200 height=200></applet>
import [Link].*;
import [Link].*;
public class Rect extends Applet
{
public void paint(Graphics g)
{
[Link](700,20,800,760);
[Link](30,60,40,50);
[Link]([Link]);
[Link](80,180,200,120);
[Link]([Link]);
[Link](270,30,100,100);
int x[]={500,420,220,500};
int y[]={500,620,20,500};
int n=4;
[Link](x,y,n);
}
}
12
2. Write a Java applet to demonstrate Animation using threads.
/*<applet code="[Link]" width=2222 height=1111>
<param name="img2" value="[Link]">
<param name="img1" value="Water [Link]"></applet>*/
import [Link].*;
import [Link].*;
public class simpleimage extends Applet implements Runnable
{
Image img2;
Image img1;
Thread t;
public void init()
{
img2=getImage(getDocumentBase(), getParameter("img2"));
img1=getImage(getDocumentBase(),getParameter("img1"));
Thread t=new Thread(this);
[Link]();
}
public void paint(Graphics g)
{
try
{
[Link](1000);
}
catch(InterruptedException e)
{
showStatus("thread Interrupted");
}
[Link](img1,10,70,this);
try
{
[Link](10000);
}
catch(InterruptedException e)
{
showStatus("thread Interrupted");
}
[Link](img2,210,110,this);
}
public void run()
{
for(; ;)
repaint();
}
13
}
3 . Write a Java program to demonstrate Access Control using packages.
// To demonstrate access control using Packages.
package package1;
public class ClassA
{
public void displayA()
{
[Link]("ClassA");
}
}
//Note: Store the file [Link] in the folder package1 and compile it.
import package1.*;
class packageTest1
{
public static void main(String args[])
{
ClassA objectA=new ClassA();
[Link]();
}
}
Output:
ClassA
14
package package2;
public class ClassB
{
protected int m=10;
public void displayB()
{
[Link]("ClassB");
[Link]("m=" + m);
}
}
//Store the file [Link] in the folder package2 and compile it.
import [Link];
import package2.*;
class packageTest2
{
public static void main(String args[])
{
ClassA objectA=new ClassA();
ClassB objectB=new ClassB();
[Link]();
[Link]();
}
}
Output:
ClassA
ClassB
m=10
15
4. Write a Java program to display the result of a student using multiple inheritance.
/student result using multiple inheritance.
class student
{
int rollNumber;
void getNumber(int n)
{
rollNumber=n;
}
void putNumber()
{
[Link]("Roll No: " + rollNumber);
}
}
class Test extends student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
[Link]("Marks obtained");
[Link]("part1=" + part1);
[Link]("part2=" + part2);
}
}
interface sports
{
float sportwt=6.0f;
void putwt();
}
class Results extends Test implements sports
{
float total;
public void putwt()
{
[Link](" Sports wt= " + sportwt);
}
void display()
{
total=part1+part2+sportwt;
putNumber();
16
putMarks();
putwt();
[Link]("Total score=" + total);
}
}
class Hybrid
{
public static void main(String args[])
{
Results stud=new Results();
[Link](1234);
[Link](27.5f,33.0f);
[Link]();
}
}
output:
Roll No: 1234
Marks obtained
part1= 27.5
part2= 33.0
sports wt =6.0
Total score = 66.5
17
5. Write a Java program to demonstrate simple calculator with the help of text fields, buttons.
// Applet to demonstrate Simple Calculator
/*<Applet code="[Link]" width=400 height=200></Applet>*/
import [Link].*;
import [Link].*;
import [Link].*;
public class simpleCalculator1 extends Applet
{
TextField text1,text2,text3;
public void init()
{
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(4);
add(text1);
add(text2);
add(text3);
[Link]("0");
[Link]("0");
[Link]("0");
}
public void paint(Graphics g)
{
int x=0,y=0,z=0,i=0;
String s,s1,s2,s3;
[Link]("Input a no in first and second box",10,50);
[Link]("Input your choice in box three:",20,80);
[Link]("[Link] [Link]",20,100);
[Link]("[Link] [Link]",20,30);
try
{
s1=[Link]();
x=[Link](s1);
s2=[Link]();
y=[Link](s2);
s3=[Link]();
i=[Link](s3);
}
catch(Exception e)
{
}
switch(i)
{
case 1:z=x+y;
break;
18
case 2:z=x-y;
break;
case 3:z=x*y;
break;
case 4:z=x/y;
break;
default:[Link]("Invalid Input",40,50);
}
s=[Link](z);
[Link]("The result is",10,205);
[Link](s,100,205);
}
public boolean action(Event event,Object object)
{
repaint();
return true;
}
}
19
6. Write a Java program using I-O streams to count the number of words in a file.
import [Link].*;
class WordCount
{
public static int words=0;
public static void wc(InputStreamReader isr) throws IOException
{
int c=0;
boolean lastwhite=true;
String whitespace=" \n\t\r";
while((c=[Link]())!=-1)
{
int index=[Link](c);
if(index==-1)
{
if(lastwhite==true)
{
++words;
}
lastwhite=false;
}
else
{
lastwhite=true;
}
}
}
public static void main(String args[])
{
try
{
wc(new InputStreamReader([Link]));
}
catch(IOException ee)
{
return;
}
[Link]("no of words="+words);
}
}
20
7. Write a Java program to copy characters from one file into another.
import [Link].*;
class CopyCharacters
{
public static void main(String args[])
{
File infile = new File("[Link]");
File outfile=new File("[Link]");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(infile);
outs=new FileWriter(outfile);
int ch;
while((ch=[Link]())!=-1)
{
[Link](ch);
}
}
catch(IOException e)
{
[Link](e);
[Link](-1);
}
finally
{
try
{
[Link]();
[Link]();
}
catch(IOException ex)
{ }
}
}
}
21
8. Write a Java program to demonstrate Client Server Interaction.
1. Client
import [Link].*;
import [Link].*;
class Client
{
public static void main(String args[])
{
Try {
Socket ci=new Socket("[Link]",5000);
DataInputStream din=new DataInputStream([Link]());
DataOutputStream dout=new DataOutputStream([Link]());
int a=10,b=20,c;
String add="";
[Link](a);
[Link](b);
c=[Link]();
add=[Link](c);
[Link]("addition of a,b is" +add);
[Link]();
}catch(Exception e) {}
}}
2. Server
import [Link].*;
import [Link].*;
class Server
{
public static void main(String args[])
{
try
{
ServerSocket s=new ServerSocket(5000);
Socket ci=[Link]();
DataInputStream din=new DataInputStream([Link]());
DataOutputStream dout=new DataOutputStream([Link]());
int x=[Link]();
int y=[Link]();
int z=x+y;
[Link](z);
[Link]();
[Link]();
}catch(Exception e){}
22
}}
9. Write a Java applet to calculate Area and Circumference of a circle using radio button and
checkbox.
// Java Applet to calculate Area & circumference of a circle
/*<applet code=[Link] width=433 height=323></applet>*/
import [Link].*;
import [Link].*;
import [Link].*;
public class area extends Applet implements ItemListener
{
Checkbox cb1,cb2;
CheckboxGroup cbg;
TextField text1;
float area,radius,circum;
String r= " ";
String a=" ";
String c="";
public void init()
{
cbg=new CheckboxGroup();
cb1=new Checkbox("area");
cb2=new Checkbox("circum",cbg,false);
text1=new TextField(6);
add(text1);
add(cb1);
add(cb2);
[Link]("0");
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent e)
{
try
{
r=[Link]();
radius=[Link](r);
if([Link]()==cb1)
{
area=3.14f*radius*radius;
a=[Link](area);
}
else if([Link]()==cb2)
{
circum=3.14f*radius*2.0f;
23
c=[Link](circum);
}}
catch(Exception ex){ }
repaint();
}
public void paint(Graphics g)
{
[Link]("enter the radius in the textbox",30,20);
[Link]("area:",10,40);
[Link]("circumference:",10,80);
[Link](a,40,40);
[Link](c,150,80);
}}
11. Write a simple database and connect it using JDBC.
//create a simple database and connect it using JDBC
import [Link].*;
public class DBconnect
{
public static void main(String args[])
{
ResultSet rs;
Connection con;
try
{
[Link]("[Link]");
con=[Link]("jdbc:odbc:LoginDetails");
Statement stmt=[Link]();
String selectQuery ="select * from login where userName='anil' ";
rs= [Link](selectQuery );
while([Link]())
{
int uId=[Link]("userId");
String uname=[Link]("userName");
String psw=[Link]("passWord");
int desigId=[Link]("designationId");
[Link]("User id:" + uId);
[Link]("UserName:"+ uname);
[Link]("password:"+psw);
[Link]("designation:"+ desigId);
}}
catch(ClassNotFoundException e)
{
[Link]();
}
catch(SQLException sqle)
{
24
[Link]();
}
25