0% found this document useful (0 votes)
15 views

Assignment - 05

The document outlines an assignment for an Applied Programming course at the National University of Computer & Emerging Sciences, focusing on inheritance, polymorphism, generics, interfaces, and exception handling in Java. It includes multiple questions requiring the design of a Hospital Management System, a payment system, and various generic classes and methods. Each question specifies requirements and expected outputs, emphasizing the application of object-oriented programming concepts and error handling.

Uploaded by

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

Assignment - 05

The document outlines an assignment for an Applied Programming course at the National University of Computer & Emerging Sciences, focusing on inheritance, polymorphism, generics, interfaces, and exception handling in Java. It includes multiple questions requiring the design of a Hospital Management System, a payment system, and various generic classes and methods. Each question specifies requirements and expected outputs, emphasizing the application of object-oriented programming concepts and error handling.

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

NATIONAL UNIVERSITY OF COMPUTER & EMERGING SCIENCES

KARACHI CAMPUS
APPLIED PROGRAMMING – FALL 2024

ASSIGNMENT-2
th
Total Marks: 100 Marks Due Date: 10 October 2024
Section: Inheritance & Polymorphism

Question 1 (15 Marks):

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.).

Define the Calculator<T extends Number> class.


Implement a method sum(T num1, T num2) that returns the sum of num1 and
num2. Demonstrate this class with different numeric types, like Integer and Double.

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

Before swap: [1, 2, 3, 4]


After swap: [1, 4, 3, 2]

Before swap: [Hello, World, Java]


After swap: [Java, World, Hello]
Section: Interface

Question 8 (10 Marks): Create a generic interface Storage<T>, which has the following methods:
void addItem(T item);
T getItem();

Then, implement this interface in a class SimpleStorage<T>.


In the SimpleStorage class, store a single item of type T.
Implement the addItem() and getItem() methods.
Write a main method to demonstrate how SimpleStorage can be used for storing different types of
objects, such as String, Integer, and Float.
Section: Exception Handling

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."

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy