Practical Exercise 1 using BlueJ
Note that this exercise comes entirely from
Barnes and Koelling, Objects First, Chapter 1.
Before starting these exercises you should perform the following actions:
- Install BlueJ
- Copy (right click then save as) projects.zip to your home directory
(e.g.Z:\My Documents) and unzip it.
- Start BlueJ and open the project named shapes
- Right click on the circle class - you will see a list of possible operations
- If you do not see new Circle() as a possible operation, the class has not been compiled. In this case, compile the class, and then choose
- Click OK and observe what is happening. Now proceed to main questions
- Create another circle. Then create a square
- What happens if you call moveDown twice? What happens if you call makeInvisible twice?
- Try invoking moveVertical, slowMoveVertical, and changeSize methods. Find out how you can ue moveHorizontal to move the circle 70 pixels left.
ke 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.
- What happens when you specify a colour that is not known?
- Invoke the changeColor method and write the colour without quotes. What happens? Why?
- Create several circle objects on the object bench.
- Inspect the objects. Try changing the state of an object. Inspect to observe the effect.
- 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 p 11. While you are doing this, write down what you have to do to achieve this. Could it be done in different ways?
- Open the picture project. Create an instance of class Picture and invoke its draw method. Try the setBlackAndWhite and setColor methods.
- How do you think the Picture class draws the picture?
- Use the editor (right click; open editor) to open the Picture class.
- In the source code, identify the part that actually draws the picture. Change it so that the sun will be blue instead of yellow.
- Add a second sun to the picture.
- Add a sunset to the single-sun version of Picture. (Hint: use the method slowMoveVertical)
- Make sunset a separate method, so that we can call draw and see the picture with the sun up, and then call sunset and make the sun go down.
- Create an object of class Student. Fill in some appropriate values.
- Create some other student objects. Invoke the getName method on each object. Explain what is happening.
- Create an object of class LabClass. As the signature indicates, you need to
specify the maximum number of students in that class (integer).
- Call the numberOfStudents method. What does it do?
- Examine the method enrollStudent of the LabClass object. Now enroll some students.
- Call the printList method of the LabClass object. What happens?
- Create three students with the following details: Snow White, student ID 100234, credits: 24; Lisa Simpson, student ID 122044, credits 56; Charlie Brown, student ID 12003P, credits 6. Then enter all three into a lab and print a list to the screen.
- Use the inspector on a LabClass object to discover what fields it has.
- Set the instructor, room, and time for a lab, and print the list to a terminal window.
- What are the predefined datatypes used by java apart from int and String?
- What are the types of the following values: 0; "hello" 101; -1, true; "33"; 3.1415?
- How do you add a new field (e.g. name) to a circle object?
- Write the signature for a method named send that as one parameter of type String and does not return a value.
- Write the signature for a method named average that has two parameters, both of type int, and returns an int value.
- Is the book you are reading an object or a class. If it is a class, name some objects. If it is an object, name its class.
- Can an object have several different classes? Discuss.