Object oriented programming with subclasses

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Inheritance & Polymorphism

Given Person class definition below:

class Person {

private String name;

Person(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

  1. Design two subclasses of Person, named Student and Employee. Make Faculty and Staff subclasses of Employee. An instance of Student has a class status (freshman, sophomore, junior, and senior). Declare the status as constants in the Student class. An instance of Employee has a department, salary, and date-hired. Define a class named MyDate that contains fields of the year, month, and day. A faculty member has office hours and a rank (lecturer, senior lecturer, associate professor, and professor). A staff member has a position title. Override the toString() method in

    • Person class to display the class name and the person's name.
    • Student class to display class name, student's name and class status.
    • Employee class to display class name, employee's name, and department
    • Faculty class to display class name, employee's name, office hours and rank
    • Staff class to display class name, employee's name, position title

    1. Draw a class diagram to represent the relationship of the classes above.
    2. Using Java to write and implement the classes with regard to the class diagram.
    3. Write a test program in Java that creates instances of Person, Student, Employee, Faculty, and Staff, and invokes their toString() method.
    4. Write another test program in Java that creates instances of Person, Student, Employee, Faculty, and Staff in an array of Object. Use a repetition statement to call its toString() method.

  2. Modify the classes in question (1) above:

    1. Add a package ctb2209.p7 to Person class, and then compile all the classes again. Explain the errors and fix it.

  3. Design a class named Square that contains data fields for height, width, and an abstract method named computeSurfaceArea(). Create a child class named Cube. Cube contains an additional data field named depth, and overrides computeSurfaceArea() method inherited from its parent method.

    1. Implement all the classes.
    2. Write an application that instantiates objects of Square and Cube in an array and displays the surface areas of each object using iteration, and test all the methods of all the objects.
    3. Create an abstract AutoMobile class with fields for the car make and price. Include get and set methods for these fields; the calculatePrice() method is abstract. Create two subclasses for individual automobile makers (for example, Ford or Honda), and include appropriate calculatePrice() method in each subclass (for example, RM110,000 or RM120,000 respectively).

  4. Finally, write an application that uses the AutoMobile class as reference type and instantiate objects of its subclasses to display information about different cars. Save the files as AutoMobile.java, Ford.java, Honda.java and UseAutoMobile.java.

  5. Create an interface called Player. The interface has an abstract method called play() that displays a message describing the meaning of "play" to the class. Create classes called Child, Musician, and Actor that implement Player interface. Create an application that demonstrates the use of the classes. Save the files as Player.java, Child.java, Actor.java, Musician.java, and UsePlayer.java.
  6. Sunway Construction Loan Corporate makes loans of up to RM100,000,000,000 for construction projects. There are two categories of Loans - those to businesses and those to individual applicants.

  7. Write an application that tracks all new construction loans. The application must also calculate the total amount owed at the due date (original loan amount + loan fee). The application should include the following classes:

    • LoanConstants - A public interface class. LoanConstants includes constant values for short-term (one year), medium-term (three years), and long-term (five years) loans. It also contains constants for the company name and the maximum loan amount.
    • Loan - A public abstract class that implements the LoanConstants interface. A Loan includes a loan number, customer last name, amount of loan, interest rate, and term. The constructor requires data for each of the fields except interest rate. Do not allow loan amounts over RM100,000,000,000. Force any loan term that is not one of the three defined in the LoanConstants class to a short-term (one-year) loan. Create a toString() method that displays all the loan data.
    • BusinessLoan - A public class that extends Loan. The BusinessLoan constructor sets the interest rate to 1% over the current basic interest rate.
    • PersonalLoan - A public class that extends Loan. The PersonalLoan constructor sets the interest rate to 2% over the current basic interest rate.
    • CreateLoans - An application that creates an array of five Loans. Prompt that user for the current basic interest rate. Then, in a loop, prompt the user for a loan type and all relevant information for that loan. Store the created Loan objects in the array. When data entry is complete, display all the loans.

    Save the files as Loan.java, LoanConstants.java, BusinessLoan.java, PersonalLoan.java, and CreateLoans.java.

[Note: java.util.Date and java.text.SimpleDateFormat may needed for your total amount owed at the due date calculation, you may look at the usage of format() and parse() methods of SimplateDateFormat class]