Object oriented programming with subclasses
Inheritance & Polymorphism
Given Person class definition below:
class Person {
private String name;
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
- 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
- Draw a class diagram to represent the relationship of the classes above.
- Using Java to write and implement the classes with regard to the class diagram.
- Write a test program in Java that creates instances of Person, Student, Employee, Faculty, and Staff, and invokes their toString() method.
- 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.
- Add a package ctb2209.p7 to Person class, and then compile all the classes again. Explain the errors and fix it.
- Implement all the classes.
- 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.
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).
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.
- 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]
Order Now