Lab: 11 - Inheritance

Assigned
Monday, 18 September 2023

OVERVIEW

In today’s lab, we’ll extend the Counter class, deriving other classes from it to illustrate the mechanics of code reuse through inheritance.

Setup

  1. Open Eclipse, create a new project called inheritance, and in that project create a new package called tallying for the classes we develop in this lab.

  2. Open the Counter.java class definition that you wrote for the Lab 5: More about Classes and Objects lab and place it in the tallying package.

Adding a Memory Field (2 points)

One useful way of extending the Counter class is to give counters a memory, allowing them to store the tally at a particular time and subsequently recover the stored tally, even after additional click operations have overwritten the tally field itself. Assume that the memory stores a scalar value. Initially, the memory is set to zero.

  1. Write the definition of a new class, CounterWithMemory, that extends the Counter class and implements the following memory facility:

    • A reset method. This method resets the stored tally to zero (or its initial value, you decide how you want to handle it). Decide whether the tally field itself should be reset too.
    • The “store the current tally” method. Note that this method should display the current stored tally, if any, before storing the new one. Think of this as a backup that saves the current tally into a backup field. But if that backup field already has a non-zero value, we want to report it before we save over it.
    • The “recover the stored tally” method. Note that this method should throw an object of BufferUnderflowException if the stored talley data field does not have a value (yet)–a highly unlikely event in this context, but it is good practice. I interpret this to mean that the value in the stored talley (backup value) should be copied into the data field for the current tally (current value).
  2. Write JUnit tests that exercise all of the methods that you added to the CounterWithMemory class and ensures that they work correctly.

    • Consider a test case that recovers the stored tally, increments the tally a few times, stores the current tally, and recovers the tally again.

Employee class hierarchy (2 points)

Let’s create hierarchies of related classes, Consider the following Employee superclass

public class Employee {
    public int getHours() {
        return 40;           // works 40 hours / week
    }
    
    public double getSalary() {
        return 40000.0;      // $40,000.00 / year
    }
    
    public int getVacationDays() {
        return 10;           // 2 weeks of paid vacation
    }

    public String getVacationForm() {
        return "yellow";     // use the yellow form
    }
}
  • Write an employee class Marketer. Marketers make $10,000 more than general Employees and they have an additional method named advertise that prints “Act now, while supplies last!”.
  • Write an employee class Lawyer. Lawyers get 5 extra days of vacation than general Employees, they use a pink vacation form and they have an additional method named sue that prints “I’ll see you in court!”.
  • Write an employee class HarvardLawyer. Harvard lawyers are like normal lawyers, but they make 20% more money than a normal lawyer, they get 3 days more vacation, and they have to fill out four of the lawyer’s forms to go on vacation. That is, the getVacationForm method should return “pinkpinkpinkpink”. (If the normal Lawyer’s vacation form ever changed, the HarvardLawyer’s should as well. For example, if Lawyer’s vacation form changed to “red”, the HarvardLawyer’s should return “redredredred”.)
  • Create the UML diagram that represents the Employees hierarchy.

Lab Submission

You will submit your files via Gradescope by the end of the week.

  • Submit your code for the new class CounterWithMemory and your testing class.
  • Submit your Employee class hierarchy and the corresponding UML diagram.

I strongly recommend that each student keep a copy of the lab. Therefore, don’t forget to share the files/folder with your lab partner!

Remember: Write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). You should include comments or organize your code and lab report in a way that will help the grader to find the answer to the exercises or posted questions.

For those with more time …

Work on the Self-check problems 5-7 Chapter 9.