Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Java Programming Lab Manual-1

The document describes an Airport class that tracks arrival and departure records of planes. It creates subclasses Arrival and Departure that implement the Airport class and record plane name, ID, arrival/departure time along with a counter. A Passenger class is also created to keep passenger records. The Airport class has a search method to find passengers by name. Interfaces are used for the arrival and departure classes to define common methods to set and display values.

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Java Programming Lab Manual-1

The document describes an Airport class that tracks arrival and departure records of planes. It creates subclasses Arrival and Departure that implement the Airport class and record plane name, ID, arrival/departure time along with a counter. A Passenger class is also created to keep passenger records. The Airport class has a search method to find passengers by name. Interfaces are used for the arrival and departure classes to define common methods to set and display values.

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Program No.

- 1
Consider we have a Class of Cars under which Santro Xing, Alto and Wagon R
represents individual Objects. In this context each Car Object will have its own, Model,
Year of Manufacture, Colour, Top Speed, etc. which form Properties of the Car class
and the associated actions i.e., object functions like Create(), Sold(), display() form the
Methods of Car Class. Use this class to create another class Company that tracks the
models it create.

class Cars
{
int year,topSpeed;
String model,colour;

void create (String model,int year,int topSpeed,String colour) {


this.model = model;
this.year = year;
this.topSpeed = topSpeed;
this.colour = colour;
}
void sold (int unit)
{
System.out.println("No of Units Sold= " + unit) ;
}

void display()
{
System.out.println("\nModel =" +model+"\nYear =" +year+"\nColour ="
+colour+"\nTop speed =" +topSpeed);
}
}

public class Practical1


{

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 1 | 51
public static void main(String args[])
{
Cars santro = new Cars();
Cars xing = new Cars();
Cars alto = new Cars();
Cars wagonR = new Cars();
santro.create("Santro",2020,140,"white");
santro.display();
santro.sold(20);
xing.create("Xing",2021,120,"Red");
xing.display();
xing.sold(30);
wagonR.create("Wagon R",2022,130,"Creame White");
wagonR.display();
wagonR.sold(20);
alto.create("Alto",2022,140,"Creame White");
alto.display();
alto.sold(34);
}
}

Output:

Model =Santro
Year =2020
Colour =white
Top speed =140
No of Units Sold= 20

Model =Xing
Year =2021
Colour =Red
Top speed =120

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 2 | 51
No of Units Sold= 30

Model =Wagon R
Year =2022
Colour = Creame White
Top speed =130
No of Units Sold= 20

Model =Alto
Year =2022
Colour =Creame White
Top speed =140
No of Units Sold= 34

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 3 | 51
Program No. 2
In a software company Software Engineers, Sr. Software Engineers, Module Lead,
Technical Lead, Project Lead, Project Manager, Program Manager, Directors all are
the employees of the company but their work, perks, roles, responsibilities differs.
Create the Employee base class would provide the common behaviours of all types of
employee and also some behaviours properties that all employee must have for that
company. Also include search method to search an employee by name.

import java.util.*;
class Employee
{
String name;
int id;
}

class EmpType extends Employee


{
String work;
int perks;
String roles;
String resp;

void setValues(String name,int id,String work,int perks,String roles,String resp)


{
this.name = name;
this.id = id;
this.work = work;
this.perks = perks;
this.roles = roles;
this.resp = resp;

}
void display()

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 4 | 51
{
System.out.println(" Employee Id = "+ id);
System.out.println(" Employee Name = "+ name);
System.out.println(" Work of Employee = "+ work);
System.out.println(" Perks of Employee = "+ perks);
System.out.println(" Roles of Employee = "+ roles);
System.out.println(" Responsibility of Employee = "+ resp);

}
}

public class Practical2


{
public static void main(String args[])
{
EmpType [] Emp = new EmpType[2];
Scanner input = new Scanner(System.in);
int i,j,k;
for (i = 0; i < 2; ++i)
{
Emp[i] = new EmpType();
System.out.println("Employee Name = ");
String name = input.nextLine();
System.out.println("Work of Employee = ");
String work = input.nextLine();
System.out.println("Perks of Employee = ");
int perks = input.nextInt();
System.out.println("Roles of Employee = ");
String roles = input.nextLine();
System.out.println("Responsibility of Employee = ");
String resp = input.nextLine();
Emp[i].setValues(name,i,work,perks,roles,resp);
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 5 | 51
System.out.println("\n\nEmployee Details");
for ( j = 0;j<2; ++j)
Emp[j].display();

while(true)
{
System.out.println("\n\nDo you want to search employee = (y/n");
String dec1 = input.next();
switch(dec1) {
case "Y":
case "y":
System.out.println("\n\nEnter name of the employee to search=");
String n1 = input.next();
for (k = 0; k < 2; ++k) {
if (Emp[k].name.equals(n1)) {
System.out.println("\n\nEmployee Found");
Emp[k].display();
break;
}
}
if (k == 2)
System.out.println("\n\nEmployee has not found");
break;
case "N":
case "n":
System.exit(0);
default:
System.out.println("\n\nDo you want to continue = (y/n) ");
}
}
}
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 6 | 51
Output:
Employee Name =
Ram
Work of Employee =
Manger
Roles of Employee =
Management
Responsibility of Employee =
Managing
Perks of Employee =
25000
Employee Name =
Work of Employee =
Shyaam
Roles of Employee =
Software Engineer
Responsibility of Employee =
Development
Perks of Employee =
24000

Employee Details
Employee Id = 0
Employee Name = Ram
Work of Employee = Manger
Perks of Employee = 25000
Roles of Employee = Management
Responsibility of Employee = Managing
Employee Id = 1
Employee Name =
Work of Employee = Shyaam
Perks of Employee = 24000

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 7 | 51
Roles of Employee = Software Engineer
Responsibility of Employee = Development
Do you want to search employee = (y/n
y

Enter name of the employee to search=


Ram

Employee Found
Employee Id = 0
Employee Name = Ram
Work of Employee = Manager
Perks of Employee = 25000
Roles of Employee = Managing
Responsibility of Employee = Management
Do you want to search employee = (y/n
n
Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 8 | 51
Program No. - 3
Suppose the Airport personals want to maintain records for the arrival and departure
of the planes. Create a class Airport that has data like name, id, and address. Create
two more classes for Arrival and Departure implementing Airport that will have track
of planes (their name, id, arrival time or departure time and a counter to count the
number of arrivals) also include the necessary methods to access the information.
Also try to keep record of passengers by creating a new class Passenger. Also include
a method search () in Airport class to search any passenger by name.

import java.util.*;
import java.lang.*;
interface one {
public void arrivalValues();

public void arrivalDisplay();


}
interface two {
public void departureValues();

public void departureDisplay();


}
class Airport
{
String airportName;
int airportId;
String airportAddress;
void getAirport()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter Airport Details");
System.out.println("Airport Name = ");
airportName = sc.nextLine();
System.out.println("Airport Id = ");

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 9 | 51
airportId = Integer.parseInt(sc.nextLine());
System.out.println("Airport Address = ");
airportAddress = sc.nextLine();
}
void airportDisplay()
{
System.out.println("Airport Name = " + airportName);
System.out.println("Airport Id = " + airportId );
System.out.println("Airport Address = " + airportAddress);

}
}

class Arrival extends Airport implements one


{
public static int arrivalCount;
String planeName;
int planeId;
String arrivalTime;

public void arrivalValues()


{
getAirport();
System.out.println("\nPlane Arrival Details");
Scanner sc = new Scanner(System.in);
System.out.println("Plane Name = ");
planeName = sc.nextLine();
System.out.println("Plane Id = ");
planeId = Integer.parseInt(sc.nextLine());
System.out.println("Plane Arrival Time = ");
arrivalTime = sc.nextLine();
++arrivalCount;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 10 | 51
}
public void arrivalDisplay()
{
airportDisplay();
System.out.println("Plane Name = " + planeName);
System.out.println("Plane Id = " + planeId);
System.out.println("Plane Arrival Time = " + arrivalTime);
System.out.println("Total Plane Arrivals at Airport = " + arrivalCount);

}
}
class Departure extends Airport implements two
{
static int departureCount;
String planeName;
int planeId;
String departureTime;

public void departureValues()


{
Scanner sc = new Scanner(System.in);
getAirport();
System.out.println("\nPlane Departure Details");
System.out.println("Plane Name = ");
planeName = sc.nextLine();
System.out.println("Plane Id = ");
planeId = Integer.parseInt(sc.nextLine());
System.out.println("Plane Departure Time = ");
departureTime = sc.nextLine();
++departureCount;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 11 | 51
public void departureDisplay()
{
airportDisplay();
System.out.println("Plane Name = " + planeName);
System.out.println("Plane Id = " + planeId);
System.out.println("Plane Departure Count = " + departureTime);
System.out.println("Total Plane Departure = " + departureCount);

}
}

class Passenger {
public static int passCount =0;
String passName;
int passId;
String passAddress;

void passenger_setValues() {
System.out.println("\nPassenger Arrival Details");
Scanner sc = new Scanner(System.in);
System.out.println("Passenger Name = ");
passName = sc.nextLine();
System.out.println("Passenger Id = ");
passId = Integer.parseInt(sc.nextLine());
System.out.println("Passenger Address = ");
passAddress = sc.nextLine();
++passCount;
}
void passenger_display ()
{
System.out.println("\nPassenger Arrival Details");
System.out.println("Passenger Name = " + passName);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 12 | 51
System.out.println("Passenger Id = " +passId);
System.out.println("Passenger Address = "+ passAddress);
System.out.println("Total Passenger = "+ passCount);
}
}

public class Practical3 {


public static void main(String args[])
{
Arrival s1[] = new Arrival[2];
Passenger P1[] = new Passenger[100];
Scanner input = new Scanner(System.in);
System.out.println("Arrival Planes Details");
int i = 0, j, pi = 0;
while (true) {
s1[i] = new Arrival();
s1[i].arrivalValues();
System.out.println("\nArrived Passenger Details");
while (true) {
P1[pi] = new Passenger();
P1[pi].passenger_setValues();
System.out.println("\n\nDo you want to Enter More Passenger Details =
(y/n");
String dec1 = input.next();
if (dec1.equals("y")) {
++pi;
continue;
} else
break;
}

System.out.println("\n\nDo you want to Enter More Plane arrivals = (y/n");


String dec2 = input.next();

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 13 | 51
if (dec2.equals("y")) {
++i;
continue;
} else
break;
}

/*Plane Departure Details Module */

System.out.println("\n\nPlane Departure Details");


Departure s2 [] = new Departure[2];
Scanner input1 = new Scanner(System.in);
int k = 0;
while (true)
{
s2[k] = new Departure();
s2[k].departureValues();
System.out.println("\n\nDo you want to Enter More departures = (y/n");
String dec2 = input1.next();
if (dec2.equals("y") && (k <= i)) {
++k;
continue;
}
else
break;
}

// Departure Details
System.out.println("\nArrival Details");
for (j = 0; j <= i; ++j)
s1[j].arrivalDisplay();
for (int t = 0; t <= pi; ++t)

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 14 | 51
P1[t].passenger_display();
// **************************************************************
System.out.println("\nDeparture Details");
for ( int t = 0;t<=k; ++t)
s2[t].departureDisplay();

// **************************************************************
}
}

Output
Arrival Planes Details
Please Enter Airport Details
Airport Name =
Indria Gandhi
Airport Id =
1001
Airport Address =
New Delhi

Plane Arrival Details


Plane Name =
Jet Airways
Plane Id =
2001
Plane Arrival Time =
10:45

Arrived Passenger Details

Passenger Arrival Details


Passenger Name =
Ram Singh

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 15 | 51
Passenger Id =
3001
Passenger Address =
Delhi

Do you want to Enter More Passenger Details = (y/n


n

Do you want to Enter More Plane arrivals = (y/n


n

Plane Departure Details


Please Enter Airport Details
Airport Name =
Bhagat Singh
Airport Id =
1002
Airport Address =
Chandigarh

Plane Departure Details


Plane Name =
spice Jet
Plane Id =
1003
Plane Departure Time =
10:56

Do you want to Enter More departures = (y/n


n

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 16 | 51
Arrival Details
Airport Name = Indria Gandhi
Airport Id = 1001
Airport Address = New Delhi
Plane Name = Jet Airways
Plane Id = 2001
Plane Arrival Time = 10:45
Total Plane Arrivals at Airport = 1

Passenger Arrival Details


Passenger Name = Ram Singh
Passenger Id = 3001
Passenger Address = Delhi
Total Passenger = 1

Departure Details
Airport Name = Bhagat Singh
Airport Id = 1002
Airport Address = Chandigarh
Plane Name = spice Jet
Plane Id = 1003
Plane Departure Count = 10:56
Total Plane Departure = 1

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 17 | 51
Program No. - 4
Create a whole menu driven hospital management system using concept of OOP like
classes, inheritance. Include information about the following:
a. Patient -name, registration id, age, disease, etc.
b. Staff – id, name, designation, salary, etc.
.
import java.util.*;
import java.lang.*;

class Hospital
{
String hospitalName;
int hospitalId;
String hospitalAddress;
void getHospital()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter Hospital Details");
System.out.println("Hospital Name = ");
hospitalName = sc.nextLine();
System.out.println("Hospital Id = ");
hospitalId = Integer.parseInt(sc.nextLine());
System.out.println("Hospital Address = ");
hospitalAddress = sc.nextLine();
}
void hospitalDisplay()
{
System.out.println("Hospital Id = " + hospitalId);
System.out.println("Hospital name = " + hospitalName );
System.out.println("Hospital Address = " + hospitalAddress);

}
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 18 | 51
class Staff extends Hospital
{
public static int staffCount;
int staffId;
String staffName;
String staffDesg;
float salary;

public void staff_Values()


{
System.out.println("\nHospital Staff Details");
Scanner sc = new Scanner(System.in);
System.out.println("Staff Name = ");
staffName = sc.nextLine();
System.out.println("Staff Id = ");
staffId = Integer.parseInt(sc.nextLine());
System.out.println("Staff Designation = ");
staffDesg = sc.nextLine();
System.out.println("Staff salary = ");
salary = sc.nextFloat();
++staffCount;

}
public void staff_display()
{
System.out.println("Staff ID = " + staffId);
System.out.println("Staff Name = " + staffName);
System.out.println("Staff Designation = " + staffDesg);
System.out.println("Staff Salary = " + salary);
System.out.println("Staff Count = " + staffCount);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 19 | 51
}
}
class Patient extends Hospital
{
static int patientCount;
String patientName;
int patientId;
int patientAge;
String patientDisease;

public void patient_Values()


{
Scanner sc = new Scanner(System.in);
System.out.println("\nAdmitted Patient Details");
System.out.println("Patient Name = ");
patientName = sc.nextLine();
System.out.println("Patient Id = ");
patientId = Integer.parseInt(sc.nextLine());
System.out.println("Patient Age = ");
patientAge = Integer.parseInt(sc.nextLine());
System.out.println("Patient Disease = ");
patientDisease = sc.nextLine();
++patientCount;

}
public void patient_Display()
{
System.out.println("Patient Id = " + patientId);
System.out.println("Patient Name = " + patientName);
System.out.println("Patient Age = " + patientAge);
System.out.println("Patient Disease = " + patientDisease);
System.out.println("Total Admitted Patient = " + patientCount);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 20 | 51
}
}

public class Practical4 {


public static void main(String args[])
{
Hospital h1 = new Hospital();
Staff s1[] = new Staff[50];
Patient p1[] = new Patient[100];
Scanner sc = new Scanner(System.in);
System.out.println("Hospital Details");
h1.getHospital();
int i = 0, j, pi = 0, choice;
System.out.println("\n\n");
while (true) {
System.out.println(" ****************** Hospital Management Menu
********************");
System.out.println(" 1. Hospital Particulars ");
System.out.println(" 2. Staff Details ");
System.out.println(" 3. Patient Details ");
System.out.println(" 4. Display Staff Details ");
System.out.println(" 5. Display Patient Details ");
System.out.println(" 6. Exit ");
System.out.println("\n\n Please Enter a choice");
choice = sc.nextInt();
switch (choice) {
case 1:
h1.hospitalDisplay();
break;
case 2:
s1[i] = new Staff();

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 21 | 51
s1[i].staff_Values();
++i;
break;
case 3:
p1[pi] = new Patient();
p1[pi].patient_Values();
++pi;
break;
case 4:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nStaff Details");
for (j = 0; j < i; ++j)
s1[j].staff_display();
break;
case 5:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nPatient Details");
for (int k = 0; k < pi; ++k)
p1[k].patient_Display();
break;
case 6:
System.exit(0);

}
}
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 22 | 51
Output
Hospital Details
Please Enter Hospital Details
Hospital Name =
Govt Civil Hospital Kurukshetra
Hospital Id =
1002
Hospital Address =
Kurukshetra

****************** Hospital Management Menu ********************


1. Hospital Particulars
2. Staff Details
3. Patient Details
4. Display Staff Details
5. Display Patient Details
6. Exit

Please Enter a choice


1
Hospital Id = 1002
Hospital name = Govt Civil Hospital Kurukshetra
Hospital Address = Kurukshetra

Please Enter a choice


2
Hospital Staff Details
Staff Name =
Mohan
Staff Id =
2
Staff Designation =
Java Programming Lab Manual by Dr. Surender Kumar
P a g e 23 | 51
Clerk
Staff salary =
19000
Please Enter a choice
3
Admitted Patient Details
Patient Name =
Raman
Patient Id =
1000
Patient Age =
18
Patient Disease =
Fever

Please Enter a choice


5
Patient Details
Patient Id = 1000
Patient Name = Raman
Patient Age = 18
Patient Disease = Fever
Total Admitted Patient = 1

Please Enter a choice


4
Staff Details
Staff ID = 2
Staff Name = Mohan
Staff Designation = Clerk
Staff Salary = 19000.0
Staff Count = 1

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 24 | 51
Program No - 5
Create a class called Musicians to contain three methods string ( ), wind ( ) and perc
( ). Each of these methods should initialize a string array to contain the following
instruments:
 veena, guitar, sitar, sarod and mandolin under string ( )
 flute, clarinet saxophone, nadhaswaram and piccolo under wind ( )
 tabla, mridangam, bangos, drums and tambour under perc ( )

It should also display the contents of the arrays that are initialized. Create a derived
class called TypeInsto contain a method called get ( ) and show ( ). The get ( ) method
must display a means as follows. Type of instruments to be displayed:
a. String instruments
b. wind instruments
c. Percussion instruments
The show ( ) method should display the relevant detail according to our choice. The
base class variables must be accessible only to its derived classes.

import java.util.Scanner;
import java.lang.*;
class Musicians
{
String per[] = new String[5];
String win[] = new String [5];
String stang[] = new String [5];

void sting()
{
stang[0] = "Veena";
stang[1] = "Guitar";
stang[2] = "Sitar";
stang[3] = "Sarod";
stang[4] = "Mando";

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 25 | 51
}
void wind()
{
win[0] = "Flute";
win[1] = "Clarinet";
win[2] = "Saxophone";
win[3] = "Nadhaswaram";
win[4]= "piccole";
}
void perc()
{

per[0] = "Tabla";
per[1] = "Mrindangam";
per[2] = "Bangos";
per[3] = "Drums";
per[4] = "Tambour";
}
void show_sting()
{

for(int i =0;i <5; ++i)


System.out.println(stang[i]);
}
void show_wind()
{

for(int j =0;j <5; ++j)


System.out.println(win[j]);
}
void show_perc()
{
for(int k =0;k <5; ++k)

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 26 | 51
System.out.println(per[k]);
}
}

public class Practical5 {


public static void main(String args[]) {
{

Musicians s2 = new Musicians();


Scanner sc = new Scanner(System.in);

while (true) {
System.out.println("\n\n");
System.out.println(" 1. String Instruments");
System.out.println(" 2. Wind Instruments");
System.out.println(" 3. Percussions Instruments");
System.out.println(" 4. Exit");
System.out.println("\n\nPlease enter your choice");
int ch = sc.nextInt();
switch (ch) {
case 1:
s2.sting();
s2.show_sting();
break;
case 2:
s2.wind();
s2.show_wind();
break;
case 3:
s2.perc();
s2.show_perc();
break;
case 4:

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 27 | 51
System.exit(0);
}
}
}
}
}

Output:
1. String Instruments
2. Wind Instruments
3. Percussions Instruments
4. Exit

Please enter your choice


1
Veena
Guitar
Sitar
Sarod
Mando

Please enter your choice


2
Flute
Clarinet
Saxophone
Nadhaswaram
Piccolo

Please enter your choice


3
Tabla
Mrindangam

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 28 | 51
Bangos
Drums
Tambour

Please enter your choice


4

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 29 | 51
Program No. - 6
Write three derived classes inheriting functionality of base class person (should have
a member function that ask to enter name and age) and with added unique features
of student, and employee, and functionality to assign, change and delete records of
student and employee. */

import java.util.*;
import java.lang.*;

class Person
{
String Name;
int age;
void person_detail()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter Person Name =");
Name = sc.nextLine();
System.out.println("Age = ");
age = Integer.parseInt(sc.nextLine());

}
void person_display()
{
System.out.println("Person Name = " + Name);
System.out.println("Age = " + age );
}
}

class Employees extends Person


{
public static int empCount;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 30 | 51
String deptName;
float salary;

public void staff_details()


{
System.out.println("\n Enter Staff Details");
person_detail();
Scanner sc = new Scanner(System.in);
System.out.println("Department Name = ");
deptName = sc.nextLine();
System.out.println("Staff salary = ");
salary = sc.nextFloat();
++empCount;

}
public void staff_display()
{
System.out.println("\nStaff Details");
person_display();
System.out.println("Department Name = " + deptName);
System.out.println("Staff Salary = " + salary);
System.out.println("Staff Count = " + empCount);
}
}
class Student1 extends Person
{
static int stuCount;
String rollno;
float marks;

public void student_details()


{
Scanner sc = new Scanner(System.in);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 31 | 51
System.out.println("\n Student Details");
person_detail();
System.out.println("Roll No = ");
rollno = sc.nextLine();
System.out.println("Marks = ");
marks = sc.nextFloat();
++stuCount;
}
public void student_display()
{
System.out.println("\nStudent Details");
System.out.println("Student Roll No = " + rollno);
System.out.println("Student Marks = " + marks);
System.out.println("Total Students = " + stuCount);

}
}

public class Practical6 {


public static void main(String args[])
{
Employees E1[] = new Employees[50];
Student1 S1[] = new Student1[100];
Scanner sc = new Scanner(System.in);
int i = 0, j, pi = 0, choice;
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\n\n");
while (true) {
System.out.println(" ****************** Person Management Menu
********************");
System.out.println(" 1. Staff Details ");

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 32 | 51
System.out.println(" 2. Student Details ");
System.out.println(" 3. Display Staff Details ");
System.out.println(" 4. Display Student Details ");
System.out.println(" 5. Exit ");
System.out.println("\n\n Please Enter a choice");
choice = sc.nextInt();
switch (choice) {
case 1:
E1[i] = new Employees();
E1[i].staff_details();
++i;
break;
case 2:
S1[pi] = new Student1();
S1[pi].student_details();
++pi;
break;
case 3:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nStaff Details");
for (j = 0; j < i; ++j)
E1[j].staff_display();
break;
case 4:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nStudent Details");
for (int k = 0; k < pi; ++k)
S1[k].student_display();
break;
case 5:
System.exit(0);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 33 | 51
}

}
}
}

Output:
****************** Person Management Menu ********************
1. Staff Details
2. Student Details
3. Display Staff Details
4. Display Student Details
5. Exit

Please Enter a choice


1

Enter Staff Details


Please Enter Person Name =
Manoj
Age =
25
Department Name =
office
Staff salary =
18000
Please Enter a choice
3

Staff Details

Staff Details

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 34 | 51
Person Name = Manoj
Age = 25
Department Name = office
Staff Salary = 18000.0
Staff Count = 1
Please Enter a choice
2

Student Details
Please Enter Stydent Name =
Aryan
Age =
16
Roll No =
221630800004
Marks =
88

Please Enter a choice


4

Student Details

Student Details
Student Roll No = 221630800004
Student Marks = 88.0
Total Students = 1
Please Enter a choice
5

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 35 | 51
Program No - 7
Using the concept of multiple inheritance create classes: Shape, Circle, Square, Cube,
Sphere, Cylinder. Your classes may only have the class variable specified in the table
below and the methods Area and/or Volume to output their area and/or volume.
Class Class Variable Constructor Base class
Shape String name Shape()
Circle double radius Circle( double r, String n ) Shape
Square double side Square( double s, String n ) Shape
Cylinder double heigh Cylinder(double h, double r, String n ) Circle
Sphere None Sphere( double r, String n ) Circle
Cube None Cube( double s, String n ) Square

class Shape
{
String name;
double area;
Shape()
{
name = "";
area = 0.0;
}
void shape_show()
{
System.out.println("Shape" + name);
}
}
class Circle extends Shape {
double r;

Circle(double r1, String n) {


r = r1;
name = n;
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 36 | 51
void circle_area() {
area = 3.14 * r * r;
System.out.println("\n");
System.out.println("Radius of the Circle ="+ r);
System.out.println("Area of the Circle ="+ area);
}
}

class Square extends Shape


{
double side;

Square(double s, String n1)


{
side = s;
name = n1;
}
void square_area()
{
area = side * side;
System.out.println("\n");
System.out.println("Side of the Square ="+ side);
System.out.println("Area of the Square ="+ area);
}
}
class Triangle extends Shape
{
double b,h;

Triangle(double base,double height, String n2)


{
b = base;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 37 | 51
h = height;
name = n2;
}
void triangle_area()
{
area = (b*h)/2.0;
System.out.println("\n");
System.out.println("Base of the Triangle = "+ b);
System.out.println("Height of the Triangle = "+ h);
System.out.println("Area of the Triangle = "+ area);
}
}
class Cylinder extends Shape
{
double h,r;

Cylinder(double h,double r,String n3)


{
this.h = h;
this.r = r;
name = n3;
}
void Cylinder_area()
{
area = 2.0 * 3.14 * r*h + 2.0*3.14 * r * r;
System.out.println("Height of the Cylinder ="+ h);
System.out.println("Radius of the Cylinder ="+ r);
System.out.println("Area of the Cylinder ="+ area);
}
}
public class Practical7 {
public static void main(String args[])
{

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 38 | 51
Triangle t1 = new Triangle(4.0,5.0, "Triangle");
t1.triangle_area();
Square s1 = new Square(12.0,"Square");
s1.square_area();
Circle c1 = new Circle(2.5,"Circle");
c1.circle_area();
Cylinder cy1 = new Cylinder(2.5,3.5,"Cylinder");
cy1.Cylinder_area();
}
}
Output:
Base of the Triangle = 4.0
Height of the Triangle = 5.0
Area of the Triangle = 10.0

Side of the Square =12.0


Area of the Square =144.0

Radius of the Circle =2.5


Area of the Circle =19.625
Height of the Cylinder =2.5
Radius of the Cylinder =3.5
Area of the Cylinder =131.88

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 39 | 51
Program No. - 8
Write a program to create class Person.
a. Make two classes, Student and Instructor, inherit from Person. A person has a name
and year of birth.
b. A student has a major, student id.
c. An instructor has salary, subject.
Write the class definitions, the constructors, set methods, get methods and for all
classes.

import java.util.*;
import java.lang.*;

class Person1
{
String Name;
int birthyear;
void getperson_detail()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter Person Name =");
Name = sc.nextLine();
System.out.println("Birth Year = ");
birthyear = Integer.parseInt(sc.nextLine());

}
void setperson_display()
{
System.out.println("Person Name = " + Name);
System.out.println("Birth Year = " + birthyear );
}
}

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 40 | 51
class Instructor extends Person1
{
public static int insCount;
String subjName;
float salary;

public void getinstructor_details()


{
System.out.println("\n Enter Instructor Details");
getperson_detail();
Scanner sc = new Scanner(System.in);
System.out.println("Subject of Instructor = ");
subjName = sc.nextLine();
System.out.println("Instructor salary = ");
salary = sc.nextFloat();
++insCount;

}
public void setinstructor_display()
{
System.out.println("\nInstructor Details");
setperson_display();
System.out.println("Department Name = " + subjName);
System.out.println("Instructor Salary = " + salary);
System.out.println("Total Instructors = " + insCount);

}
}
class Student2 extends Person1
{
static int stuCount;
int studentid;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 41 | 51
String major;

public void getstudent_details()


{
Scanner sc = new Scanner(System.in);
System.out.println("\n Student Details");
getperson_detail();
System.out.println("Student Id = ");
studentid = sc.nextInt();
System.out.println("Major = ");
major = sc.nextLine();
++stuCount;
}
public void setstudent_display()
{
System.out.println("\nStudent Details");
setperson_display();
System.out.println("Student Id = " + studentid);
System.out.println("Major Name = " + major);
System.out.println("Total Students = " + stuCount);

}
}

public class Practical8 {


public static void main(String args[])
{
Instructor E1[] = new Instructor[50];
Student2 S1[] = new Student2[100];
Scanner sc = new Scanner(System.in);
int i = 0, j, pi = 0, choice;
System.out.print("\033[H\033[2J");

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 42 | 51
System.out.flush();
System.out.println("\n\n");
while (true) {
System.out.println(" ****************** Main Menu ********************");
System.out.println(" 1. Instructor Details ");
System.out.println(" 2. Student Details ");
System.out.println(" 3. Display Instructor Details ");
System.out.println(" 4. Display Student Details ");
System.out.println(" 5. Exit ");
System.out.println("\n\n Please Enter a choice");
choice = sc.nextInt();
switch (choice) {
case 1:
E1[i] = new Instructor();
E1[i].getinstructor_details();
++i;
break;
case 2:
S1[pi] = new Student2();
S1[pi].getstudent_details();
++pi;
break;
case 3:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nInstructor Details");
for (j = 0; j < i; ++j)
E1[j].setinstructor_display();
break;
case 4:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nStudent Details");

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 43 | 51
for (int k = 0; k < pi; ++k)
S1[k].setstudent_display();
break;
case 5:
System.exit(0);
}
}
}
}

Output:
****************** Main Menu ********************
1. Instructor Details
2. Student Details
3. Display Instructor Details
4. Display Student Details
5. Exit

Please Enter a choice


1

Enter Instructor Details


Please Enter Person Name =
Rajan
Birth Year =
1980
Subject of Instructor =
BEE
Instructor salary =
20000
Please Enter a choice
3

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 44 | 51
Instructor Details

Instructor Details
Person Name = Rajan
Birth Year = 1980
Department Name = BEE
Instructor Salary = 20000.0
Total Instructors = 1

Please Enter a choice


2

Student Details
Please Enter Person Name =
Rajan
Birth Year =
1998
Major =
Computer science
Student Id =
145
Please Enter a choice
4

Student Details

Student Details
Person Name = Rajan
Birth Year = 1998
Student Id = 145
Major Name = Computer science
Total Students = 1

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 45 | 51
Program No. - 9
Old MacDonald had a farm and several types of animals. Every animal shared certain
characteristics: they had a type (such as cow, chick or pig) and each made a sound
(moo, cluck or oink). An Interface define those things required to be an animal on the
farm. Define new classes for the Old MacDonald that implement the Animal and Farm
class. Create array of object of animal to define the different types of animal in the
farm. Also create appropriate methods to get and set the properties.

interface Animal
{
public void setanimaltype(String animal);
public String getanimaltype();

public void setsound(String sound);


public String getsound();
}
class Cow implements Animal
{
String type,sound;
public void setanimaltype(String animal)
{
type = animal;
}
public String getanimaltype()
{
return type;
}
public void setsound(String sound)
{
this.sound = sound;
}
public String getsound()
{

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 46 | 51
return sound;
}
}

class Chick implements Animal


{
String type,sound;
public void setanimaltype(String animal)
{
type = animal;
}
public String getanimaltype()
{
return type;
}
public void setsound(String sound)
{
this.sound = sound;
}
public String getsound()
{
return sound;
}
}
public class Practical9 {
public static void main(String args[])
{
Animal a1[] = new Animal[2];
a1[0] = new Cow();
a1[0].setanimaltype("Cow");
a1[0].getanimaltype();
a1[0].setsound("Moo");
a1[0].getsound();

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 47 | 51
a1[1] = new Chick();
a1[1].setanimaltype("Chick");
a1[1].getanimaltype();
a1[1].setsound("Cluck");
a1[1].getsound();
for (int i=0; i<2;++i)
{
System.out.println(" Animal Name = "+ a1[i].getanimaltype());
System.out.println(" Animal Sound = "+ a1[i].getsound());
System.out.println("\n");
}
}
}

Output:
Animal Name = Cow
Animal Sound = Moo

Animal Name = Chick


Animal Sound = Cluck

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 48 | 51
Program No. - 10
Write a program with Student as abstract class and create derive classes Engineering,
Medicine and Science from base class Student. Create the objects of the derived
classes and process them and access them using array of pointer of type base class
Student. */

abstract class Students


{
private String name;
private String stream;

public Students(String name,String stream)


{
this.name = name;
this.stream = stream;
}

abstract void marks();

public String toString()


{
System.out.println("\n");
return "Name = " + name + "::Stream ="+ stream;
}
}

class Engineering extends Students


{
private int marks;

Engineering(String name,String stream,int marks)


{
super(name,stream);

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 49 | 51
this.marks = marks;
}
void marks()
{
System.out.println("Marks = "+marks);
}

class Medicine extends Students


{
private int marks;

Medicine(String name,String stream,int marks)


{
super(name,stream);
this.marks = marks;
}
void marks()
{
System.out.println("Marks = "+marks);
}

class Science extends Students


{
private int marks;

Science(String name,String stream,int marks)


{
super(name,stream);
this.marks = marks;

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 50 | 51
}
void marks()
{
System.out.println("Marks = "+marks);
}

}
class Practical10 {
public static void main(String args[])
{
Students s[] = new Students[3];
s[0] = new Engineering("Rakesh", "Engineering",425);
s[1] = new Engineering("Raman", "Medicine",475);
s[2] = new Engineering("Sunil", "Science",450);

for (int i = 0; i <3; ++i)


{
System.out.println(s[i].toString());
s[i].marks();
}
}
}

Output:
Name = Rakesh::Stream =Engineering
Marks = 425

Name = Raman::Stream =Medicine


Marks = 475

Name = Sunil::Stream =Science


Marks = 450

Process finished with exit code 0

Java Programming Lab Manual by Dr. Surender Kumar


P a g e 51 | 51

You might also like