Java Lab Manual1 PDF
Java Lab Manual1 PDF
BRANCH : ………………………………………………………………………………
BATCH :………………………………………………………………………………
Sl. No. PART A – List of problems for which student should develop program and execute in the
Laboratory
Aim: Introduce the java fundamentals, data types, operators in java
1
Program: Write a java program that prints all real solutions to the quadratic equation
ax2+bx+c=0. Read in a, b, c and use the quadratic formula.
Aim: Demonstrating creation of java classes, objects, constructors, declaration andinitialization
of variables.
Program: Create a Java class called Student with the following details as variables within it.USN
Name Branch
2
Phone
Write a Java program to create n Student objects and print the USN, Name, Branch, andPhone
of these objects with suitable headings.
3 Program:
A. Write a program to check prime number
B. Write a program for Arithmetic calculator using switch case menu
Aim: Demonstrate the core object-oriented concept of Inheritance, polymorphism
4 Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend thisclass
by writing three subclasses namely Teaching (domain, publications), Technical (skills), and
Contract (period). Write a Java program to read and display at least 3 staff
objects of all three categories.
Aim: Introduce concepts of method overloading, constructor overloading, overriding.
5
Program: Write a java program demonstrating Method overloading and Constructor
overloading.
6 Program: Develop a java application to implement currency converter (Dollar to INR, EURO
to INR, Yen to INR and vice versa), distance converter (meter to KM, miles to KM and viceversa),
time converter (hours to minutes, seconds and vice versa) using packages.
7 Aim: Introduction to abstract classes, abstract methods, and Interface in java
Program: Write a program to generate the resume. Create 2 Java classes Teacher (data:
personal information, qualification, experience, achievements) and Student (data: personal
information, result, discipline) which implements the java interface Resume with the
method biodata().
Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threaded programming.
8 Program: Write a Java program that implements a multi-thread application that has three
threads. First thread generates a random integer for every 1 second; second thread
computes the square of the number and prints; third thread will print the value of cube ofthe
number.
Aim: Introduce java Collections.
9 Program: Write a program to perform string operations using ArrayList. Write functions for
the following a. Append - add at end b. Insert – add at particular index c. Search d. List allstring
starts with given letter.
Aim: Exception handling in java, introduction to throwable class, throw, throws, finally.
10
Program: Write a Java program to read two integers a and b. Compute a/b and print, whenb
is not zero. Raise an exception when b is equal to zero.
Aim: Introduce File operations in java.
11 Program:
Write a java program that reads a file name from the user, displays information about whether
the file exists, whether the file is readable, or writable, the type of file and the
length of the file in bytes
Aim: Introduce java Applet, awt, swings.
12 Programs:
Develop an applet that displays a simple message in center of the screen.Develop a
simple calculator using Swings.
PART B – Practical Based Learning
A problem statement for each batch is to be generated in consultation with the co-examiner
01 and student should develop an algorithm, program and execute the program for the given
problem with appropriate outputs.
Program -1
Write a java program that prints all real solutions to the quadratic equationax2+bx+c=0. Read in a, b, c and
use the quadratic formula.
…................................................................................................................
import java.util.Scanner;
class s1
{
public static void main (String args[])
{
System.out.println("Enter the cofficients a,b,c of quadratic equation ax2 + bx
+ c = 0 and where a not 0 ");
Scanner sc = new Scanner(System.in);
double a=sc.nextInt();
if (a==0)
{
System.out.println("a can not be zero");
}
else
{
double b=sc.nextInt();
double c=sc.nextInt();
double z=b*b-4*a*c;
EquationCheck ob=new EquationCheck();
if (z<0)
{
System.out.println("There are no real solutions");
}
else if(z==0)
{
System.out.println("The solutions are real and equal");
ob.check(a,b,c);
ob.display();
}
else
{
System.out.println("The solutions are real and distinct");
ob.check(a,b,c);
ob.display();
}
}
}
}
class EquationCheck
{
double a;
double b;
double c;
double x1;
double x2;
void check(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
double z=Math.pow( b*b-4*a*c , 0.5 );
x1=(-b-z)/(2*a);
x2=(-b+z)/(2*a);
}
void display()
{
System.out.println(x1);
System.out.println(x2);
}
}
::::::::::::::::::::::::::::::::::::::::::
Output:-
Enter the cofficients a,b,c of quadratic equation ax2 + bx + c = 0 and where a not 0
0 1 2
a can not be zero
…………………………………………………………………………………………………………………………………………………………………………………………………………………………….
Enter the coefficients a ,b ,c of quadratic equation ax2 + bx + c = 0 and where a not 0
1 4 4
The solutions are real and equal
-2.0
-2.0
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………
3 2 1
There are no real solution
Program -2:-
Program: Create a Java class called Student with the following details as
variables within it. USN, Name ,Branch, Phone.
Write a Java program to create n Student objects and print the USN, Name,
Branch, and Phone of these objects with suitable headings .
…................................................................................................................................................................
import java.util.Scanner;
void displayRecord()
{
System.out.println(USN+" "+Name+" "+branch+" "+phone);
}
Output:-
enter the number of students
3
enter the usn,name,branch,phone
:::::::::::::::::::::::::::::::::::::::::::::
Program 3:-
A. Write a program to check prime number
B. Write a program for Arithmetic calculator using switch case menu
….........................................................................................................
A. Write a program to check prime number:-
import java.util.Scanner;
if(count==0)
System.out.println("\nIt is a Prime Number.");
else
System.out.println("\nIt is not a Prime Number.");
}
}
OUTPUT:-
Enter a Number: 13
It is a Prime Number.
……………………………………………
Enter a Number: 29
It is a Prime Number.
.......................
Enter a Number: 100
..............................
Enter a Number: 58
::::::::::::::::::::::::::::::::::::::::::
B. Write a program for Arithmetic calculator using switch case menu
import java.util.Scanner;
public class ab39_CalculatorUsingSwitch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 1st number :");
int a = sc.nextInt();
System.out.println("Select operation");
System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
char ch = sc.next().charAt(0);
switch(ch) {
case 'a' :
System.out.println("Sum of the given two numbers: "+(a+b));
break;
case 's' :
System.out.println("Difference between the two numbers: "+(a-b));
break;
case 'm' :
System.out.println("Product of the two numbers: "+(a*b));
break;
case 'd' :
System.out.println("Result of the division: "+(a/b));
break;
default :
System.out.println("Invalid grade");
}
}
}
………………………………………………………..
Output:-
Enter value of 1st number :
20
Enter value of 2nd number :
30
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
a
Sum of the given two numbers: 50
……………………………………………………………………………………………..
Enter value of 1st number ::
100
Enter value of 2nd number ::
20
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
s
Difference between the two numbers: 80
………………………………………………………………………………………………………
Enter value of 1st number ::
300
Enter value of 2nd number ::
2
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
m
Product of the two numbers: 600
…………………………………………………………………………………………………
Enter value of 1st number ::
400
Enter value of 2nd number ::
8
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
d
Result of the division: 50
::::::::::::::::::::::::::::::::::::::::::
Program 4:-
Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend
this class by writing three subclasses namely Teaching (domain, publications),
Technical (skills), and Contract (period). Write a Java program to read and display at
least 3 staff objects of all three categories
class Staff
{
private int StaffId;
private String Name;
private String Phone;
private long Salary;
public Teaching(int staffId, String name, String phone,long salary, String domain,
int publications)
{
super(staffId, name, phone, salary);
Domain = domain;
Publications = publications;
}
public void Display()
{
super.Display();
System.out.print("\t\t"+Domain+"\t\t"+Publications+"\t\t"+"--"+"\t"+"--");
}
}
{
super(staffId, name, phone, salary);
Skills = skills;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t"+Skills+"\t"+"--");
}
}
Output:-
………………………………………………………………………………………………………………………………………………………..
Program 5:-
Write a java program demonstrating Method overloading and Constructor
overloading.
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Program:-5(ii)
(Constructor Overloading):-
// Java program to illustrate
// Constructor Overloading
class Box
{
double width, height, depth;
{
width = height = depth = 0;
}
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
:::::::::::::::::::::::::::::::::::::::::::::::::
currencycoversion.java:-
package project2;
import java.util.*;
double inr,usd;
double euro,yen;
usd=in.nextInt();
inr=usd*67;
inr=in.nextInt();
usd=inr/67;
euro=in.nextInt();
inr=euro*79.50;
inr=in.nextInt();
euro=(inr/79.50);
yen=in.nextInt();
inr=yen*0.61;
inr=in.nextInt();
yen=(inr/0.61);
............................................................
distanceconversion.java:-
package project2;
//package distanceconversion;
import java.util.*;
double km,m,miles;
System.out.print("Enter in km ");
km=sc.nextDouble();
m=(km*1000);
m=sc.nextDouble();
km=(m/1000);
System.out.print("Enter in miles");
miles=sc.nextDouble();
km=(miles*1.60934);
System.out.print("Enter in km ");
km=sc.nextDouble();
miles=(km*0.621371);
…............................................
timeconversion.java
package project2;
import java.util.*;
int hours,seconds,minutes;
int input;
input = sc.nextInt();
minutes=sc.nextInt();
hours=minutes/60;
minutes=minutes%60;
hours=sc.nextInt();
minutes=(hours*60);
hours=sc.nextInt();
seconds=(hours*3600);
}}
............................................
converter.java:-
package project2;
import java.util.*;
import java.io.*;
class converter
int choice,ch;
do
choice=s.nextInt();
switch(choice)
case 1:
c.dollartorupee();
break;
case 2:
c.rupeetodollar();
break;
case 3:
c.eurotorupee();
break;
case 4:
c.rupeetoeuro();
break;
case 5:
{c.yentorupee();
break;}
case 6 :
c.rupeetoyen();
break;
case 7 :
d.mtokm();
break;
case 8 :
d.kmtom();
break;
case 9 :
d.milestokm();
break;
case 10 :
d.kmtomiles();
break;
case 11 :
t.hourstominutes();
break;
case 12 :
t.hourstoseconds();
break;
case 13 :
t.secondstohours();
break;
case 14 :
t.minutestohours();
break;
}}
ch=s.nextInt();
}while(ch==1);
…..............................
Output:-
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…......................................
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…......................................
100
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….................................
20
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….............................
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…..................................
500
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….................................
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….................................
100
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…...................................
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…...............................
Enter in km 5
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…..............................
Enter in miles1
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…..............................
10
Enter in km 100
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…........................................
11
Minutes: 60
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…........................................
12
Minutes: 10800
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….......................................
13
Hours: 0
Minutes: 10
Seconds: 0
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
…...................................
13
Hours: 0
Minutes: 1
Seconds: 0
1.dollar to rupee
2.rupee to dollar
3.Euro to rupee
4..rupee to Euro
5.Yen to rupee
6.Rupee to Yen
7.Meter to kilometer
8.kilometer to meter
9.Miles to kilometer
10.kilometer to miles
11.Hours to Minutes
12.Hours to Seconds
13.Seconds to Hours
14.Minutes to Hours
….................................
Enter ur choice
14
Hours: 10
Minutes: 0
:::::::::::::::::::::::::::::::::
Program 7:-
Introduction to abstract classes, abstract methods, and Interface in java Program: Write a
program to generate the resume. Create 2 Java classes Teacher (data: personal information,
qualification, experience, achievements) and Student (data: personal information, result,
discipline) which implements the java interface Resume with the method biodata().
Resume.java
package lab;
void biodata();
……………………………………………………………………………………………………………….
Student.java
package lab;
String personalinformation;
String result ;
String discipline;
this.personalinformation=personalinformation;
this.result=result;
this.discipline=discipline;
@Override
System.out.println("Name is :"+personalinformation);
System.out.println("Result is :"+result);
System.out.println("Discipline :"+discipline);
}}
…………………………………………………………………………………………………………………………..
Teacher.java
package lab;
String personalinformation;
String qualification ;
String experience;
String achievement;
this.personalinformation=personalinformation;
this.qualification=qualification;
this.experience=experience;
this.achievement=achievement;
@Override
System.out.println("Name is :"+personalinformation);
System.out.println("Qualification is :"+qualification);
System.out.println("Experience is :"+experience);
System.out.println("Achievement:"+achievement);
}}
…………………………………………………………………………………………………………………………….
Program7.java
package lab;
s1.biodata();
System.out.println();
s2.biodata();
System.out.println();
s3.biodata();
System.out.println();
t1.biodata();
System.out.println();
t2.biodata();
…………………………………………………………………………………………………………………………………………
Output:-
Student Details are
Name is :Arman
Result is :Distinction
Discipline :CSE
Name is :Saburi
Result is :First Class
Discipline :ISE
Name is :Radha
Result is :First Class
Discipline :ISE
Name is :Sudha
Qualification is :M.Tech
Experience is :3 years
Achievement:Best teacher Award
Name is :Radhika
Qualification is :Phd
Experience is :5 years
Achievement:vtu gold medalist
Program 8:-
Program: Write a Java program that implements a multi-thread application that has
three threads. First thread generates a random integer for every 1 second; second
thread computes the square of the number and prints; third thread will print the value
of cube of the number.
import java.util.Random;
int x; Square(int n)
{
x = n;
}
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
int x; Cube(int n)
{
x = n;
}
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
…...........................................................
Output:-
Random Integer generated : 99
Square of 99 = 9801
Cube of 99 = 970299
Random Integer generated : 23
Square of 23 = 529
Cube of 23 = 12167
Random Integer generated : 1
Square of 1 = 1
Cube of 1 = 1
Random Integer generated : 90
Square of 90 = 8100
Cube of 90 = 729000
Random Integer generated : 92
Square of 92 = 8464
Cube of 92 = 778688
Random Integer generated : 64
Square of 64 = 4096
Cube of 64 = 262144
SEACET Dept. of CSE/ISE/AIML/IOT Page 45
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Program 9:-
Program: Write a program to perform string operations using Array List. Write
functions for the following a. Append - add at end b. Insert – add at particular index
c. Search d. List all string starts with given letter.
package p1;
import java.util.*;
import java.io.*;
int c,ch;
int i,j;
String str,str1;
do
System.out.println("STRING MANIPULATION");
System.out.println("******************************");
c=Integer.parseInt(in.readLine());
switch(c)
case 1:
str=in.readLine();
obj.add(str);
break;
case 2:
str=in.readLine();
i=Integer.parseInt(in.readLine());
obj.add(i-1,str);
break;
case 3:
str=in.readLine();
j=obj.indexOf(str);
if(j==-1)
else
break;
case 4:
str=in.readLine();
for(i=0;i<(obj.size()-1);i++)
str1=obj.get(i);
if(str1.startsWith(str))
System.out.println(str1);
break;
case 5:
break;
case 6:
str=in.readLine();
if(obj.remove(str))
System.out.println("Element Removed"+str);
else
break;
case 7:
Collections.sort(obj);
break;
case 8:
break;
ch=Integer.parseInt(in.readLine());
}while(ch==1);
…......................................................................
Output:-
STRING MANIPULATION
******************************
1. Append at end
3.Search
apple
STRING MANIPULATION
******************************
1. Append at end
3.Search
grapes
STRING MANIPULATION
******************************
1. Append at end
3.Search
guava
STRING MANIPULATION
******************************
1. Append at end
3.Search
banana
STRING MANIPULATION
******************************
1. Append at end
3.Search
guava
STRING MANIPULATION
******************************
1. Append at end
3.Search
Enter the character to List string that starts with specified character
app
apple
STRING MANIPULATION
******************************
1. Append at end
3.Search
STRING MANIPULATION
******************************
1. Append at end
3.Search
banana
STRING MANIPULATION
1. Append at end
3.Search
STRING MANIPULATION
******************************
1. Append at end
3.Search
import java.util.Scanner;
class Division
int a,b,result;
a=input.nextInt();
b=input.nextInt();
try
result=a/b;
System.out.println("Result="+result);
catch(ArithmeticException e)
System.out.println("Divisor is Zero");
}}}
…...........................................................
Result=30
…................................
5 50
Result=0
….................................
Result=6
…..................................
25 0
Divisor is Zero
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
}
public class FileDetails
{
public static void main(String rr[])throws IOException
{
filedemo fd=new filedemo();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file name:");
String s=br.readLine();
fd.analyze(s);
}
}
………………………………………………………………………………………………………………………………………………………………………………………………………………..
g.setColor(Color.green);
g.drawString("HELLO",50,40);
g.setColor(Color.red);
g.drawString("Welcome to Applet",60,70);
}}
.............................................................
Output:-
::::::::::::::::::::::::::::::::::::::::::::::::::::
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements
// create a textfield
static JTextField l;
// default constructor
calculator1()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}
// create a textfield
l = new JTextField(16);
// equals button
beq1 = new JButton("=");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
double te;
s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
// convert it to string
s0 = Double.toString(te);