Objects and Classes
Aim
To familiarise yourself with Bluej and with the Object and Class
concepts.
Exercises
Download the shapes and picture Bluej projects to your work area (make sure you unzip the files and download the folder in your work area. If you are unsure how to do this please ask the tutor). Start Bluej and open the shapes project. To start Bluej go to the start menu, all programs, programming, BlueJ 2.1.1 and BlueJ. Do the exercises 1.1 to 1.16 of the Bluej book.
-
1.1 Create two circles, then create a square. Make them visible.
-
1.2 What happens if you call moveDown twice? Ot three times? What happens if you call makeInvisible twice?
-
1.3 Try invoking the moveVertical, SlowMoveVertical, and changeSize methods. Find out how you can use moveHorizontal to move one of the circles 70 pixels to the left.
-
1.4 Invoke the changeColor method on one of your circle objects and enter the string "red". This should change the colour of the circle. Try other colours.
-
1.5 This is a very simple example, and many colours are not supported. See what happens when you specify a colour that is not known.
-
1.6 Invoke the changeColor method, and write the colour into the parameter field without the quotes. What happens?
-
1.7 Create several circle objects in the object bench. Make them visible. Move them around on the screen using the `move' methods. Make one green and yellow, make another one small and green. Try the other shapes too: create a few triangles and squares. Change their positions, sizes and colours.
-
1.8 Make sure you have several objects on the object bench and then inspect them in turn. Try changing the state of an object (for example calling the moveLeft method) while the object inspector is open. You should see the values in the object inpector change.
-
1.9 Use the shapes from the shapes project to create an image of a house and a sun, similar to that shown in Figure 1.7. While you are doing this, write down what you have to do to achieve it. Could it be done in different ways?
-
1.10 Open the picture project. Create an instance of class Picture and invoke its draw method. Also, try out the setBlackAndWhite and setColor methods.
-
1.11 How do you think the Picture class draws the picture?
-
1.12 Look at the pop up menu of the class Picture again. You will see an option labeled Open Editor. Select it. This will open a text editor displaying the source code of this class.
-
1.13 In the source code of class Picture find the part that actually draws the picture. Change it so that the sun will be blue rather than yellow.
-
Add a second sun to the picture. To do this, pay attention to the field definitions close to the top of this class. You need to add a line for the second sun there (private Circle sun2;). Then write the appropriate code for creating the second sun.
-
Add a sunset to the single-sun version of picture. That is: make the sun go down slowly. Remember: the circle has a method slowMoveVertical that you can use to do this.
-
If you added your sunset to the end of the draw method (so that the sun goes down automatically when the picture is drawn), change this now. We want the sunset in a separate method, so that we can call draw and see the picture with the sun up, and then call sunset (a separate method) to make the sun go down.
| Last updated at 8:55pm, Thursday September 22nd 2011 |
Dr Natalia Beloff (N.Beloff@sussex.ac.uk) |
|
|
|