Assignment - 05
Assignment - 05
KARACHI CAMPUS
APPLIED PROGRAMMING – FALL 2024
ASSIGNMENT-2
th
Total Marks: 100 Marks Due Date: 10 October 2024
Section: Inheritance & Polymorphism
You have been hired as a software developer to design a simplified Hospital Management System. The system
should manage various types of employees within a hospital, each with distinct roles and responsibilities. The
system should allow the hospital administration to manage these employees efficiently by utilizing the
concepts of inheritance and polymorphism.
Base Class:
o Employee: Each employee in the hospital shares common attributes and behaviors. Create a
base class Employee that contains the following:
name: String
id: Integer
salary: double
work(): a method that prints a generic message like "Employee is working."
Derived Classes:
o Doctor: Inherits from Employee. This class should have an additional attribute specialization
(String). The work() method should be overridden to print "Doctor is treating patients."
o Nurse: Inherits from Employee. This class should have an additional attribute shiftHours (int).
o Receptionist: Inherits from Employee. This class should have an additional attribute deskNumber
(int). Override the work() method to print "Receptionist is managing appointments."
o Surgeon: Inherits from Doctor. This class should have an additional attribute surgeryCount (int).
Override the work() method to print "Surgeon is performing surgery."
Polymorphism:
o Create a method manageEmployee(Employee emp) in a separate Hospital class. This method
should take an Employee object and call the work() method. Demonstrate how different
employee types can be managed using polymorphism (i.e., how the method works differently
based on whether the Employee is a Doctor, Nurse, Receptionist, or Surgeon).
Write a main method that creates a list of different types of employees, calls the manageEmployee() method
for each, and demonstrates polymorphism in action.
Additional Task:
o Implement a method in the Hospital class called paySalary() that takes an Employee object and prints
the salary based on the type of employee (use polymorphism if required).
Expected Output
Doctor is treating patients. The salary of Doctor [name] is [salary] PKR.
Nurse is taking care of patients. The salary of Nurse [name] is [salary] PKR.
Receptionist is managing appointments. The salary of Receptionist [name] is [salary] PKR.
Surgeon is performing surgery. The salary of Surgeon [name] is [salary] PKR.
Question 2 (15 Marks):
You have been tasked with developing a simple payment system for a retail store. The store offers different
payment methods based on the customer's preferences. To manage payments, you need to implement method
overloading, a concept of compile-time polymorphism, to handle different types of payment scenarios.
Requirements:
Class: Payment
o Create a Payment class that has overloaded methods to process payments based on different
payment types:
By Cash: A method processPayment(double amount) that accepts the total amount and prints a
message like "Processing cash payment of [amount] PKR."
By Credit Card: A method processPayment(String cardNumber, double amount) that accepts
the card number and amount, and prints "Processing credit card payment of [amount] PKR
for card number [cardNumber]."
By Check: A method processPayment(String checkNumber, String bankName, double
amount) that accepts a check number, bank name, and amount, and prints "Processing
check payment of [amount] PKR from [bankName] with check number [checkNumber]."
Create a main method to test the different payment scenarios using a Payment object, invoking the correct
overloaded method for each type of payment.
Expected Output
Processing cash payment of 100 PKR.
Processing credit card payment of 250 PKR for card number 1234-5678-9876-5432.
Processing check payment of 300 PKR from Bank of America with check number 00112233.
Processing mobile payment of 50 PKR from phone number +922345678900.
Section: Generics
Question 3 (10 Marks): Create a generic class Box that can hold any type of object. Implement the following:
A private field of type T to store the item.
A constructor that takes a T object and stores it in the field.
A method getItem() that returns the stored item.
A method setItem(T item) to change the item in the box.
Write a main method that demonstrates how you can create a Box for different types, such as Integer, String,
and Double.
Expected Output
Box contains: 42
Box contains: Hello, World
Box contains: 15.75
Question 4 (10 Marks): Write a generic class Calculator that has a method sum() to add two numbers. Use
bounded type parameters to ensure the method only works with types that extend Number (e.g., Integer,
Double, etc.).
Question 5 (10 Marks): You have a class Printer with a generic method printItems(List<?> items) that prints
each element in the list.
Implement the Printer class and the printItems() method using a wildcard ? to accept a list of any type.
Write a main method that creates lists of different types (Integer, String, etc.) and prints their contents
using the Printer class.
Expected Output
Items: 1, 2, 3, 4, 5
Items: Apple, Orange, Banana
Question 6 (10 Marks): Design a class Pair<K, V> that holds a pair of objects (a key of type K and a value of
type V).
The class should have two private fields: key and value.
Implement a constructor that initializes both fields.
Implement getKey() and getValue() methods to return the key and value.
Write a main method that demonstrates the use of Pair with different types, like Integer-String, String-
Double, etc.
Expected Output
Key: 1, Value: Apple
Key: Name, Value: John
Question 7 (10 Marks): Write a generic method swap(T[] array, int i, int j) that swaps the elements at positions i and j in
an array.
The method should work for arrays of any type.
Write a main method to demonstrate the swap() method with an array of Integer and String.
Expected Output
Question 8 (10 Marks): Create a generic interface Storage<T>, which has the following methods:
void addItem(T item);
T getItem();
Question 9 (10 Marks): Write a Java program that performs the division of two integers. Your program should
take two numbers as user input and handle the following exceptions:
ArithmeticException: If the user tries to divide by zero, the program should display a message: "Cannot
divide by zero."
InputMismatchException: If the user inputs something other than an integer, display: "Invalid input.
Please enter integers only."