CS 139 Algorithm Development
Lab12A: Let's Make Some Cars

Objectives

Students will be able to:

Background

Object Oriented programming switches the view of a program as a series of steps to a view of a program as an interaction between objects. You design the objects with what they should look like (attributes or fields) and what behaviors they should have (methods), then your program will control how those objects interact in the world.

Key Terms

Materials

You will write this program from scratch.

You may refer to Gaddis, Chapter 6, Programming Challenges 2, either edition.


Part 1 General Instructions:

  1. Setup your program environment. Create a new folder for your files named lab12a.

  2. Create a new program named Car.java.


Part 2 Create a basic Car class:

  1. Create three instance variables within the class: an int that holds the car's model year, a String that holds the car's make, an int that holds the car's speed. These should be listed as private.

  2. Create a constructor that accepts the model year and make and sets the speed to 0. Note that either you should make the constructor's parameters a different name than the instance variables or use the "this" qualifier when placing the parameter's value into the instance variable. (Chapter 9.8)

  3. Create a toString method that accepts NO parameters and returns a String. The String should be of the format("A %d %s that is going %d mph", year, make, speed)

    1. Some hints. First do not print anything in the method. It should only create and return the String.

    2. String.format() operates just like printf. So you can use this line directly to create a String.

  4. Compile your Car class and correct any errors.


Part 3 Test your Car.

  1. Create a CarTester class which contains a main method.

  2. Create two Car objects of your choice. (Hint: you need to call the constructor with the values you want for your "dream cars").

  3. Display the Car objects so that you know that your constructor is working correctly by calling the toString method and printing that result. (Hint, you will need to call the method with the Car objects that you created.)


Part 4 Refinement:

  1. Edit Car.java.

  2. Create accessor methods that let you access each individual element of the Car. They should be named with "get" followed by the name of the attribute. So one of your accessor methods would be getSpeed(). They should take no parameters and should return the value of that attribute.

  3. Create two mutator methods, accelerate() and brake(). These methods are void methods and take no parameters. When called, the accelerate() method should increase the speed variable by 5 miles per hour. The brake() method should decrease the speed variable by 5 miles per hour.


Part 5 Testing the refinement:

  1. Edit CarTester.java.

  2. For the first car, call the accelerate() method 5 times in a row and call the getSpeed() method in a print statement (with an appropriate label) for each increase.

  3. Do the same for the second car but accelerate only 3 times in a row also calling getSpeed() in a print statement for each acceleration.

  4. For the first car, call brake 5 times in a row calling getSpeed() with each pass.

  5. Print the result of a call to the toString() method for each of the cars. The first car should be at zero and the second at 15 mph.


Part 6 - (OPTIONAL) Just for fun:

  1. It is possible to pass one object into the method for another object. For example, the String method equals is called by a String object and is passed another String object: answer.equals("yes") for example.

  2. Add a mutator method to your Car class called crash that takes a Car object as a parameter and returns nothing. It should display the message "CRASH" and then set the speed of both the calling object and the called object to 0. (See Chapter 9.2 for examples of passing an object into a method and 9.8 for examples of referencing two objects' fields (instance variables)).

  3. In your tester, call accelerate for the first car until it is the speed of the second. Call the first car's crash() method passing in the second car.

  4. Display both of the cars after the crash() using the toString() method.


Updated 11/12/12 (nlh)