Practical Exercise 1 using BlueJ

Note that this exercise comes entirely from Barnes and Koelling, Objects First, Chapter 1.

Before Starting

Before starting these exercises you should perform the following actions:
  1. Install BlueJ
  2. Copy (right click then save as) to your home directory (e.g.Z:\My Documents) and unzip it.
  3. Start BlueJ and open the project named shapes
  4. Right click on the circle class - you will see a list of possible operations
  5. 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 new Circle()
  6. Click OK and observe what is happening. Now proceed to main questions

Main Exercise

  1. Create another circle. Then create a square
  2. What happens if you call moveDown twice? What happens if you call makeInvisible twice?
  3. 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.
  4. What happens when you specify a colour that is not known?
  5. Invoke the changeColor method and write the colour without quotes. What happens? Why?
  6. Create several circle objects on the object bench.
  7. Inspect the objects. Try changing the state of an object. Inspect to observe the effect.
  8. 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?

    Picture Class

  9. Open the picture project. Create an instance of class Picture and invoke its draw method. Try the setBlackAndWhite and setColor methods.
  10. How do you think the Picture class draws the picture?
  11. Use the editor (right click; open editor) to open the Picture class.
  12. In the source code, identify the part that actually draws the picture. Change it so that the sun will be blue instead of yellow.
  13. Add a second sun to the picture.
  14. Add a sunset to the single-sun version of Picture. (Hint: use the method slowMoveVertical)
  15. 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.
  16. Create an object of class Student. Fill in some appropriate values.
  17. Create some other student objects. Invoke the getName method on each object. Explain what is happening.

    LabClass Project

  18. Create an object of class LabClass. As the signature indicates, you need to specify the maximum number of students in that class (integer).
  19. Call the numberOfStudents method. What does it do?
  20. Examine the method enrollStudent of the LabClass object. Now enroll some students.
  21. Call the printList method of the LabClass object. What happens?
  22. 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.
  23. Use the inspector on a LabClass object to discover what fields it has.
  24. Set the instructor, room, and time for a lab, and print the list to a terminal window.

    General Questions

  25. What are the predefined datatypes used by java apart from int and String?
  26. What are the types of the following values: 0; "hello" 101; -1, true; "33"; 3.1415?
  27. How do you add a new field (e.g. name) to a circle object?
  28. Write the signature for a method named send that as one parameter of type String and does not return a value.
  29. Write the signature for a method named average that has two parameters, both of type int, and returns an int value.
  30. 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.
  31. Can an object have several different classes? Discuss.