Java Lab Manual
Java Lab Manual
Semester: III
EXPERIMENT NAME
Display message
Add two numbers
Arithmetic operations
Area of the circle
Check odd or even
Greatest among three numbers
Greatest number using nested if
Sum and average of five marks
Arithmetic operations using switch
Names of 10 natural numbers
Sum of n natural numbers
Factorial of a number
Sum of n numbers
Sum of two numbers using class and object
Prepare marksheet of a student
Find area of rectangle using constructor
Find area of room
Arithmatic operations using static members
Print greatest value using nesting of methods
Find area and volumn of a room using inheritance
Method overriding
Read and print n numbers(array)
Sorting numbers in ascending order
Sum of two matrices
Sorting names in ascending order
Arithmatic operations using package
Implementing interfaces
Managing errors and exceptions
Multithreading
Applet program to draw a house
Applet program to draw a car
1
SRM UNIVERSITY
FACULTY OF ENGINEERING AND TECHNOLOGY
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
LAB MANUAL
Course Code : TE0223
Course Title : Java Programming Lab
Course Time : July Nov. 2012
Ex. No :1
Semester: III
DISPLAY MESSAGE
Ex. No :2
Ex. No :3
ARITHMETIC OPERATIONS
Ex. No :4
Ex. No: 5
Ex. No: 6
Ex. No: 7
AIM : To write a program to find the greatest among 3 nos using nested if.
ALGORITHM:
Step 1: Start
Step 2: Class gtnnestedif
Step 3: Use if and compare a,b and c
Step 4: Print the greatest no
Step 5: Stop
PROGRAM:
import java.io.*;
class gtnnestedif
{
public static void main(String args[])throws IOException
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a,b,c;
System.out.println("Enter the values");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
if(a>b)
{if(a>c)
{System.out.println("A is greatest");}
else
{
System.out.println("C is greatest");}}
else if (b>c)
{
System.out.println("B is greatest");}
else
{System.out.println("C is greatest");}}}
OUTPUT:
D:\JAVA>javac clsgtn.java
D:\JAVA>java clsgtn
Enter the values:
10 20 34
C is greatest
Ex. No: 8
AIM : Program to find calculate sum, average and check if pass or fail.
ALGORITHM:
Step 1: Start
Step 2: Read marks in five subjects
Step 3: Sum=m1+m2+m3+m4+m5
Step 4: Avg = sum/5f
Step 5: Print the sum and average
Step 6: If (m1>40) and (m2>40) and(m3>40) and(m4>40) and(m5>40) then pass else fail
Step 8: Stop
PROGRAM:
import java.io.*;
class mark
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int m1,m2,m3,m4,m5,tot;
float avg;
System.out.println("Enter the mark");
m1=Integer.parseInt(br.readLine());
m2=Integer.parseInt(br.readLine());
m3=Integer.parseInt(br.readLine());
m4=Integer.parseInt(br.readLine());
m5=Integer.parseInt(br.readLine());
tot=m1+m2+m3+m4+m5;avg=tot/5;
System.out.println("Total="+tot);
System.out.println("Average="+avg);
if((m1>=40)&&(m2>=40)&&(m3>=40)&&(m4>+40)&&(m5>=40))
{
System.out.println("Result=Pass");
if (avg>=60){System.out.println("Class=First");}
else if (avg>=50)
{System.out.println("Class=Second");}
else
{System.out.println("Class=Third");
}}
else
{System.out.println("Result=Fail"); }
}
}
OUTPUT:
D:\JAVA>javac mark.java
D:\JAVA>java mark
Enter the mark
80
80
80
80
80
Total=400
Average=80.0
Result=Pass
Class=First
D:\JAVA>
10
Ex. No: 9
11
OUTPUT:
D:\JAVA>javac arith.java
D:\JAVA>java arith
Enter the values
10
10
1.Add
2.Sub
3.Mul
4.Div
Enter your choice
3
Result is 100.0
D:\JAVA>
12
Ex. No: 10
AIM : To write a program to print the name of the entered natural no(1-10)
ALGORITHM:
Step 1: Start
Step 2: Read the value
Step 3: Switch case and print the name of the number
Step 4: Print the desired output
Step 5: Stop
PROGRAM:
import java.io.*;
class natno
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a;
System.out.println("Enter the value");
a=Integer.parseInt(br.readLine());
switch(a)
{ case 1 : System.out.println("One");
break ;
case 2 : System.out.println("Two");
break ;
case 3 : System.out.println("Three");
break ;
case 4 : System.out.println("Four");
break ;
case 5 : System.out.println("Five");
break ;
case 6 : System.out.println("Six");
break ;
case 7 : System.out.println("Seven");
break ;
case 8: System.out.println("Eight");
break ;
case 9 : System.out.println("Nine");
break ;
case 10 : System.out.println("Ten");
break ;
13
14
Ex. No: 11
15
Ex. No: 12
FACTORIAL OF A NUMBER
AIM : To write a program to find the factorial of a number entered by the user.
ALGORITHM
Step 1: Start
Step 2: Read the value of n.
Step 3: for i=1 to n
Step 4: fact=fact*i
Step 5: Display the fact
Step 6: Stop
PROGRAM:
import java.io.*;
class clssum
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n");
int n,i;
int s=1;
n = Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
{
s+=i;
}
System.out.println("Factorial is"+s);
}
}
OUTPUT:
D:\JAVA>javac clsfact.java
D:\JAVA>java clsfact
Enter the value of n
5
Factorial is 120
D:\JAVA>
16
EX. NO: 13
SUM OF N NUMBERS
17
Ex. No: 14
Aim : To write a program to find sum of two numbers using class and object.
ALGORITHM
Step 1: Start
Step 2: Define a class sum
Step 3: Define methods getdata and sum
Step 4: Define clsmain
Step 5: Read the two numbers.
Step 6: Call getdata and sum
Step 7: Print the sum
Step 8: Stop
PROGRAM:
import java.io.*;
class sum
{
int a;
int b;
void getdata(int c,int d)
{
a=c;
b=d;
}
int sum1()
{
return(a+b);
}}
class clsmain
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int d,e;
d=Integer.parseInt(br.readLine());
e=Integer.parseInt(br.readLine());
sum r = new sum();
r.getdata(d,e);
System.out.println("The sum is " + r.sum1());
18
}}
OUTPUT:
D:\JAVA>javac sum.java
D:\JAVA>java clsmain
10
20
The sum is 30
D:\JAVA>
19
Ex. No: 15
20
else
{System.out.println("Result=Fail");}}}
class clsmain
{public static void main(String args[])throws IOException
{marksheet r1 = new marksheet();
r1.getdata();
r1.putdata();}}
OUTPUT:
D:\JAVA>javac marksheet.java
D:\JAVA>java clsmain
Enter the mark
90
85
80
90
100
Name : CHIRAG GUPTA
Subject 1 marks : 90
Subject 2 marks : 85
Subject 3 marks : 80
Subject 4 marks : 90
Subject 5 marks : 100
Total=445
Average=89.0
Result=Pass
Class=First
21
Ex. No: 16
22
23
Ex. No: 17
AREA OF ROOM
24
Ex. No: 18
25
Ex. No: 19
Aim : To write a program to print the greatest value using nesting of methods.
PROGRAM:
import java.io.*;
class clsnesting
{
int m,n;
clsnesting(int x,int y)
{
m=x;
n=y;
}
int largest()
{
return((m>n)?m:n);
}
void display()
{
System.out.println("Largest value = " + largest());
}}
class clsmain
{public static void main (String args[])
{
clsnesting n1=new clsnesting (10,20);
n1.display();}}
OUTPUT:
D:\JAVA>javac clsnesting.java
D:\JAVA>java clsmain
Largest value is 20
26
Ex. No: 20
Aim : To write a program to calculate area and volumn using inheritance of classes
PROGRAM:
import java.io.*;
class clsroom
{
int l;
int b;
clsroom(int x,int y)
{
l=x;
b=y;
}
int area()
{
return(l*b);
}}
class studyroom extends clsroom
{
int h;
studyroom(int x,int y,int z)
{
super(x,y);
h=z;
}
int volumn()
{
return(l*b*h);
}
}
class clshome
{
public static void main (String args[])
{
studyroom s1 = new studyroom(12,10,10);
System.out.println("Area = "+s1.area());
System.out.println("Volumn="+s1.volumn());
}
}
27
OUTPUT:
D:\JAVA>javac clsroom.java
D:\JAVA>java clshome
Area = 120
Volumn = 1200
28
Ex. No: 21
METHOD OVERRIDING
29
Ex. No: 22
30
Ex. No: 23
31
OUTPUT:
D:\JAVA>javac clsarray.java
D:\JAVA>java clsarray
Enter the value of n
3
Enter the values
2
4
3
9
7
After sorting
2
3
4
7
9
32
Ex. No: 24
33
}
}
System.out.println("The third matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(c[i][j]);
}
System.out.print("\n");
}
}
}
OUTPUT:
D:\JAVA>javac clsmatrix.java
D:\JAVA>java clsmatrix
Enter the order of matrix
3
3
Enter the first matrix
111
111
111
Enter the second matrix
222
222
222
The third matrix is
333
333
333
D:\JAVA>
34
Ex. No: 25
35
36
Ex. No: 26
PACKAGE:
package ARI;
public class clspackage
{
public int add(int a,int b)
{
return(a+b);
}
public int sub(int c, int d)
{
return(c-d);
}
public int mul(int e, int f)
{
return(e*f);
}
public int div(int g, int h)
{
return(g/h);
}
}
MAIN PROGRAM:
import java.io.*;
import ARI.*;
class clsmain
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a,b;
System.out.println("Enter the numbers:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
clspackage r = new clspackage();
System.out.println(a + " + " + b + " = " +r.add(a,b));
37
38
Ex. No: 27
IMPLEMENTING INTERFACES
39
System.out.println("Sports wt :"+sportwt);
}
void display()
{
tot = m1+ m2 + sportwt;
putno();
putmarks();
putwt();
System.out.println("Total: " +tot);
}}
class clsmultiple
{public static void main(String args[])
{
Results r = new Results();
r.getno(1001);
r.getmarks(79f,95f);
r.display();
}}
OUTPUT:
D:\JAVA>javac clsmultiple.java
D:\JAVA>java clsmultiple
RegNo:1001
M1 :79.0
M2 :95.0
Sports wt :6.0
Total: 180.0
D:\JAVA>
40
Ex. No: 28
41
Ex. No: 29
MULTITHREADING
42
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}
OUTPUT:
D:\JAVA>javac clsthread.java
D:\JAVA>java clsthread
i=1
j=1
k=1
i=2
j=2
k=2
i=3
j=3
k=3
i=4
j=4
k=4
i=5
j=5
k=5
D:\JAVA>
43
Ex. No: 30
Aim : To write a program to use applet function to draw a shape of the house.
PROGRAM:
import java.awt.*;
import java.applet.*;
/*<applet code="appskel" width=300 height=100>
</applet>
*/
public class appskel extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,100,50,50);
g.drawLine(50,50,100,100);
g.drawLine(100,100,100,200);
g.drawLine(100,200,0,200);
g.drawLine(0,200,0,100);
g.drawLine(50,50,200,50);
g.setColor(Color.blue);
g.drawLine(200,50,250,100);
g.drawLine(250,100,250,200);
g.drawLine(250,200,100,200);
g.drawLine(250,100,0,100);
}
}
OUTPUT:
D:\JAVA>javac appskel.java
D:\JAVA>appletviewer appskel.java
44
Ex. No: 31
Aim : To write a program to use applet function to draw a shape of the CAR.
PROGRAM:
import java.awt.*;
import java.applet.*;
/*<applet code="appskul" width=400 height=300>
</applet>
*/
public class appskul extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(100,40,300,40);
g.drawLine(100,40,100,100);
g.drawLine(200,40,200,100);
g.drawLine(300,40,300,100);
g.drawLine(100,100,300,100);
g.drawLine(100,100,0,150);
g.drawLine(0,150,0,200);
g.drawLine(0,200,350,200);
g.drawLine(350,200,350,150);
g.drawLine(350,150,300,100);
g.drawArc(75,200,50,50,0,360);
g.drawArc(275,200,50,50,0,360);
g.setColor(Color.cyan);
g.fillArc(75,200,50,50,0,360);
g.fillArc(275,200,50,50,0,360);
}
}
OUTPUT:
D:\JAVA>javac appskul.java
D:\JAVA>appletviewer appskul.java
45