Lab: 6 - More about Classes and Objects

Assigned
Wednesday, 6 September 2023

OBJECTIVE

This lab will give you additional practice in creating classes and objects.

Start this lab session by sharing something (most interesting or helpful and Java methods) you learned from the readings or on your own.

Implementing a Counter (2 points)

You can think of a counter as a simulation of those old-fashioned hand-held clickers that people used to use to count attendees at a county fair as they pass through the turnstile at the entrance gate or to tally cars as they cross a bridge. Such a clicker is basically a numerical display with a tally button that increments the number displayed and a reset button to reset that number to zero.

Create a Java class Counter that encapsulates the properties and behavior of a counter device. Provide its constructor, accessor method, and restricted mutation procedures for incrementing the internal tally and resetting it to zero increment and reset.

Create a tester or client App and test the followings after instantiating a Counter in the main method:

  • Increment the counter and display (using the accessor method) its value.
  • Increment the counter multiple times, reset it, increment the counter, and then display its value.

Let’s assume that each counter produced by the constructor actually carries around a name which is specified as a string argument to the constructor. This name is supposed to be immutable, since none of the ADT procedures modify it, and it is used only by the toString method that displays the internal state of the counter.

  • Add a private name field to your Counter class.
  • Add a String argument to the constructor that you wrote earlier that takes a name of the counter.
  • Add the toString method that constructs and returns a String consisting the counter’s name, a colon, a space, and the current tally.

Add code in the tester class to test this feature by creating at least two instances of a counter, incrementing their values several times (but a different number!), and calling the toString method to display the string representation of the counters.

Note: copy and paste the terminal output of your programs in a .txt file (lab report).

Implementing an Averager (2 points)

A slightly more sophisticated device is the averager, which allows the user to add any number, not just 1, to a running total that it maintains, and also keeps track of the number of addends that it has received since it was created or last reset.

The averager can divide its running total by its running tally of addends to report the arithmetic mean of all of those addends.

For a minimal implementation, you’ll need a constructor, a mutator that takes a numerical argument and adds it to the running total (simultaneously incrementing the addend tally behind the scenes), a mutator that resets both the running total and the addend tally to 0, and an accessor that gives you the average of the addends.

The arithmetic mean is undefined when the addend tally is 0. In this special case, the accessor method should return 0 after printing a brief error report to System.err

System.err.println("Write your error message here");

which is a PrintStream analogous to C’s stderr file descriptor.

Implement and test an Averager class:

  • Computing and displaying the arithmetic mean of five positive and negative numbers
  • Reporting the error as explained above.

Note: copy and paste the terminal output of your programs in a .txt file (lab report).

Lab Submission

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

  • Counter.java, Averager.java, your client or tester App, and your lab report.

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 report in a way that will help the grader to find the answer to the exercises or posted questions.

For those with more time

Other potentially useful features for averagers include separate accessors for the running total and the addend tally, toString and equals methods, accessors for the maximum and minimum values among the addends seen so far, and a method that takes another Averager as argument and combines its running total and addend tally with the running total and addend tally of this averager. Note that the equals method takes Object as its parameter. Thus, you need to do a type conversion.