Java Lab Manual
Java Lab Manual
BELAGAVI
(21CSL35)
(As per Visvesvaraya Technological University Syllabus)
Compiled By:
2022-23
Disclaimer
The information contained in this document is the proprietary and exclusive property of Acharya
Institutes except as otherwise indicated. No part of this document, in whole or in part, may be reproduced,
stored, transmitted, or used for course material development purposes without the prior written
permission of Acharya Institutes.
The information contained in this document is subject to change without notice. The information in this
document is provided for informational purposes only.
Trademark
Edition: 2022-23
Document Owner
The primary contact for questions regarding this document is:
List of Experiments
Program: Create a Java class called Student with the following details as variables within [Link]
Name
2 Branc
h
Phone
Write a Java program to create n Student objects and print the USN, Name, Branch, and Phoneof
these objects with suitable headings.
Aim: Discuss the various Decision-making statements, loop constructs in java
Program:
3
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
Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class by
4
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 Constructoroverloading.
Aim: Introduce the concept of Abstraction, packages.
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 vice versa),time
converter (hours to minutes, seconds and vice versa) using packages.
Aim: Introduction to abstract classes, abstract methods, and Interface in java
7 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
© Copyright Acharya Institutes
biodata(). Page 2
Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threadedprogramming.
8
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
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.
Aim: Introduce java Collections.
9 Program: Write a program to perform string operations using ArrayList. Write functions for
thefollowing a. Append - add at end b. Insert – add at particular index c. Search d. List all
string 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, when b is
notzero. 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
thefile 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
calculatorusing Swings.
PART B – Practical Based Learning
A problem statement for each batch is to be generated in consultation with the co-examiner and
01 student should develop an algorithm, program and execute the program for the
givenproblem with appropriate outputs.
PROGRAM :1
1. 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.
PROGRAM :
import [Link];
public class Quadratic
{
public static void main(String[] args)
{
int a, b, c; // coefficients
double root1, root2;
[Link]("Enter the coefficients");
Scanner in=new Scanner([Link]);
a = [Link]();
b = [Link]();
c = [Link]();
OUT PUT:
PROGRAM :2
2. 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 Phoneof
these objects with suitable headings.
PROGRAM :
import [Link].*;
public class Student
{
String usn,name,branch;
long phone;
void displayStudent()
{
[Link]("**********************");
[Link]("USN= "+usn);
[Link]("NAME= "+name);
[Link]("BRANCH= "+branch);
[Link]("PHONE NUMBER= "+phone);
[Link]("**********************");
}
for(int i=0;i<n;i++)
st[i]=new Student();
for(int j=0;j<n;j++)
{
[Link]("Enter the Usn,Name,Branch,Phone Number");
String usn=[Link]();
String name=[Link]();
String branch=[Link]();
long phone=[Link]();
st[j].insertStudent(usn,name,branch,phone);
}
for( int m=0;m<n;m++)
{
[Link]("Student %d details are\n",m+1);
st[m].displayStudent();
}
}
}
OUTPUT:
PROGRAM :3
PROGRAM :3A
import [Link];
class Prime
{
public static void main(String args[])
{
int i,n,flag=0;
[Link]("Enter the number");
Scanner inp=new Scanner([Link]);
n=[Link]();
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
[Link]("The given number is Not a Prime");
else
[Link]("The given number is Prime");
}
}
OUTPUT:
PROGRAM :3B
import [Link].*;
class Switch
{
public static void main(String[] args)
{
Scanner inp = new Scanner([Link]);
[Link]("Enter the Operator (+,-,*,/) : ");
char operator = [Link]().charAt(0);
[Link]("Enter the First Operand : ");
double first = [Link]();
[Link]("Enter the Second Operand : ");
double second = [Link]();
double result = 0;
switch(operator)
{
case '+':
result = first + second;
[Link]("The Result is : "+first+" "+operator+" "+second+" = "+result);
break;
case '-':
result = first - second;
[Link]("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
break;
case '*':
result = first * second;
[Link]("The Result is : "+first+" "+operator+" "+second+" = "+result);
break;
case '/':
result = first / second;
[Link]("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
break;
default :
[Link]("Invalid Operator");
break;
}
}
}
OUTPUT:
PROGRAM :4
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.
PROGRAM:
import [Link];
class Staff
{
String staffId;
String name;
long phone;
float salary;
public void accept()
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter Staff Id: ");
staffId = [Link]();
[Link]("Enter Name: ");
name = [Link]();
[Link]("Enter Phone: ");
phone = [Link]();
[Link]("Enter Salary: ");
salary = [Link]();
}
public void display()
{
[Link]("Staff Id: " + staffId);
[Link]("Name: " + name);
[Link]("Phone: " + phone);
[Link]("Salary: " + salary);
}
}
class Teaching extends Staff
{
String domain;
int n;
public void accept()
{
[Link]();
Scanner scanner = new Scanner([Link]);
© Copyright Acharya Institutes Page 12
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
}
class Contract extends Staff
{
int period;
public void accept()
{
[Link]();
Scanner scanner = new Scanner([Link]);
[Link]("Enter Period: ");
period = [Link]();
[Link]("\n");
}
OUTPUT :
PROGRAM :5
PROGRAM:5A
OUTPUT:
PROGRAM:5B
Constructor Overloading
public class Constructor
{
int id;
String name;
Constructor()
{
[Link]("This is Default constructor");
[Link]("Student Id : "+id + "\nStudent Name : "+name);
}
Constructor(int i, String n)
{
[Link]("This is Parameterized Constructor:");
id = i;
name = n;
[Link]("Student Id : "+id + "\nStudent Name : "+name);
}
public static void main(String[] args)
{
Constructor s = new Constructor();
Constructor student = new Constructor(10, "David");
}
}
OUTPUT:
PROGRAM :6
6. 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 vice versa),
time converter (hours to minutes, seconds and vice versa) using packages.
PROGRAM:
[Link]
package cc;
import [Link].*;
public class CurrencyC
{
double inr,usd;
double euro,yen;
Scanner in=new Scanner([Link]);
public void dollartorupee()
{
[Link]("Enter dollars to nvert into Rupees:");
usd=[Link]();
inr=usd*81.83;
[Link]("Dollar ="+usd+" equal to INR="+inr);
[Link]("\n");
}
public void rupeetodollar()
{
[Link]("Enter Rupee to convert into Dollars:");
inr=[Link]();
usd=inr/81.83;
[Link]("Rupee ="+inr+"equal to Dollars="+usd);
}
public void eurotorupee()
{
[Link]("Enter Euro to convert into Rupees:");
euro=[Link]();
inr=euro*79.06;
[Link]("Euro ="+euro+" equal to INR="+inr);
[Link]("\n");
}
[Link]
package dc;
import [Link].*;
public class DistanceC
{
double km,m,miles;
Scanner in=new Scanner([Link]);
public void mtokm()
{
[Link]("Enter the distance in meter");
m=[Link]();
km=(m/1000);
[Link](m+"m" +" is equal to "+km+"km");
[Link]("\n");
}
public void kmtom()
{
[Link]("Enter the distance in Kilometer");
km=[Link]();
m=km*1000;
[Link](km+"km" +" is equal to "+m+"m");
[Link]("\n");
}
public void milestokm()
{
[Link]("Enter the distance in miles");
miles=[Link]();
km=(miles*1.60934);
[Link](miles+"miles" +" is equal to "+km+"km");
[Link]("\n");
}
[Link]
package tc;
import [Link].*;
public class TimeC
{
int hours,seconds,minutes;
Scanner in = new Scanner([Link]);
public void hourstominutes()
{
[Link]("Enter the no of Hours to convert into minutes");
hours=[Link]();
minutes=(hours*60);
[Link]("Minutes: " + minutes);
}
public void minutestohours()
{
[Link]("Enter the no of Minutes to convert into Hours");
minutes=[Link]();
hours=minutes/60;
[Link]("Hours: " + hours);
}
public void hourstoseconds()
{
[Link]("Enter the no of Hours to convert into Seconds");
hours=[Link]();
seconds=(hours*3600);
[Link]("Seconds: " + seconds);
}
public void secondstohours()
{
[Link]("Enter the no of Seconds to convert into Hours");
seconds=[Link]();
hours=seconds/3600;
[Link](seconds+"seconds"+ " is equal to "+hours+"hour");
}
}
Main Class
import cc.*;
import dc.*;
import tc.*;
public class Main
{
public static void main(String args[])
{
CurrencyC obj=new CurrencyC();
DistanceC obj1=new DistanceC();
TimeC obj2=new TimeC();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
Enter dollars to convert into Rupees:1
Dollar =1.0 equal to INR=81.83
PROGRAM :7
7. 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().
PROGRAM:
interface Resume
{
void biodata();
}
class Teacher implements Resume
{
String name,qualification,achievements;
float experience;
public void biodata()
{
name="Imran Ulla Khan";
qualification="[Link]";
achievements="Q1 publication";
experience=14.8f;
[Link]("Teacher Resume");
[Link]("Name : " +name);
[Link]("Qualification : "+qualification);
[Link]("Achievements : "+achievements);
[Link]("Experience : "+experience);
}
}
class Student implements Resume
{
String name,discipline;
float result;
public void biodata()
{
name="Rahul Sharma";
result=9.8f;
discipline="Computer Science and Engineering";
[Link]("");
[Link]("Student Resume");
[Link]("Name : " +name);
[Link]("Result : "+result+" cgpa");
[Link]("Discipline : "+discipline);
}
}
© Copyright Acharya Institutes Page 25
public class InterfaceP
{
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
OUTPUT:
PROGRAM :8
8. 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 computesthe square of the
number and prints; third thread will print the value of cube of the number.
Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threaded programming.
PROGRAM :
import [Link];
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
[Link]("Square of " + x + " = " + sqr );
}
}
}
}
OUTPUT:
PROGRAM :9
9. 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 all string
starts with given letter.
import [Link].*;
public class ArrayL
{
ArrayList<String> list=new ArrayList<String>(); //Creating arraylist
public void arraydisplay()
{
[Link]("CSE");//Adding object in arraylist
[Link]("ISE");
[Link]("ME");
[Link]("ArrayList element are");
[Link](list);
[Link]("");
}
public void appendatend()
{
[Link]("Enter the element to append at end");
Scanner scob1=new Scanner([Link]);
String ele=[Link]();
[Link](ele);
[Link](list);
[Link]("");
}
public void insertatpos()
{
[Link]("Enter the position and element to insert");
Scanner scob1=new Scanner([Link]);
int posind=[Link]();
String ele=[Link]();
[Link](posind,ele);
[Link](list);
[Link]("");
}
else
{
[Link]("Element found at "+in);
}
}
void print()
{
Scanner nip=new Scanner([Link]);
[Link]("Enter the starting charecter to print strings");
char inputc=[Link]().charAt(0);
String strc=[Link](inputc);
[Link]("String starting with character "+strc);
for(int i=0;i<[Link]();i++)
{
if([Link](i).startsWith(strc))
{
[Link]([Link](i));
}
}
}
public static void main(String args[])
{
ArrayL obj=new ArrayL();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
PROGRAM :33
10. Write a Java program to read two integers a and b. Compute a/b and print, when bis not zero.
Raise an exception when b is equal to zero.
Aim: Exception handling in java, introduction to throwable class, throw, throws, finally
import [Link].*;
public class TryP
{
int c;
void div(int a,int b)
{
try
{
c=a/b;
[Link]("Result="+c);
}
catch(ArithmeticException e)
{
[Link]("Cannot divide by zero");
}
}
public static void main(String args[])
{
TryP obj=new TryP();
Scanner in=new Scanner([Link]);
[Link]("Enter the values of a and b");
int no1=[Link]();
int no2=[Link]();
[Link](no1,no2);
}
}
OUTPUT:
11. 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
import [Link];
import [Link];
class FileP
{
public static void main(String args[ ])
{
Scanner obj=new Scanner([Link]);
String fname=[Link]();
File f1 = new File(fname);
[Link]("File Name: " + [Link]());
[Link](false);
[Link]([Link]() ? "File exists" : "File does not exist");
[Link]([Link]() ? "File is writeable" : "File is not writeable");
[Link]([Link]() ? "File is readable" : "File is not readable");
String fileName = [Link]();
int index = [Link]('.');
if(index > 0)
{
String type = [Link](index + 1);
[Link]("File type is " + type);
}
else
{
[Link]("File doesn't have type");
}
[Link]("File size: " + [Link]() + " Bytes");
}
}
OUTPUT:
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
PROGRAM :12
12. A. Develop an applet that displays a simple message in center of the screen.
PROGRAM:
import [Link];
import [Link];
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
public class AppletP extends Applet
{
public void paint(Graphics g)
{
[Link]("Welcome to applet",100,150);
}
}
OUTPUT:
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
import [Link].*;
import [Link].*;
import [Link];
import [Link];
t=new JTextField();
[Link](30,10,165,35);
b0=new JButton("0");
[Link](30,50,45,40);
b1=new JButton("1");
[Link](70,50,45,40);
b2=new JButton("2");
[Link](110,50,45,40);
b3=new JButton("3");
[Link](150,50,45,40);
b4=new JButton("4");
[Link](30,90,45,40);
b5=new JButton("5");
[Link](70,90,45,40);
b6=new JButton("6");
[Link](110,90,45,40);
b7=new JButton("7");
[Link](150,90,45,40);
b8=new JButton("8");
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
[Link](30,130,45,40);
b9=new JButton("9");
[Link](70,130,45,40);
bdot=new JButton(".");
[Link](110,130,45,40);
badd=new JButton("+");
[Link](150,130,45,40);
bsub=new JButton("-");
[Link](30,170,45,40);
bmul=new JButton("*");
[Link](70,170,45,40);
bdiv=new JButton("/");
[Link](110,170,45,40);
beq=new JButton("=");
[Link](150,170,45,40);
bclr=new JButton("CLR");
[Link](30,210,165,40);
[Link](t);
[Link](b0);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bdot);
[Link](badd);
[Link](bsub);
[Link](bmul);
[Link](bdiv);
[Link](beq);
[Link](bclr);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==bclr)
{
[Link]("");
}
if([Link]()==b0)
{
[Link]([Link]().concat("0"));
}
if([Link]()==b1)
{
[Link]([Link]().concat("1"));
}
if([Link]()==b2)
{
[Link]([Link]().concat("2"));
}
if([Link]()==b3)
{
[Link]([Link]().concat("3"));
}
if([Link]()==b4)
{
[Link]([Link]().concat("4"));
}
if([Link]()==b5)
{
[Link]([Link]().concat("5"));
}
if([Link]()==b6)
{
[Link]([Link]().concat("6"));
}
if([Link]()==b7)
{
[Link]([Link]().concat("7"));
}
if([Link]()==b8)
{
[Link]([Link]().concat("8"));
}
if([Link]()==b9)
{
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
[Link]([Link]().concat("9"));
}
if([Link]()==bdot)
{
[Link]([Link]().concat("."));
}
if([Link]()==badd)
{
a=[Link]([Link]());
op=1;
[Link]("");
}
if([Link]()==bsub)
{
a=[Link]([Link]());
op=2;
[Link]("");
}
if([Link]()==bmul)
{
a=[Link]([Link]());
op=3;
[Link]("");
}
if([Link]()==bdiv)
{
a=[Link]([Link]());
op=4;
[Link]("");
}
if([Link]()==beq)
{
b=[Link]([Link]());
switch(op)
{
case 1:res=a+b;
break;
case 2:res=a-b;
break;
case 3:res=a*b;
break;
case 4:res=a/b;
break;
}
[Link](""+res);
}
}
}
OUTPUT: