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 1 (for week 2)

This work is based on material from Chapter 1 of the text for CITS1001, Objects First with Java - A Practical Introduction using BlueJ by David Barnes and Michael Kolling. The goal is to demonstrate some characteristics of objects and classes, plus some experimentation with modifying source code.

A video note by David Barnes that walks you through the concepts for this lab is available here: Creating and using objects within BlueJ.

The lab instructions assume you are using MS Windows on one of the lab’s computers; if you are using your own computer, or another operating system, you may have to adjust them.

Perform the following tasks in order.

  1. Creating a “Shapes” project in BlueJ.

    1. Download the following link to a zip file by right-clicking on it and selecting ‘Save As’: CITS1001-Lab01.zip. Save the file to your student drive, and unzip it by right-clicking on the file and selecting ‘uncompress’.
    2. Start up BlueJ. On the lab machines it will be available from the Start Menu.
    3. Select Project/Open Non Bluej..., select the CITS1001-Lab01 folder, and click Open in BlueJ. You should see the files contained in the folder. The arrows between the boxes indicate the dependencies between the classes. Picture uses the three “shape” classes, which each use Canvas – rearrange the boxes within the work area to make this hierarchy clearer.
    4. Select Tools/Compile. BlueJ should then compile the five classes so that we can run them. Successful compilation of a class is indicated by the unshading of its box. Have a look in the CITS1001-Lab01 directory. You should see a number of new files that are created by BlueJ during the compilation process – but we won’t worry about those for the moment.
    5. Right-click on Square, select new Square(), and click Ok. This creates an object square1 of class Square.
      Right-click on square1 and select Inspect. This shows you the current state of square1: note in particular that the field isVisible is false, hence you cannot see square1. Also note the xPosition and yPosition of square1.
    6. Right-click on square1 and select makeVisible(). This should create a canvas and display square1 on that canvas. Look at the state of square1 again using Inspect – what value does isVisible have now?
    7. Right-click on square1, select slowMoveHorizontal(), enter -200 into the box, and click Ok. What happens? Why do you think square1 moved to the left (as opposed to the right)? What do you think has happened to the state of square1? Have a look!
    8. Try invoking slowMoveHorizontal() without entering a number into the box. Try invoking slowMoveHorizontal() with the argument 50 * 2, and with true, and with 45.6. Observe what happens.
    9. Try invoking other methods of square1. Think about the way that they work, why some take arguments and others don’t, etc.
    10. Select View/Show Code Pad (if the Code Pad isn’t already open). Type in 0.1 * 2. Type in 0.1 == 0.3 - 0.2. Type in 0.3 - 0.2 - 0.1. Wonder at the inadequacies of software.
  2. Repeat Step 1 to create two Square objects and a Triangle object.
    When you make them all visible, how many squares can you see? Is this what you expected? If not, why do you think this might be? (Hint: try setting each square to different colours and different positions).
    Compare the changeSize methods of the three classes. Note that the changeSize method of the Triangle class differs from that of the Square and the Circle classes. How does it differ? What does it do? Why do you think these methods are different for the three shape classes?

  3. Recording method calls and submitting
    Click View / Show Terminal in BlueJ, and make sure “Show Terminal” is ticked.
    In the “Terminal” window, click Options / Record method calls, and make sure “Record method calls” is ticked.
    These options allow you to make a series of method calls, and the Terminal window will record which ones are made.
    Using method calls you have seen in previous steps: construct a square; make it visible; and move it slowly 100 pixels to the left.
    In the Windows start menu, search for “WordPad” and select it. Copy and paste the method calls from your “Terminal” window into the blank document, and save it as “Lab1Work.txt”.
    Go to CSSubmit, find the submissions page for CITS1001, and submit the text file as “Lab 1”.
    This is the method that will be used to submit your project work for CITS1001. Note that the web page will give you feedback on whether you’ve correctly submitted the file it expects; however, for this lab, it won’t do any additional checking of what you have submitted.
  4. (challenge exercise) A challenge exercise means that this exercise might require additional knowledge or not be solved quickly. I do not expect everyone to be able to solve this at the moment. If you do, great. If you don’t, then don’t worry - things will become clearer in the following weeks. Come back to this exercise later.
    Open the Picture class and add fields to represent components of a picture. Add code to the draw method to create, manipulate, and display each of your components. Do not change the fields and method signatures that draw the house. Just change the method code, or add new fields and methods.
    If you have extra time then add code to the Picture class to make some (or all) of the components of your picture move.
    Hint: To help develop your code, select View/Show Terminal. This shows another window that BlueJ uses for text output. Then select Options/Record method calls in the terminal. This function will cause all method calls (in their textual form) to be written to the terminal. Now create the objects you will need for your picture and manipulate them. It doesn’t matter if you make mistakes - those calls can be deleted. Cut and paste the method calls you want to keep from the terminal window into the draw method of your Picture class.