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

School of Computer Science and Software Engineering

CITS1001 Object-oriented Programming and Software Eng

Labsheet 2 (for week 3)

This work is based on material from Chapter 2 of the text for CITS1001, Objects First with Java - A Practical Introduction using BlueJ by David Barnes and Michael Kolling; and on tasks originally designed by Rachel Cardell-Oliver. The goal is to look inside the structure of a Java class.

Some useful BlueJ tips

  • In the editor, use Edit/Auto-layout after making any changes to your code, to correct indentation. Using auto-layout makes it easier to spot bugs.
  • Holding down the ctrl key and hitting the space key should allow you to see code completions. For example, typing Math. and then the ctrl+space combination will show you all the available Math methods.
  • You can comment out code in BlueJ by selecting one or more lines of code and selecting Edit/Comment from the menu.
  • You can compile your code with a single shortcut key - ctrl+k on Windows or Linux, cmd+k on MacOS.

Task 1

Download the CITS1001-Lab02.zip zip file, and unzip it to your student network drive. Open this as a BlueJ project, and compile the code. (See last week’s labs for more detailed instructions if you don’t recall how to do this.)

Create a TicketMachine instance. You will be asked to supply a number that corresponds to the price of tickets (assumed to be in cents) that will be issued by that particular machine. For instance, entering 500 would create a machine which issues tickets costing $5. Take a look at the machine’s methods. You should see the following:

getPrice, getBalance, insertMoney, and printTicket.

Try the getPrice method – what does it return? Use the insertMoney method to simulate inserting an amount of money into the machine, and use getBalance to check that the machine has kept an accurate record of the amount just inserted. Try inserting several amounts and check what result getBalance gives you. Once you have inserted enough money for a ticket, call the printTicket method. A facsimile ticket should be printed in the “Terminal” window.

  1. What value is returned if you get the machine’s balance after it has printed a ticket?

  2. Experiment with inserting different amounts of money before printing tickets. Do you notice anything strange about the machine’s behavior? What happens if you insert too much money into the machine – do you receive any refund? What happens if you do not insert enough and then try to print a ticket?

  3. Create another ticket machine for tickets of a different price; remember that you have to supply this value when you create the machine object. Buy a ticket from that machine. Compare the printed ticket with that from the first machine you created – does it look any different?

Task 2

Take a look at the source code for the TicketMachine class. You can do this by double-clicking the class in the main BlueJ window (or by right-clicking it and selecting “Open Editor”); a new window should open up, containing the source code for the class.

Consider the following questions.

  1. Does it matter whether we write public class TicketMachine or class public TicketMachine in the outer wrapper of the class? Edit the source of the TicketMachine class to make this change. Close the editor window and note the changed appearance of the TicketMachine class. What error message do you get when you now click Compile? Do you think this message clearly explains what was wrong? Change the code back and compile it again. Check whether or not it is possible to leave out the word public from the outer wrapper. Put back the word public and then try leaving out the word class. Make sure that you put back both words before continuing. (Or, you can unzip the zip file again that contains the source code, and any changes you have made should be over-written.)
  2. Find the field declaration

      private int price;

    Does it matter which order the three words appear in? As before, try some different orders and compile them. What error message do you get if you remove the semicolon?

  3. Comment out the statement

       return price;

    from the getPrice method by inserting // at the beginning of the line.
    (You can also do this by selecting one (or more) lines of code and selecting Edit/Comment.)
    What happens when you compile the class? Is this message informative?

Task 3

  1. Create an accessor method getTotal in the TicketMachine class; the new method should return the value of the total field.

    Start by pasting the following code into the class:

      /**
       * Return the total amount of money collected by this machine.
       */
      public int getTotal() 
      {
          return 0; // replace this with your own code
      }
    

    Then replace the line containing return 0 with your own code. Compile the code; then try creating a TicketMachine instance and using it to see if your new method does what you expect.

  2. Try removing the return statement from the body of getPrice. What error message do you see when you try compiling the class? Look at the code for the insertMoney and printTicket classes – do they have return statements? Why (or why not), do you think?

  3. Create a mutator method setPrice in the TicketMachine class. The new method should take one parameter, and set the price field to the value of that parameter.

    Start by pasting the following code into the class:

      /**
       * Set the price of this machine's tickets to be cost (if reasonable)
       */
      public void setPrice(int cost) 
      {
          //add code here
      }
    

    Then replace the line that says //add code here with your own code. Again, make sure your code compiles, and try making a TicketMachine instance to ensure it does what you expect.

Task 4

This week you will learn to use the BlueJ debugger. Before you start the next task, watch this YouTube video: Using the Debugger in BlueJ with Java.

Open the BlueJ “Debugger” window, by selecting “View/Show Debugger” from the menu, and ensuring “Show Debugger” is ticked: a separate debugger window should open.
Open the code for the TicketMachine class (if you don’t have it open already) by double-clicking on the class. Find the constructor for the class, and put your cursor in the line that says

price = cost;

Select Tools/Set/Clear Breakpoint from the BlueJ menu, and a red “STOP” icon should appear in the left-hand margin of the line.

Now try creating a new TicketMachine instance – what happens? You can use the Step button to continue executing the code. Look at what happens to the value of variables as you do so. What value do the balance and total fields have when the constructor starts executing? What values do they have afterwards? And why do you think they have been given the values they have, in the constructor?

Set breakpoints in the getTotal and setPrice methods, then try calling those methods. In this case, your methods should be very simple, and you should easily be able to predict what will happen to the values of variables as you step through the methods. But in longer pieces of code, the Debugger provides a useful way of finding out what your code is actually doing, when you don’t see the results you expect.

Task 5 – challenge task

Try the following, if you have completed the previous tasks.

  1. Modify the code of insertMoney so that a user can enter only “sensible” amounts of money. What do you think “sensible” should mean? (Try calling insertMoney with different values – are there any you think should not be allowed?)

  2. Modify the code of printTicket so that it will a) print a ticket only if enough money has been input, b) print an error message if there is not enough money, c) deduct only the ticket price from the balance (possibly leaving change to be given) rather than setting the balance to 0.

In both cases, try creating a new TicketMachine instance, and checking to see if your code does what you think it does.

Submission

Submit your code, with the revised getTotal and setPrice methods, using CSSubmit as follows.

Go to CSSubmit, find the submissions page for CITS1001, click on the Lab 2 submission link, and submit your code file TicketMachine.java. Do not submit any other type of file: zip, class, txt etc. This is the method that will be used to submit your project work for CITS1001. Note that the web submission page will give you feedback on whether you've correctly submitted the file it expects and will check a few properties of the submission.