JAVA
CS117 Second Exam Review Page 1 of 2
Exam 2 is scheduled for Friday, April 9. In order to help you prepare for the exam, this review sheet represents the types of questions you might expect on the exam. This review is definitely longer than the exam will be, because I wanted to include every kind of question and topic that might appear on the real exam. So take out a sheet of paper and answer the following questions.
1. Answer the following questions with a sentence or two.
(a) When must you make a class abstract?
(b) What must you do if you implement an interface?
2. Write an Employee class with the following features:
(a) A field that stores the employee’s name.
(b) A field that stores the number of hours the employee worked.
(c) A field that stores the number of years the employee has worked for the company.
(d) A constructor with parameters for name and years of service.
(e) Accessors for the three fields.
(f) A method named addHours that can be used at the end of the day to note the number of hours she worked.
(g) An abstract method named getPay that calculates and returns the amount of money owed to the employee for their work.
(h) A method named getBonus that calculates the annual bonus to be paid to the employee. Every employee receives $100 for each year they have worked.
3. Create a HourlyEmployee class that is a subclass of Employee, with the following features:
(a) A field that stores the employee’s hourly wage.
(b) A constructor with parameters for the employee’s name, years of service, and hourly wage.
(c) An override of the getPay method that calculates the pay by multiplying the wage by the number of hours worked.
(d) An override of the getBonus method that calculates the bonus by adding 10 * the wage to the bonus that all employees receive.
4. Create a SalariedEmployee class that is a subclass of Employee, with the following features:
(a) A field that stores the employee’s salary.
(b) A constructor with parameters for the employee’s name, years of service, and hourly wage.
(c) An override of the getPay method that simply returns the salary.
(d) An override of the getBonus method that calculates the bonus by adding 0.01 * the salary to the bonus that all employees receive.
5. Create a Main class with a main method that does the following:
(a) Creates an ArrayList of Employees.
(b) Inserts into that list an SalariedEmployee named Alan who has worked for the company 10 years and earns an annual salary of $50,000.00.
(c) Inserts into that list an HourlyEmployee named Betty who has worked for the company 2 years and earns an hourly wage of $15.00.
(d) Uses a loop to add 40 hours to each employee in the list.
(e) Uses a loop to print the pay and bonus of each employee in the list.
CS117 Second Exam Review Page 2 of 2
6. Write down the expected output of the program you just wrote.
7. Draw a basic UML class diagram, of the type that BlueJ draws, for your four classes.
8. Create an interface named Expense with the following features:
(a) A method getCost that returns the total annual cost of whatever implements the interface.
9. Update the Employee class to make it implement the Expense interface. The total annual cost of an employee is the sum of their pay and bonus, plus $20,000 for benefits.
10. Create a class named RentedBuilding that implements the Expense interface, with all of the features required by the interface plus:
(a) A field that stores the building’s address.
(b) A field that stores the monthly rent due for the building.
(c) A constructor with fields for the address and monthly rent amount.