Back to 111 Main Page

Mobius strip

Assignment 3

Task

Create a Car class, including instance variables, constructor, and methods with parameters.

Details:

We are going to create a very simple model of a demolition derby. So first we're going to need some cars.

Car.java

Write a Car class that includes the following details. Remember that Java is case-sensitive.

Instance variables (private):

  • descriptor -- a color or other descriptive text (String)
  • value -- the worth of a car in whole dollars (int)

Constructor (public):

  • Car(String, int) -- Write one constructor that takes two parameters: a descriptor and a value.

Methods (public):

  • String toString() -- Should return a string comprised of this car's descriptor + " car".
  • int getValue() -- Returns this car's current value.
  • void repair(int) -- Adds the given amount to this car's current value.
  • void crashInto(Car) -- Should reduce this car's value by 10%; should reduce the value of the parameter car by 50%.

Wherever I have used int above, you may use Integer instead if you prefer. I have given only the type of the parameters used in methods and constructors above; you will need to provide parameter variable names when you write your code.

Now, to test the new Car class, write a main method in the Car class. In this method, you should:

  • Create two new Cars
  • Print out their description and values. (For all such printing in this method, use the appropriate Car methods. The descriptor and value for each car should only occur in one place--as arguments/parameters to the constructor. Those values should not appear again in your main code. As an example, for the output below, the word "red" and the number 500 occur only once in my code.)
  • Crash the first car into the second by calling the crashInto method, something like this: first.crashInto(second);
  • Print out the cars' descriptions and values again.

When you run the Car class, your main method should produce output something like this:

Two new cars and their values:
red car: $500
blue car: $1000

After the red car smashes into the blue car:
red car: $450
blue car: $500

You can use your own descriptors and initial values. You can also add more to your main method after this, if you like. (For instance, you might want to test your repair method.)

DemolitionDerby.java

To provide further testing, you can run my DemolitionDerby.java. Save the .java file into your project folder. Then, in Eclipse, right-click the little blue project folder (on the left) and choose Refresh to get the new file to show up. Go to Run -> Run... to create a new run configuration.

If you get any compile errors, fix your Car class to match the specifications above. When you run DemolitionDerby, you should get exactly the following output:

Creating three cars: an orange car, a blue car, and a rusty old car.
They have the following values:
orange car: $300
blue car: $150
rusty old car: $100

After a short and furious competition, they have the following values:
orange car: $135
blue car: $121
rusty old car: $25

After quick repairs ($50 each), they have the following values:
orange car: $185
blue car: $171
rusty old car: $75

This ends the demolition derby.

What to Submit

Attach your Car.java file to an email.

FAQ

In DemolitionDerby, everything is correct except my blue car has a value of $1 more than shown above.

Depending how you do the math in crashInto, the blue car may have a value of $122 (and then $172) instead of $121 (and then $171). This is acceptable.

The correct answer is actually 121.5. If you calculate the value as (value * 9)/10, it's rounded down to 121. If you calculate it as value - (value/10), the rounding down occurs in the division, giving 122. Again, this highlights both the dangers of integer math as well as the maxim "there's more than one way to do it".

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
2 - Correct constructor
A Car should hold a String and an integer.
4 - Correct methods
1 - Main method as described
Again, it should use Car's getValue() and toString() to access details of each Car for printing.
2 - DemolitionDerby compiles and runs correctly using your Car.

Solution

Car.java



~ztomasze Index : TA Details: ICS111: Assignment 3
http://www2.hawaii.edu/~ztomasze
Last Edited: 21 Sep 2007
©2007 by Z. Tomaszewski.