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.
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.
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.
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.
Write the definition of a new class, CounterWithMemory
, that extends the Counter
class and implements the following memory facility:
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.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).Write JUnit tests that exercise all of the methods that you added to the CounterWithMemory
class and ensures that they work correctly.
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
}
}
Marketer
. Marketers make $10,000 more than general Employees and they have an additional method named advertise
that prints “Act now, while supplies last!”.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!”.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”.)You will submit your files via Gradescope by the end of the week.
CounterWithMemory
and your testing class.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.
Work on the Self-check problems 5-7 Chapter 9.