CS 139
Algorithm Development
Lab12A: Let's Make Some Cars
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.
You will write this program from scratch.
You may refer to Gaddis, Chapter 6, Programming Challenges 2, either edition.
Setup your program environment. Create a new folder for your
files named lab12a.
Create a new program named Car.java.
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
.
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)
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
)
Some hints. First do not print anything in the method. It should only create and return the String.
String.format()
operates just like printf
. So you can use
this line directly to create a String.
Compile your Car class and correct any errors.
Create a CarTester class which contains a main method.
Create two Car objects of your choice. (Hint: you need to call the constructor with the values you want for your "dream cars").
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.)
Edit Car.java.
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.
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.
Edit CarTester.java.
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.
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.
For the first car, call brake 5 times in a row calling getSpeed() with each pass.
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.
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.
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)).
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.
Display both of the cars after the crash
()
using the toString
() method.
Updated 11/12/12 (nlh)