Self-Study 6, CPSC 331, Fall 2010

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


Self-Study 1 -  Self-Study 2 -  Self-Study 3 -  Self-Study 4 -  Self-Study 5 -  Self-Study 6 -  Self-Study 7

 Implementing ADT's and Data Structures in Java

About This Exercise

This exercise can be completed at any time after the lecture on September 29. It is intended to help you to make sure that you are able to

and make use of interfaces and classes that have been provided for you.

Expected Background and Preparation

Sections 2.2 and 2.4 of the textbook may be helpful, as well as the following sections from the Koffman and Wolfgang supplemental reference:

Problems To Be Solved

  1. Download and compile each of the following. Note, as you are doing so, that the file SimpleCounter.java can be used to to produced the “Simple Counter” that was used as an example in the above-mentioned lecture.

    1. LimitReachedException.java
    2. Counter.java
    3. SimpleCounter.java
    4. counterTest.java

    Where are the corresponding .class files located? Why?

    What (kind of construct) does compilation of the file Counter.java produce? What does SimpleCounter.java produce? How are these related?

  2. After you have completed the previous question, execute the command

    java counterTest

    You should see the following information displayed as a result of this.

    Counter limit: 5
     
    Counter value: 0
     
    Trying to increase counter value.
    Counter value: 1
     
    Trying to increase counter value.
    Counter value: 2
     
    Trying to increase counter value.
    Counter value: 3
     
    Trying to increase counter value.
    Counter value: 4
     
    Trying to increase counter value.
    Counter has reached its limit.

    Why (or how) was this output produced?

  3. Suppose, now, that we are anticipating the needs of a user who will be frequently asking the question, ``How much time do I have left?''

    1. Create a file BackwardsCounter.java with the following properties.

      • This can be compiled to create a class that implements the Counter interface.

      • The data item limit is represented as an “instance variable,” just as it is for the class produced by compiling SimpleCounter.class.

      • A second instance variable with name timeRemaining is also used. This has the same value as limit when an object is created. Its value is decremented (as long as the resulting value is positive) whenever the advanceValue method is used — but a LimitReachedException should be thrown, and this variable’s value should be reset to the value of limit, if decrementing would reduce its value to zero.

      • No other instance variables are used, and the method getValue generates its output value as a (reasonably straightforward) function of the instance variables that have been described above.

    2. Now modify the file counterTest.java so that it creates and makes use of an instance of your BackwardsCounter instead of the instance of SimpleCounter that it uses now. It should not be necessary for you to change more than three lines of code in order to this, if you wrote the BackwardsCounter source code in the expected way.

      Then compile and run the counterTest program again. The output should be the same as it was before you made any changes to this program.

  4. Write each of the following programs. Keep in mind, when doing so, that the purpose of this exercise is to help you to give you practice in the use of object-oriented programming — so you are probably defeating the purpose of the exercise if you write the programs in different ways than are suggested here.

    If you do not have time to write these programs then — make sure that you know how these programs would be written.

    1. A TimeOfDay class: Instances of this class can be used to keep track of the current time of day as measured in seconds, minutes, and hours. Initially the time (given using as a 24 hour clock) is 00:00:00.

      A tick method advances the time of day by exactly one second, so that repeated uses of this method could be used to advance the time all the way to 23:59:59 — at which point, another application of the tick method would cause the time to be reset to 00:00:00, and would cause a LimitReachedException to be thrown, indicating that a new day has started.

      A listTime method reports the current time as a string with the format suggested above (that is, the current hour, minute and second are shown, separated by colons).

      You should (in some way) use three instances of the SimpleCounter class in order to implement (an instance of) your TimeOfDay class — but it should not be necessary for a user of your class’s methods to know this, and it should not be possible for such a user to have direct access to these SimpleCounter objects.

    2. A BetterCounter interface extends the Counter interface that is available to you now, by providing a getRemaining that reports the difference between the counter’s limit and value.

    3. A BetterBackwardsCounter class, whose objects (essentially) provide the same methods as those of the BackwardsCounter that you wrote when you solved Problem #3, above, along with one more method — and which the implements the BetterCounter interface.

      Try to write (or rewrite) as little code as you can when you write this program!


Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F10/tutorials/self-study6.html