LAB Assignment 5
1 : Online Shopping System: Create a class Bill with an overloaded method generateBill():
One version takes int quantity, double price.
Another version takes int quantity, double price, double discount.
Demonstrate bill calculation using both methods.
import [Link].*;
public class Bill{
public void generateBill(int quant, double price) throws IOException
{
[Link]("Total bill is " + quant*price);
}
public void generateBill (int quan,double price,double disc) throws IOException
{
double total=quan*price;
total=total - (( total * disc)/100);
[Link]("Total bill is " + total);
}
public static void main(String args[]) throws IOException{
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter quantity");
int quant=[Link]([Link]());
[Link]("Enter price");
double price=[Link]([Link]());
[Link]("Enter discount");
double disc=[Link]([Link]());
Bill obj=new Bill();
[Link](quant,price);
[Link](quant,price,disc);
}
}
Himanshi Jain 23 23124040
2 : Banking Transactions: Create a class Transaction with overloaded methods transferCal():
transferCal(String fromAccount, String toAccount, double amount)
transferCal(String fromAccount, String toAccount, double amount, String note)
Show how both versions can be used in real-world banking.
import [Link].*;
public class Transaction{
public void transferCal(String fromAccount, String toAccount, double amount) throws IOException
{
[Link]("Money Transffered from " + fromAccount + " to " + toAccount
+ " is " + amount);
}
public void transferCal (String fromAccount, String toAccount, double amount, String note) throws
IOException
{
[Link]("Money Transffered from " + fromAccount + " to " + toAccount
+ " is " + amount + " with note : " + note);
}
public static void main(String args[]) throws IOException{
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter fromAccount");
String fromAccount=([Link]());
[Link]("Enter toAccount");
String toAccount=([Link]());
[Link]("Enter ammount");
double ammount=[Link]([Link]());
[Link]("Enter note");
Himanshi Jain 24 23124040
String note=[Link]();
Transaction obj=new Transaction();
[Link](fromAccount,toAccount,ammount);
[Link](fromAccount,toAccount,ammount,note);
3 : Hospital Management System : Create a class Appointment with overloaded methods book():
book(String doctorName, String patientName)
book(String doctorName, String patientName, String date)
book(String doctorName, String patientName, String date, String time)
import [Link].*;
public class Appointment{
public void book(String doctorName, String patientName) throws IOException
{
[Link](" Appointent from " + doctorName + " by " + patientName);
}
public void book(String doctorName, String patientName, String date) throws IOException
{
[Link](" Appointent from " + doctorName + " by " + patientName + " on " +
date);
}
public void book(String doctorName, String patientName, String date, String time) throws IOException
{
[Link](" Appointent from " + doctorName + " by " + patientName + " on " +
date +" at " + time );
}
Himanshi Jain 25 23124040
public static void main(String args[]) throws IOException{
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter doctorName");
String doctorName=([Link]());
[Link]("Enter patientName");
String patientName=([Link]());
[Link]("Enter date");
String date=([Link]());
[Link]("Enter time");
String time=[Link]();
Appointment obj=new Appointment ();
[Link](doctorName, patientName);
[Link](doctorName, patientName,date);
[Link](doctorName, patientName ,date, time);
Himanshi Jain 26 23124040
4 : E-Learning Platform: Create a class Course with overloaded methods enroll():
enroll(String studentName)
enroll(String studentName, String courseName)
enroll(String studentName, String courseName, String batch)
import [Link].*;
public class Course{
public void enroll(String studentName) throws IOException
{
[Link](" Student enrolled " + studentName );
}
public void enroll(String studentName, String courseName) throws IOException
{
[Link](" Student enrolled " + studentName + " in " + courseName);
}
public void enroll(String studentName, String courseName, String batch) throws IOException
{
[Link](" Student enrolled " + studentName + " in " + courseName + " in " +
batch);
public static void main(String args[]) throws IOException{
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter studentName");
String studentName=([Link]());
[Link]("Enter courseName");
String courseName=([Link]());
[Link]("Enter batch");
String batch=([Link]());
Course obj=new Course();
[Link](studentName);
[Link](studentName,courseName);
[Link](studentName,courseName,batch);
Himanshi Jain 27 23124040
}
5 : Travel Booking System: Create a class Ticket with overloaded methods bookTicket():
bookTicket(String passengerName, String destination)
bookTicket(String passengerName, String destination, String travelDate)
bookTicket(String passengerName, String destination, String travelDate, String seatType)
import [Link].*;
public class Ticket{
public void bookTicket(String passengerName, String destination) throws IOException
{
[Link](" passenger booked " + passengerName + " to " + destination );
}
public void bookTicket(String passengerName, String destination, String travelDate) throws IOException
{
[Link]( " passenger booked " + passengerName + " to " + destination + " on " +
travelDate);
}
public void bookTicket(String passengerName, String destination, String travelDate, String seatType)
throws IOException
{
[Link]( " passenger booked " + passengerName + " to " + destination + " on " +
travelDate + " at seat type " + seatType);
public static void main(String args[]) throws IOException{
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter passengerName");
String passengerName=([Link]());
Himanshi Jain 28 23124040
[Link]("Enter destination ");
String destination=([Link]());
[Link]("Enter travel date");
String travelDate=([Link]());
[Link]("Enter travel seatType");
String seatType=([Link]());
Ticket obj=new Ticket();
[Link](passengerName, destination);
[Link](passengerName, destination, travelDate);
[Link](passengerName, destination, travelDate, seatType);
Himanshi Jain 29 23124040
Himanshi Jain 30 23124040
Lab Assignment 6
Q1 : Banking Transactions: Create a class Transaction with overriding methods transferCal():
transferCal(String fromAccount, String toAccount, double amount) transferCal(String
fromAccount, String toAccount, double amount) Show how both versions can be used in real
import [Link].*;
class Transaction {
void transferCal(String fromAccount, String toAccount, double amount) {
[Link]("Transaction: " + amount + " transferred from " + fromAccount + " to " + toAccount);
}
}
class BankTransfer extends Transaction {
@Override
void transferCal(String fromAccount, String toAccount, double amount) {
[Link]("Bank Transfer: " + amount + " transferred securely from " + fromAccount + "to "+
toAccount);
}
}
public class Program1 {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream([Link]);
[Link]("Enter first transaction amount: ");
double a = [Link]([Link]());
[Link]("Enter bank transfer transaction amount: ");
double b = [Link]([Link]());
Transaction t = new Transaction();
[Link]("ACC100", "ACC200", a);
Transaction bt = new BankTransfer();
[Link]("ACC300", "ACC400", b);
}
}
Himanshi Jain 31 23124040
Q2 : Hospital Management System : Create a class Appointment with overriding methods book():
book(String doctorName, String patientName, String date) book(String doctorName, String
patientName, String date) book(String doctorName, String patientName, String date)
class Booking {
void book() {
[Link]("Generic booking done");}
}
class Appointment extends Booking {
@Override
void book() {
[Link]("Appointment booked with doctor");
}
void book(String doctorName, String patientName) {
[Link]("Appointment booked for patient " + patientName + " with Dr. " +
doctorName);
}
void book(String doctorName, String patientName, String date) {
[Link]("Appointment booked for patient " + patientName + " with Dr. " +
doctorName + " on " + date);
}
}
public class AppointmentTest {
public static void main(String[] args) {
Appointment appt = new Appointment();
[Link]();
[Link]("Smith", "John");
[Link]("Smith", "John", "10-10-2025");
}
}
Himanshi Jain 32 23124040
3 : E-Learning Platform: Create a class Course with overriding methods enroll():
enroll(String studentName, String courseName)
enroll(String studentName, String courseName)
enroll(String studentName, String courseName)
class Booking {
void book() {
[Link]("Generic booking done");
}
}
class Course extends Booking {
@Override
void book() {
[Link]("Course enrollment done");
}
void enroll(String studentName, String courseName) {
[Link](studentName + " enrolled in course " + courseName);
}
void enroll(String studentName, String courseName, String startDate) {
[Link](studentName + " enrolled in course " + courseName + " starting on " +
startDate);
}
}
public class CourseTest {
public static void main(String[] args) {
Course course = new Course();
[Link]();
[Link]("Alice", "Java Programming");
[Link]("Alice", "Java Programming", "01-11-2025");
}
}
Himanshi Jain 33 23124040
4 : Travel Booking System: Create a class Ticket with overriding methods bookTicket():
bookTicket(String passengerName, String destination, String travelDate)
bookTicket(String passengerName, String destination, String travelDate)
bookTicket(String passengerName, String destination, String travelDate)
class Booking {
void book() {
[Link]("Generic booking done");
}
}
class Ticket extends Booking {
@Override
void book() {
[Link]("Ticket booking done");
}
void bookTicket(String passengerName, String destination) {
[Link]("Ticket booked for " + passengerName + " to " + destination);
}
void bookTicket(String passengerName, String destination, String travelDate) {
[Link]("Ticket booked for " + passengerName + " to " + destination + " on " +
travelDate);
}
}
public class TicketTest {
public static void main(String[] args) {
Ticket ticket = new Ticket();
[Link]();
[Link]("David", "Paris");
[Link]("David", "Paris", "15-12-2025");
}
}
Himanshi Jain 34 23124040
LAB ASSIGNMENT 7
1. Implement a CarBuilder class using this
to chain methods (e.g., setModel(),
setColor(),
setPrice()).
Then create a subclass LuxuryCarBuilder
that overrides one method but still calls the parent method using super.
import [Link].*;
class CarBuilder {
String model;
String color;
double price;
public CarBuilder setModel(String model) {
[Link] = model;
return this;
}
public CarBuilder setColor(String color) {
[Link] = color;
return this;
}
public CarBuilder setPrice(double price) {
[Link] = price;
return this;
}
public void showCarDetails() {
[Link]("Car Model: " + model);
[Link]("Car Color: " + color);
[Link]("Car Price: " + price);
}
}
class LuxuryCarBuilder extends CarBuilder {
@Override
public LuxuryCarBuilder setModel(String model) {
[Link](model);
return this;
}
Himanshi Jain 35 23124040
@Override
public LuxuryCarBuilder setColor(String color) {
[Link](color);
return this;
}
@Override
public LuxuryCarBuilder setPrice(double price) {
[Link](price + 500000);
[Link]("Luxury tax added! Final price set.");
return this;
}
}
public class Program1 {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream([Link]);
[Link]("Enter car model: ");
String model = [Link]();
[Link]("Enter car color: ");
String color = [Link]();
[Link]("Enter car price: ");
double price = [Link]([Link]());
LuxuryCarBuilder car = new LuxuryCarBuilder()
.setModel(model)
.setColor(color)
.setPrice(price);
[Link]("\n--- Car Details ---");
[Link]();
}
}
Himanshi Jain 36 23124040
[Link] two classes Device and Smartphone. Override a method showDetails() in Smartphone.
Inside it, call [Link](). Also demonstrate returning this from another method for
chaining like setBrand().setModel().
class Device {
String brand;
String model;
public Device setBrand(String brand) {
[Link] = brand;
return this;
}
public Device setModel(String model) {
[Link] = model;
return this;
}
public void showDetails() {
[Link]("Device Brand: " + brand);
[Link]("Device Model: " + model);
}
}
class Smartphone extends Device {
String os;
int cameraMP;
// Override parent methods to return Smartphone
@Override
public Smartphone setBrand(String brand) {
[Link](brand);
return this;
}
Himanshi Jain 37 23124040
@Override
public Smartphone setModel(String model) {
[Link](model);
return this;
}
public Smartphone setOS(String os) {
[Link] = os;
return this;
}
public Smartphone setCameraMP(int cameraMP) {
[Link] = cameraMP;
return this;
}
@Override
public void showDetails() {
[Link]();
[Link]("Operating System: " + os);
[Link]("Camera: " + cameraMP + " MP");
}
}
public class Program2 {
public static void main(String[] args) {
Smartphone phone = new Smartphone()
.setBrand("Apple")
.setModel("iPhone 15 Pro")
.setOS("iOS 18")
.setCameraMP(48);
[Link]("--- Smartphone Details ---");
[Link]();
}
}
Himanshi Jain 38 23124040
3. Build three classes: University → Department → Professor. Use super() to manage constructor
execution order. Inside Professor, create methods that return this to allow method chaining.
class University {
String uniName;
public University(String uniName) {
[Link] = uniName;
[Link]("University constructor called");
}
public void showUniversity() {
[Link]("University Name: " + uniName);
}
}
class Department extends University {
String deptName;
public Department(String uniName, String deptName) {
super(uniName);
[Link] = deptName;
[Link]("Department constructor called");
}
public void showDepartment() {
showUniversity();
[Link]("Department Name: " + deptName);
}
}
class Professor extends Department {
String profName;
String subject;
Himanshi Jain 39 23124040
public Professor(String uniName, String deptName) {
super(uniName, deptName);
[Link]("Professor constructor called");
}
public Professor setName(String profName) {
[Link] = profName;
return this;
}
public Professor setSubject(String subject) {
[Link] = subject;
return this;
}
public void showProfessor() {
showDepartment();
[Link]("Professor Name: " + profName);
[Link]("Subject: " + subject);
}
}
public class Program3 {
public static void main(String[] args) {
Professor prof = new Professor("Delhi University", "Computer Science")
.setName("Dr. Mehta")
.setSubject("Artificial Intelligence");
[Link]("\n--- Professor Details ---");
[Link]();
}
}
Himanshi Jain 40 23124040
4. Create a base class Shape with a method area(). Override it in subclasses Rectangle and
Circle. In one of the subclasses, call [Link]() as part of the overridden logic. Use this
to return the current object in a method like resize().
class Shape {
double area;
public double area() {
[Link]("Calculating area in Shape (base class)...");
return area;
}
}
class Rectangle extends Shape {
double length;
double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
public double area() {
[Link](); // just to show constructor/parent invocation
area = length * width;
[Link]("Rectangle area calculated using overridden method.");
return area;
}
public Rectangle resize(double factor) {
[Link] *= factor;
[Link] *= factor;
return this; // return current object for chaining
}
public void show() {
[Link]("Rectangle Length: " + length);
[Link]("Rectangle Width: " + width);
[Link]("Area: " + area());
}
}
class Circle extends Shape {
double radius;
Himanshi Jain 41 23124040
public Circle(double radius) {
[Link] = radius;
}
@Override
public double area() {
area = [Link] * radius * radius;
[Link]("Circle area calculated.");
return area;
}
public Circle resize(double factor) {
[Link] *= factor;
return this;
}
public void show() {
[Link]("Circle Radius: " + radius);
[Link]("Area: " + area());
}
}
public class Program4 {
public static void main(String[] args) {
[Link]("--- Rectangle ---");
Rectangle rect = new Rectangle(4, 5)
.resize(1.5); // method chaining
[Link]();
[Link]("\n--- Circle ---");
Circle circle = new Circle(3)
.resize(2); // method chaining
[Link]();
}
}
Himanshi Jain 42 23124040
5. Design a hierarchy Vehicle →Car → ElectricCar → AutonomousCar. Each level should have
overloaded [Link] this() to chain constructors within a class. Use super()
to call parent constructors. Show how execution flows when creating an AutonomousCar
Object.
class Vehicle {
String brand;
public Vehicle() {
[Link]("Vehicle: Default constructor called");
}
public Vehicle(String brand) {
[Link] = brand;
[Link]("Vehicle: Parameterized constructor called");
}
}
class Car extends Vehicle {
int wheels;
public Car() {
this(4);
[Link]("Car: Default constructor called");
}
public Car(int wheels) {
super();
[Link] = wheels;
Himanshi Jain 43 23124040
[Link]("Car: Parameterized constructor called");
}
}
class ElectricCar extends Car {
int batteryCapacity;
public ElectricCar() {
this(75);
[Link]("ElectricCar: Default constructor called");
}
public ElectricCar(int batteryCapacity) {
super(4);
[Link] = batteryCapacity;
[Link]("ElectricCar: Parameterized constructor called");
}
}
class AutonomousCar extends ElectricCar {
boolean autoDriveEnabled;
public AutonomousCar() {
this(true);
[Link]("AutonomousCar: Default constructor called");
}
public AutonomousCar(boolean autoDriveEnabled) {
super(100);
[Link] = autoDriveEnabled;
[Link]("AutonomousCar: Parameterized constructor called");
}
}
public class Program5 {
public static void main(String[] args) {
[Link]("=== Creating AutonomousCar object ===");
AutonomousCar car = new AutonomousCar();
}
}
Himanshi Jain 44 23124040
6. Build an : Class Product (name, price). Class Electronics (adds warranty). Class Mobile
(adds brand and model). Use super for constructor chaining across levels. Use this
for method chaining like setBrand().setModel().setWarranty().
class Product {
String name;
double price;
public Product(String name, double price) {
[Link] = name;
[Link] = price;
[Link]("Product constructor called");
}
public void showProduct() {
[Link]("Product Name: " + name);
[Link]("Price: $" + price);
}
}
class Electronics extends Product {
int warranty; // in months
public Electronics(String name, double price, int warranty) {
super(name, price); // call Product constructor
[Link] = warranty;
[Link]("Electronics constructor called");
}
public Electronics setWarranty(int warranty) {
[Link] = warranty;
return this;
}
Himanshi Jain 45 23124040
public void showElectronics() {
showProduct();
[Link]("Warranty: " + warranty + " months");
}
}
class Mobile extends Electronics {
String brand;
String model;
public Mobile(String name, double price, int warranty, String brand, String model) {
super(name, price, warranty); // call Electronics constructor
[Link] = brand;
[Link] = model;
[Link]("Mobile constructor called");
}
public Mobile setBrand(String brand) {
[Link] = brand;
return this;
}
public Mobile setModel(String model) {
[Link] = model;
return this;
}
@Override
public Mobile setWarranty(int warranty) {
[Link](warranty);
return this;
}
public void showMobile() {
showElectronics();
[Link]("Brand: " + brand);
[Link]("Model: " + model);
}
}
public class Program6 {
public static void main(String[] args) {
[Link]("=== Creating Mobile Object ===");
Mobile phone = new Mobile("Smartphone", 79999.0, 24, "Samsung", "Galaxy S25")
.setBrand("Samsung")
.setModel("Galaxy S25 Ultra")
Himanshi Jain 46 23124040
.setWarranty(36); // method chaining across levels
[Link]("\n--- Mobile Details ---");
[Link]();
}
}
Himanshi Jain 47 23124040