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.
Create a new program named Car.java.
Create the Car class header and documentation.
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
. Private means that only methods within this class may use the variables. Any other class can only access the variable values indirectly through methods of the class.
Create a constructor (see video) that accepts the model year and make as parameters 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.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. Add error checking such that no matter how many times we accelerate the speed does not exceed 150 mph. (Hmmm, this might make a good constant value...add the constant to the attribute area and call it MAX_SPEED).
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. Add error checking such that no matter how many times we accelerate the speed does not exceed 150 mph. (Hmmm, this might make a good constant value...add the constant to the attribute area and call it MAX_SPEED). Also, insure that your brake method should not allow the speed to drop below 0 miles per hour. These error checks should not print anything, but they should insure that the data remains in a reasonable state for the object (speed between 0 and 150).
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 8.2 for examples of passing
an object into a method and 8.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 (15 mph). 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/10/14 (nlh)