CompSci 18S - Spring 2008 - Objects, Methods and Classes

Classwork 2: January 14, 2008
Group Writeup (10 pts)

For group work, please turn in one sheet of paper with all the names of those who participated in your group.

Part 1: Using Objects and Designing Methods

For this part, assume an Artist class exists that provides the following methods for drawing lines and moving the pen.


  1. Consider the following code and draw the picture that corresponds to it.

    Artist picasso = new Artist();


  2. Consider the picture shown below. Write the code to generate this picture.

  3. Consider the picture shown below. Write the Java code to generate this picture. Think about what you could do to make your life easier in writing this code. Think about adding other types of statements (if, while) and/or writing a method.

  4. Consider the pictures of spirals shown below. The leftmost picture is a 1-spiral, the second picture is a 2-spiral (it is in a 2x2 unit square), the third picture a 3-spiral, and the fourth a 4-spiral.

    Write Java code for a method called drawSpiral(int size) that given size, draws a size-spiral. First think about how to give code for a 1-spiral, 2-spiral, 3-spiral and 4-spiral.

Part 2 - The Car Class

This exercise helps in understanding what happens when objects are declared and created, and when methods are called. It also focuses on the difference between copying a primitive type and copying an object.

Consider the following Java code that compiles and runs.

Look at the Car class and the DriveCars class and discuss them with your group. For the Car class, find the data members for the class. Find all the methods for the class.

Write answers to the following questions.

  1. In the Car class, examine the method changeSpeed. Where is the variable currSpeed declared?

  2. In the Car class, which methods are mutator methods and which methods are accessor methods?

  3. In the main method of DriveCars, how many variables of type Car are there?

  4. In the main method of DriveCars, how many Car objects are created?

  5. What is the output when the main method of DriveCars is executed?

  6. Cars have headlights. How could you represent the information that the headlights are on or off? How could you modify the Car class to be able to indicate whether the headlights are on or off?