This work is based on material from Chapter 3 of the text for CITS1001, Objects First with Java - A Practical Introduction using BlueJ by David Barnes and Michael Kolling. The goal is to illustrate interacting classes and expressions.
Download the CITS1001-Lab03.zip zip file, and unzip it to your student network drive. Open this as a BlueJ project, and compile the code.
Create an instance of ClockDisplay
, using the constructor which takes no parameters, and inspect the instance. With the inspector window open, call the object’s methods, such as timeTick
and setTime
. Watch the “displayString” in the inspector. You can also double-click the arrows next to fields, like the one labelled “private NumberDisplay hours
” – these represent objects contained within the ClockDisplay
object. Open up the code for the NumberDisplay
and ClockDisplay
classes to see more about how they work.
setValue
method of the NumberDisplay
class is called with an illegal value (i.e., a number which doesn’t represent a valid hour or second)? Using the debugger, covered in the previous lab, you can set a breakpoint in the setValue
method and step through it, to see in more detail what is happening. (Note that the step into
button in the debugger will, as it says, “step into” the code of methods that are called – you will probably want to use that insted of step
.)In the code for the setValue
method, what would happen if you replaced the “>=
” operator in the test with “>
” so that it reads:
if((replacementValue > 0) && (replacementValue < limit))
What would happen if you replaced the &&
operator in the test with ││
so that it reads:
if((replacementValue >= 0) ││ (replacementValue < limit))
ClockDisplay
object by selecting the constructor which takes no parameters. Call its getTime
method to find out the initial time the clock has been set to. Can you work out why it starts at that particular time? (Look at the code, and use the debugger if you need to.)ClockDisplay
’s source code. Try to work out what it does and how it does it.Identify the similarities and differences between the two constructors. Why is there no call to updateDisplay
in the second constructor, for instance?
In the Code Pad, experiment with the modulus operator (%
). Try typing the expression (8 % 3)
into the Code Pad – what does it evaluate to?
What happens if you use different numbers? What happens if the first number is zero, or negative?
Suppose we have an integer variable n
. Consider the expression (n % 5)
; what possible results can this evaluate to? (Hopefully you should be able to work this out yourself – but use the Code Pad if you need to.)
Open up the code for the NumberDisplay
class, and take a look at the increment
method. See if you can explain how it works. Then, see if you can rewrite the method without using the modulus operator.
Test the NumberDisplay
class by creating several NumberDisplay
objects and calling their methods.
Does the getDisplayValue
method work correctly in all circumstances? What assumptions are made within it? What happens if you create a number display with limit 800, for instance?
How many times would you need to call the timeTick
method on a newly created ClockDisplay object to make its time reach 01:00? How else could you make it display that time?
Create a NumberDisplay object with limit 80 in the Code Pad by typing:
NumberDisplay nd = new NumberDisplay(80);
Then call its getValue
, setValue
, and increment
methods in the Code Pad. You can do this by using the dot operator on the nd
variable you have created – e.g., by typing:
nd.getValue()
Note that statements (mutators) need a semicolon at the end, while expressions (accessors) do not.
Right-click on the TestNumberDisplay
class, and select “Test All”. A “Test Results” window should open up, showing a number of tests the NumberDisplay
class has passed. You can also right-click on TestNumberDisplay
and run individual tests.
Take a look at the code inside the TestNumberDisplayClass
: each test is made up of one method.
In the NumberDisplay
class, edit the code for the constructor – replace
value = 0;
with
value = 10;
Re-compile, and run the tests using “Test All” again. Are the results what you expect? Then change the code back and re-run.
In the TestNumberDisplay
class, look at the code for the testSetValue
method. Considering the observations you made in Task 1, step 1, do you think this method adequately tests the behaviour of the NumberDisplay
class? Can you think of a way to improve the test?