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.
Creating a “Shapes” project in BlueJ.
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.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.Square
, select new Square(), and click Ok. This creates an object square1
of class Square
.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
.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?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!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.square1
. Think about the way that they work, why some take arguments and others don’t, etc.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.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?
(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.