0% found this document useful (0 votes)
2 views3 pages

Polymorphism Assignment Programs

The document outlines six programming problems related to object-oriented programming concepts such as method overloading, method overriding, dynamic method dispatch, upcasting, downcasting, and safe downcasting. Each problem includes class definitions and methods that demonstrate these concepts in practical scenarios, such as a hotel booking system, an online learning platform, a transportation fleet management system, a hospital management system, a digital art gallery, and a smart home automation system. The examples illustrate the use of inheritance and polymorphism in Java.

Uploaded by

cbc8yd5vz6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Polymorphism Assignment Programs

The document outlines six programming problems related to object-oriented programming concepts such as method overloading, method overriding, dynamic method dispatch, upcasting, downcasting, and safe downcasting. Each problem includes class definitions and methods that demonstrate these concepts in practical scenarios, such as a hotel booking system, an online learning platform, a transportation fleet management system, a hospital management system, a digital art gallery, and a smart home automation system. The examples illustrate the use of inheritance and polymorphism in Java.

Uploaded by

cbc8yd5vz6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem 1 — Hotel Booking System (Method Overloading)

public class Problem1_HotelBooking {


static class Hotel {
private double baseRate(String roomType) {
switch ([Link]()) {
case "suite": return 500;
case "deluxe": return 300;
default: return 150; // standard
}
}

public void book(String roomType, int nights) { ... }


public void book(String roomType, int nights, double seasonalMultiplier) { ... }
public void book(String roomType, int nights, double corporateDiscountPercent, double mealPackagePer
public void book(String roomType, int nights, int guestCount, double decorationFee, double cateringP
}

public static void main(String[] args) {


Hotel h = new Hotel();
[Link]("Deluxe", 3);
[Link]("Suite", 2, 1.25);
[Link]("Standard", 5, 10.0, 12.0);
[Link]("Suite", 2, 120, 1500.0, 500.0);
}
}

Problem 2 — Online Learning Platform (Method Overriding)


abstract class Course {
String title;
String instructor;
Course(String t, String i) { title = t; instructor = i; }
public abstract void displayProgress();
}

class VideoCourse extends Course {


double completionPercent;
int minutesWatched;
public void displayProgress() { ... }
}

class InteractiveCourse extends Course { ... }


class ReadingCourse extends Course { ... }
class CertificationCourse extends Course { ... }

public class Problem2_OnlineLearning {


public static void main(String[] args) {
Course c1 = new VideoCourse("Java", "Singh");
Course c2 = new InteractiveCourse("Robotics", "Patel");
[Link]();
[Link]();
}
}

Problem 3 — Transportation Fleet Management (Dynamic Method


Dispatch)
abstract class Vehicle {
String id;
Vehicle(String id) { [Link] = id; }
public abstract void dispatch(String destination);
}

class Bus extends Vehicle { public void dispatch(String destination) { ... } }


class Taxi extends Vehicle { public void dispatch(String destination) { ... } }
class Train extends Vehicle { public void dispatch(String destination) { ... } }
class Bike extends Vehicle { public void dispatch(String destination) { ... } }

class Dispatcher {
public void sendVehicle(Vehicle v, String destination) { [Link](destination); }
}

public class Problem3_TransportDispatch {


public static void main(String[] args) {
Dispatcher d = new Dispatcher();
Vehicle v = new Taxi("TX-01", 12.5);
[Link](v, "Central Station");
}
}

Problem 4 — Hospital Management System (Upcasting)


class MedicalStaff {
String name;
MedicalStaff(String name) { [Link] = name; }
public void shiftSchedule() { ... }
public void idCardAccess() { ... }
}

class Doctor extends MedicalStaff { public void diagnose() { ... } }


class Nurse extends MedicalStaff { public void administerMedicine() { ... } }
class Technician extends MedicalStaff { public void runTest() { ... } }
class Administrator extends MedicalStaff { public void manageRecords() { ... } }

public class Problem4_HospitalUpcasting {


public static void main(String[] args) {
MedicalStaff staff = new Doctor("Dr. Mehta");
[Link](); // upcasting
}
}

Problem 5 — Digital Art Gallery (Downcasting)


class Artwork {
String title;
Artwork(String t) { title = t; }
}

class Painting extends Artwork { public void paintingDetails() { ... } }


class Sculpture extends Artwork { public void sculptureDetails() { ... } }
class DigitalArt extends Artwork { public void digitalDetails() { ... } }
class Photography extends Artwork { public void photographyDetails() { ... } }

public class Problem5_ArtGalleryDowncasting {


public static void main(String[] args) {
Artwork art = new Painting("Sunrise");
if (art instanceof Painting) {
Painting p = (Painting) art; // downcasting
[Link]();
}
}
}
Problem 6 — Smart Home Automation (Safe Downcasting with
instanceof)
abstract class SmartDevice { String id; SmartDevice(String id) { [Link] = id; } }

class SmartTV extends SmartDevice { public void changeChannel(int ch) { ... } }


class Thermostat extends SmartDevice { public void setTemperature(double t) { ... } }
class SecuritySystem extends SmartDevice { public void arm() { ... } }
class SmartOven extends SmartDevice { public void startRecipe(String recipe) { ... } }

public class Problem6_SmartHomeSafeDowncast {


public static void main(String[] args) {
SmartDevice d = new SmartTV("TV-1");
if (d instanceof SmartTV) {
SmartTV tv = (SmartTV) d;
[Link](5);
}
}
}

You might also like