Java Programming Lab Manual-1
Java Programming Lab Manual-1
- 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 display()
{
System.out.println("\nModel =" +model+"\nYear =" +year+"\nColour ="
+colour+"\nTop speed =" +topSpeed);
}
}
Output:
Model =Santro
Year =2020
Colour =white
Top speed =140
No of Units Sold= 20
Model =Xing
Year =2021
Colour =Red
Top speed =120
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
import java.util.*;
class Employee
{
String name;
int id;
}
}
void 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) ");
}
}
}
}
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
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
import java.util.*;
import java.lang.*;
interface one {
public void arrivalValues();
}
}
}
}
class Departure extends Airport implements two
{
static int departureCount;
String planeName;
int planeId;
String departureTime;
}
}
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);
// Departure Details
System.out.println("\nArrival Details");
for (j = 0; j <= i; ++j)
s1[j].arrivalDisplay();
for (int t = 0; t <= pi; ++t)
// **************************************************************
}
}
Output
Arrival Planes Details
Please Enter Airport Details
Airport Name =
Indria Gandhi
Airport Id =
1001
Airport Address =
New Delhi
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
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);
}
}
}
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);
}
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);
}
}
}
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";
per[0] = "Tabla";
per[1] = "Mrindangam";
per[2] = "Bangos";
per[3] = "Drums";
per[4] = "Tambour";
}
void show_sting()
{
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:
Output:
1. String Instruments
2. Wind Instruments
3. Percussions Instruments
4. Exit
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 );
}
}
}
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;
}
}
}
}
}
Output:
****************** Person Management Menu ********************
1. Staff Details
2. Student Details
3. Display Staff Details
4. Display Student Details
5. Exit
Staff Details
Staff Details
Student Details
Please Enter Stydent Name =
Aryan
Age =
16
Roll No =
221630800004
Marks =
88
Student Details
Student Details
Student Roll No = 221630800004
Student Marks = 88.0
Total Students = 1
Please Enter a choice
5
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;
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 );
}
}
}
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;
}
}
Output:
****************** Main Menu ********************
1. Instructor Details
2. Student Details
3. Display Instructor Details
4. Display Student Details
5. Exit
Instructor Details
Person Name = Rajan
Birth Year = 1980
Department Name = BEE
Instructor Salary = 20000.0
Total Instructors = 1
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
interface Animal
{
public void setanimaltype(String animal);
public String getanimaltype();
Output:
Animal Name = Cow
Animal Sound = Moo
}
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);
Output:
Name = Rakesh::Stream =Engineering
Marks = 425