Java Lab Programs PDF
Java Lab Programs PDF
Aim:
Introduce the java fundamentals, data types, and operators in
java Program: Write a java program that prints all real
solutions to the quadratic equation ax2+bx+c=0. Read in a, b,
and c and use the quadratic formula.
import java.util.Scanner;
public class QuadraticEquationExample1
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b -‐ 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-‐b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-‐b -‐ Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -‐b / (2.0 * a);
System.out.println("The root is " + r1);
}
else
{
System.out.println("Roots are not real.");
}
}
}
2) Aim:
Demonstrating creation of java classes, objects, constructors,
declarations and initialization of variables. 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;
class Student
{
String stdname,branch;
int usn,phone_no;
void readStudent()
{
Scanner scan=new Scanner(System.in);
System.out.println(“Enter student name”);
scan.nextLine();
stdname=scan.nextLine();
System.out.println(“Enter student branch”);
Branch=scan.nextLine();
System.out.println(“Enter usn”);
usn=scan.nextInt();
System.out.prinltn(“Enter phono no”);
Phone_no=scan.nextLint();
}
void printStudent()
{
System.out.println(“Student information is”);
System.out.println(“Student name is “+stdname);
System.out.println(“Student department is”+branch);
System.out.println(“Student Roll no is”,+usn);
System.out.println(“Student phone number is”+phone_no);
}
}
class A
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.readStudent();
s1.printStudent();
s2.readStudent();
s2.printStudent();
}
}
3) Aim:
Discuss the various Decision-‐making statements and loop
constructs in java Program:
A. Write a program to check prime number
B. Write a program for an Arithmetic calculator using the
switch case menu
Program for 3.A)
import java.util.Scanner;
public class Prime
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i=1,count=0,num;
System.out.println("Enter the num");
num=scan.nextInt();
while(i<=num)
{
if(num%i==0)
count++;
i++;
}
if(count==2)
System.out.println("Given num is Prime");
else
System.out.println("Given num is not Prime");
}
}
Program for 3.B)
import java.util.Scanner;
public class Calculator {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int a,b,c,choice;
System.out.println("Enter the value of a ");
a=scan.nextInt();
System.out.println("Enter the value of b");
b=scan.nextInt();
System.out.println("Enter your choice 1. addition 2.
subtraction 3. multiplication 4.divison");
choice=scan.nextInt();
switch(choice)
{
case 1: c=a+b;
System.out.println("Addition of two
number is "+c);
break;
case 2: c=a-‐b;
System.out.println("subtraction of two
numbers is "+c);
break;
case 3: c=a*b;
System.out.println("Multiplication of two
numbers is "+c );
break;
case 4: c=a/b;
System.out.println("Divison of two number
is "+c);
break;
default: System.out.println("Invalid choice");
}
}
}
4) Aim:
Demonstrate the core object-‐oriented concept of Inheritance,
polymorphism Design a superclass called Staff with details as
StaffId, Name, Phone, and 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.
import java.util.Scanner;
class Staff
{
int staffid;
String name;
long phone;
int salary;
Scanner scan=new Scanner(System.in);
void Read_Staff()
{
System.out.println("Enter Staff ID");
staffid = scan.nextInt();
System.out.println("Enter Staff Name");
scan.nextLine();
name = scan.nextLine();
System.out.println("Enter Staff Phone number");
phone = scan.nextLong();
System.out.println("Enter Staff Salary");
salary = scan.nextInt();
}
void Display_Staff()
{
System.out.print(staffid + "\t" + name + "\t" + phone + "\t" +
salary + "\t");
}
}
class Teaching extends Staff
{
private String domain;
private String pub;
void Read_Teaching()
{
super.Read_Staff();
System.out.println("Enter Domain");
scan.nextLine();
domain = scan.nextLine();
System.out.println("Enter Publications");
pub = scan.nextLine();
}
void Display_Teaching()
{
System.out.println(staffid + "\t" + name + "\t" + phone + "\t" +
salary + "\t"+domain + "\t" + pub);
}
}
class Technical extends Staff
{
private String skills;
void Read_Technical()
{
super.Read_Staff();
System.out.println("Enter skills");
skills = scan.nextLine();
}
void Display_Technical()
{
super.Display_Staff();
System.out.println(skills);
}
}
class Contract extends Staff
{
private float period;
void Read_Contract()
{
super.Read_Staff();
System.out.println("Enter Experience in years");
period = scan.nextFloat();
}
void Display_Contract()
{
super.Display_Staff();
System.out.println(period);
}
}
public class Demo1
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your choice");
System.out.println("1. Teaching \n 2. Technical \n 3.
Contract ");
int ch = scan.nextInt();
System.out.println("Enter number of records");
int no = scan.nextInt();
switch(ch)
{
case 1: Teaching[] t = new Teaching[no];
for(int i = 0; i < t.length; i++ )
{
System.out.println("Enter " + (i + 1) + " details");
t[i] = new Teaching();
t[i].Read_Teaching();
}
System.out.println("Teaching Staff details are as
follows:");
System.out.println("StaffID" + "\t" + "Name" + "\t" +
"Phone" + "\t\t" + "Salary" + "\t"
+ "Domain" + "\t" + "Publications");
for(int i = 0; i < t.length; i++ )
{
t[i].Display_Teaching();
}
break;
case 2: Technical[] tech = new Technical[no];
for(int i = 0; i < tech.length; i++ )
{
System.out.println("Enter " + (i + 1) + " details");
tech[i] = new Technical();
tech[i].Read_Technical();
}
System.out.println("Technical Staff details are as
follows:");
System.out.println("StaffID" + "\t" + "Name" + "\t" +
"Phone" + "\t\t" + "Salary" + "\t"
+ "Skills" );
for(int i = 0; i < tech.length; i++ )
{
tech[i].Display_Technical();
}
break;
case 3: Contract[] c = new Contract[no];
for(int i = 0; i < c.length; i++ )
{
System.out.println("Enter " + (i + 1) + " details");
c[i] = new Contract();
c[i].Read_Contract();
}
System.out.println("Technical Staff details are as
follows:");
System.out.println("StaffID" + "\t" + "Name" + "\t" +
"Phone" + "\t" + "Salary" + "\t" + "Period" );
for(int i = 0; i < c.length; i++ )
{
c[i].Display_Contract();
}
break;
default:
System.out.println("Wrong Choice");
break;
}
}
}
5) Aim:
Introduce concepts of method overloading, constructor
overloading, and overriding. Program: Write a java program
demonstrating Method overloading and Constructor
overloading.
class A
{
A()
{
System.out.println(“Constructor with no parameters in class
A”);
}
A(int a)
{
System.out.println(“Constructor with int parameter in class A”);
void m1()
{
System.out.println(“In m1() with no paramaters”);
}
void m1(int a)
{
System.out.println(“In m1() with one integer parameter”);
}
public static void main(String args[])
{
A ob=new A();
A ob1=new A(100);
ob.m1();
ob1.m1(900);
}
}
6) Aim:
Introduce the concept of Abstraction and packages. Program:
Develop a java application to implement a 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.
//Time Conversion package
package Time;
import java.util.Scanner;
public class TimeConvert
{
private Scanner scan;
public void timeConvert()
{
scan = new Scanner(System.in);
System.out.println("Enter your choice \n 1. Hour to minute and
Seconds \n"+ "2. Minutes to HOurs \n 3. seconds to Hour and
minute ");
int choice=scan.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the time in hours");
double time=scan.nextDouble();
double minutes=time*60;
double seconds=time*3600;
System.out.println("There are " + minutes + " minutes in " +
time + " hours");
System.out.println("There are " + seconds + " seconds in " +
time + " hours");
break;
case 2:
System.out.println("Enter number of minutes");
double min=scan.nextInt();
double hours=min/60 ;
double seconds1=min*60 ;
System.out.println("There are " + hours + " hours in " + min + "
min");
System.out.println("There are " + seconds1 + " seconds in " +
min + " min");
break;
case 3:
System.out.println("Enter Seconds");
int seconds2 = scan.nextInt();
int min1 = seconds2 /60;
int hours1 = seconds2 / 3600;
System.out.println( hours1 + ":" + min1 + ":" + seconds2);
}
}
}
//Meter Conversion Package
package METERConversion;
import java.util.Scanner;
public class MeterConversion
{
public void meterConvert()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter your choice \n 1. "
+ "KM to Meter and Miles \n"
+ "2. Miles to KM and Meter \n"
+ " 3. Meter to KM and Miles ");
int choice=scan.nextInt();
switch(choice)
{
case 1:
System.out.println("Please enter kilometers:");
double km = scan.nextDouble();
double miles=km/1.6;
double meter=km*1000;
System.out.println("There are " + miles + " miles in " + km + "
kilometer");
System.out.println("There are " + meter + " meters in " + km + "
Kilometer");
break;
case 2:
System.out.println("Please enter miles:");
miles = scan.nextDouble();
km=miles*1.6;
meter=miles*1609.34;
System.out.println("There are " + km + " km " +
miles + " miles");
System.out.println("There are " + meter + " meters
in " +miles + " miles");
break;
case 3:
System.out.println("Please enter meter:");
meter = scan.nextDouble();
km=meter/1000;
miles=meter*0.00062137;
System.out.println("There are " + km + " km " + meter
+ " meter");
System.out.println("There are " + miles + " meters in " +meter +
" meter");
}
}
}
//Currency Converter package
package CurrencyConvert;
import java.util.Scanner;
public class Currency
{
Scanner scan=new Scanner(System.in);
public void CurrencyConvert()
{
int choice=1;
float dollar,amount,pound,euro,rupee;
System.out.println("Enter the amount");
amount=scan.nextInt();
System.out.println("Enter your choice");
System.out.println("1.Rupee to dollar and pound and
euro");
System.out.println("2. Dollar to Rupees and pound
and euro");
System.out.println("3. Pound to Rupees and euro and
dollar");
choice=scan.nextInt();
switch (choice)
{
case 1: // Ruppe Conversion
dollar = amount / 70;
System.out.println(amount+" Rupee ="+dollar+"dollar");
pound = amount / 88;
System.out.printf("\n%.2f Rupee = %.2f pound",
amount, pound);
euro = amount / 80;
System.out.printf("\n%.2f Rupee = %.2f euro",
amount, euro);
break;
case 2: // Dollar Conversion
rupee = amount * 70;
System.out.printf("\n%.2f Dollar = %.2f rupee",
amount, rupee);
pound = (float)(amount *0.78);
System.out.printf("\n%.2f Dollar = %.2f pound",
amount, pound);
euro = (float)(amount *0.87);
System.out.printf("\n%.2f Dollar = %.2f euro",
amount, euro);
break;
case 3: // Pound Conversion
rupee = amount * 88;
System.out.printf("\n%.2f Pound = %.2f rupee",
amount, rupee);
dollar = (float)(amount *1.26);
System.out.printf("\n%.2f Pound = %.2f dollar",
amount, dollar);
euro = (float)(amount *1.10);
System.out.printf("\n%.2f Pound = %.2f euro",
amount, euro);
break;
case 4: // Euro Conversion
rupee = amount * 80;
System.out.printf("\n%.2f Euro = %.2f rupee",
amount, rupee);
dollar = (float)(amount *1.14);
System.out.printf("\n%.2f Euro = %.2f dollar",
amount, dollar);
pound = (float)(amount *0.90);
System.out.printf("\n.2%f Euro = %.2f pound",
amount, pound);
break;
//Default case
default:
System.out.printf("\nInvalid Input");
}
}
}
//Main Class
import java.util.Scanner;
import CurrencyConvert.Currency;
import METERConversion.MeterConversion;
import Time.TimeConvert;
public class Converter
{
private static Scanner scan;
public static void main(String[] args)
{
Currency c=new Currency();
MeterConversion m=new MeterConversion();
TimeConvert t=new TimeConvert();
scan = new Scanner(System.in);
boolean status=true;
while(status)
{
System.out.println("Enter your choice");
System.out.println("1.Currency Convert");
System.out.println("2. Meter Conversion");
System.out.println("3. Time COnvert");
int choice=scan.nextInt();
switch(choice)
{
case 1: c.CurrencyConvert();
break;
case 2: m.meterConvert();
break;
case 3: t.timeConvert();
break;
default: System.out.println("invalid choice");
}
System.out.println("Do you want to continue");
status=scan.nextBoolean();
}
}
}
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()
import java.util.Scanner;
interface Resume
{
void biodata();
}
class Teacher implements Resume
{
Scanner scan;
String name,qualification,achievements;
int experience;
public void biodata()
{
scan=new Scanner(System.in);
System.out.println("Enter the name");
name=scan.nextLine();
System.out.println("Enter the qualification");
qualification=scan.nextLine();
System.out.println("Enter the achievements");
achievements=scan.nextLine();
System.out.println("Enter the experience");
experience=scan.nextInt();
}
void resume()
{
System.out.println("Teacher resume ");
System.out.println("______________");
System.out.println("Name is "+name);
System.out.println("Qulaification is "+qualification);
System.out.println("Achievements are
"+achievements);
System.out.println("Total Experience "+experience);
}
}
public class Student implements Resume
{
String dept,name;
String result;
public void biodata()
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the name");
name=scan.nextLine();
System.out.println("Enter the department");
dept=scan.nextLine();
System.out.println("Enter the result");
result=scan.nextLine();
}
void resume()
{
System.out.println("Student resume ");
System.out.println("______________");
System.out.println("Name is "+name);
System.out.println("Department is "+dept);
System.out.println("Result is: "+result);
}
public static void main(String[] args)
{
// TODO Auto-‐generated method stub
Teacher t=new Teacher();
t.biodata();
t.resume();
Student s= new Student();
s.biodata();
s.resume();
}
}
8) Aim:
Demonstrate creation of threads using Thread class and
Runnable interface, multithreaded programming. 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;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr);
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub);
}
}
class Number extends Thread
{
public void run()
{
Random random = new Random();
for(int i =0; i<5; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : "
+ randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try
{
Thread.sleep(4000);
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
}
public class EvenOdd
{
public static void main(String args[])
{
Number n = new Number();
n.start();
}
}
9 ) Aim:
Introduce java Collections. 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 all string starts with a given
letter.
import java.util.ArrayList;
public class ArrayListDemo2
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("VivekIT");
al.add("bkit");
al.add("Vit");
System.out.println(al);
al.add(2,"bhalki");
System.out.println(al);
System.out.println(al.indexOf("bhalki"));
for(int i=0;i<al.size();i++)
{
String str1=al.get(i);
if(str1.startsWith("V"))
System.out.println(str1);
}
}
}
10. Aim:
Exception handling in java, introduction to the throwable
class, throw, throws, finally. Program: Write a Java program to
read two integers, a and b. Compute a/b and print when b is
not zero. Raise an exception when b is equal to zero.
import java.util.Scanner;
class ZeroException extends Exception
{
}
class A
{
public static void main(String args[])
{
int a,b,c;
Scanner scan=new Scanner(System.in);
System.out.println(“Enter the value of a”);
a=scan.nextInt();
System.out.println(“Enter the value of b”);
b=scan.nextInt();
try
{
if(b==0)
throw new ZeroException();
else
{
c=a/b;
System.out.println(“Divison of two numbers is”+c);
}
}
catch(ZeroException e)
{
System.out.println(“Value of b must be greater than 0”);
}
}