The University of Western Australia
School of Computer Science and Software Engineering
 
 

School of Computer Science and Software Engineering

CITS1001 Lab sheet 3

Labsheet 3 (for week 4)

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.

Task 1

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.

  1. What happens when the 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.)
    Is the solution adopted a good one? Can you think of a better solution?
  2. 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))

  3. What would happen if you replaced the && operator in the test with ││ so that it reads:

    if((replacementValue >= 0) ││ (replacementValue < limit))

  4. Create a new 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.)
  5. Look at the second constructor in ClockDisplay’s source code. Try to work out what it does and how it does it.
  6. Identify the similarities and differences between the two constructors. Why is there no call to updateDisplay in the second constructor, for instance?

Task 2

  1. 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?

  2. 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.)

  3. 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.

Task 3

Test the NumberDisplay class by creating several NumberDisplay objects and calling their methods.

  1. 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?

  2. 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?

  3. 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.

Task 4

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.

  1. 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.

  2. 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?


School of Computer Science and Software Engineering

This Page

Last updated:
Thu Dec 8 17:51:13 2011

Website Feedback:
[email protected]

http://undergraduate.csse.uwa.edu.au/units/CITS1001/