Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

OOP Assignment 5

Uploaded by

Abdullah Afzal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OOP Assignment 5

Uploaded by

Abdullah Afzal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Task #1

Scenario:

An academic institution needs to manage the hierarchy of its employees, which includes
general staff, faculty, and administrators. Each employee has a salary, but faculty members
also have office hours and a rank. The system needs to keep track of these attributes and
allow for customized printing of details based on the type of employee.

Task Description:

1. Create a base class Person with fields for name, address, phone number, and email
address.
2. Extend Person to create an Employee class with fields for salary and the date of
hiring.
3. Further extend Employee to create a Faculty class that adds fields for office hours and
rank.
4. Override the toString() method in each class to output customized details for each
type of person.
5. Write a main program to create objects for a generic person, employee, and faculty
member, then print their details using the overridden toString() methods.

Task #2

Scenario:

A company needs to track shipments of products, which have multiple layers of attributes
such as size, weight, and shipping costs. The system must calculate the volume, weight, and
total cost of shipping a product based on its attributes.

Task Description:

1. Create a Box class with width, height, and depth, along with a method to calculate the
volume.
2. Extend Box to a BoxWeight class, which adds a weight attribute.
3. Extend BoxWeight into a Shipment class, which adds a cost attribute for shipping.
4. Write a main program that creates shipment objects, calculates and displays their
volume, weight, and shipping cost.

Task #3

Scenario:

A banking application needs to manage different types of accounts, including savings and
checking accounts. The system should ensure that all accounts can perform common operations
such as deposits and withdrawals, while also allowing for specific implementations based on
the type of account.
Task Description:

1. Create an Abstract Class:


o Define an abstract class named Account with the following attributes:
▪ protected String id
▪ protected double balance
o Implement the following methods:
▪ public Account(String id, double balance): A constructor that
initializes the account ID and balance.
▪ public String getID(): Returns the account ID.
▪ public double getBalance(): Returns the current balance.
▪ public abstract boolean withdraw(double amount): An abstract method
for withdrawing money.
▪ public abstract void deposit(double amount): An abstract method for
depositing money.
2. Create Subclass:
o Implement a subclass named SavingsAccount that extends Account and
includes:
▪ A constructor that requires an initial deposit of at least $10.
▪ Implementation of the withdraw method, including a transaction fee of
$2 for each withdrawal, ensuring that the balance does not drop below
$10 after withdrawal.
3. Test the Implementation:
o In the main method, create an instance of SavingsAccount, perform some
deposits and withdrawals, and print the resulting balances to verify the
functionality.

Task 4:
Scenario:

An educational institution needs to manage various staff roles, including faculty and
administrative personnel. It requires a system that distinguishes between different types of
employees while enforcing specific rules about roles and responsibilities.

Task Description:

1. Create a Base Class:


o Define a class named Person with fields for name, address, phone number, and
email. Implement a constructor to initialize these fields.
2. Use Abstract Classes:
o Extend Person to create an abstract class named Employee that includes:
▪ Fields for salary and date hired.
▪ An abstract method public abstract String getDetails(): To retrieve
employee details.
3. Implement Subclasses:
o Create two subclasses: Faculty and Staff, which inherit from Employee and
implement the getDetails() method to return customized details specific to
their roles.
4. Test the Implementation:
o In the main method, create instances of Faculty and Staff, print their details
using the getDetails() method to demonstrate the use of inheritance and
polymorphism.

Task #5

You have been assigned the responsibility of designing a Fleet Management System for a
transportation company that manages various vehicle types, specifically Sedans and Cargo
Trucks. This system must leverage the principles of inheritance, constructor overloading,
and method overriding to efficiently handle the specifications and operations of each vehicle
type.

1. Base Class - Vehicle:


o Create a base class named Vehicle, which includes the following attributes:
▪ A private attribute manufacturer (String): indicating the manufacturer
of the vehicle.
▪ A private attribute model (String): representing the model of the
vehicle.
▪ A protected attribute manufacturingYear (int): indicating the year in
which the vehicle was manufactured.
o The Vehicle class should consist of:
▪ A constructor that initializes the manufacturer, model, and
manufacturingYear.
▪ A method named displayDetails() that returns a formatted string
showcasing the manufacturer, model, and manufacturing year of the
vehicle.
2. Derived Classes:
o Sedan Class:
▪ Inherit from the Vehicle class.
▪ Introduce a private attribute numberOfDoors (int): indicating how
many doors the sedan has.
▪ Provide a constructor that initializes the manufacturer, model,
manufacturingYear, and numberOfDoors.
▪ Override the displayDetails() method to include the number of doors in
the output.
o CargoTruck Class:
▪ Inherit from the Vehicle class.
▪ Add a private attribute cargoCapacity (double): representing the
maximum weight the truck can carry.
▪ Provide a constructor that initializes the manufacturer, model,
manufacturingYear, and cargoCapacity.
▪ Override the displayDetails() method to present the cargo capacity
alongside the other vehicle details.
Task Description:
In the Fleet Management System for a transportation company, a base class named Vehicle
is created with private attributes for the manufacturer and model, and a protected attribute for
the manufacturing year. Two derived classes, Sedan and CargoTruck, inherit from the
Vehicle class. The Sedan class includes an additional private attribute for the number of
doors, while the CargoTruck class features a private attribute for cargo capacity. Each
derived class has its own constructor to initialize all attributes and overrides the
displayDetails() method to present specific information. This scenario showcases the
principles of inheritance, constructor overloading, and method overriding in object-
oriented programming.

CODE:
class Vehicle {

private String manufacturer;

private String model;

protected int manufacturingYear;

Vehicle(String manufacturer, String model, int manufacturingYear) {

this.manufacturer = manufacturer;

this.model = model;

this.manufacturingYear = manufacturingYear;

public String displayDetails() {

return "Manufacturer: " + manufacturer + ", Model: " + model + ", Year: " + manufacturingYear;

class Sedan extends Vehicle {

private int numberOfDoors;

Sedan(String manufacturer, String model, int manufacturingYear, int numberOfDoors) {

super(manufacturer, model, manufacturingYear);


this.numberOfDoors = numberOfDoors;

@Override

public String displayDetails() {

return super.displayDetails() + ", Number of Doors: " + numberOfDoors;

class CargoTruck extends Vehicle {

private double cargoCapacity;

CargoTruck(String manufacturer, String model, int manufacturingYear, double cargoCapacity) {

super(manufacturer, model, manufacturingYear);

this.cargoCapacity = cargoCapacity;

@Override

public String displayDetails() {

return super.displayDetails() + ", Cargo Capacity: " + cargoCapacity + " tons";

public class Test {

public static void main(String[] args) {

Vehicle sedan = new Sedan("Toyota", "Camry", 2022, 4);

Vehicle truck = new CargoTruck("Volvo", "FH16", 2020, 25.5);

System.out.println(sedan.displayDetails());

System.out.println(truck.displayDetails()); } }

You might also like