Back to 111 Main Page

Mobius strip

Assignment 7

Task

Explore object-oriented programming by creating objects and changing their state (instance variables).

Details

We are going to create a very simple model of a demolition derby.

DerbyCar.java

First we need some cars. I have already written a DerbyCar class for you: DerbyCar.java.

Download this file and use it in writing your demolition derby (below). Do not change DerbyCar.

Assignment7.java

Now, write a short demolition derby as follows:

  • Create three new DerbyCars. (These are the only three variables you need to declare for this assignment.) Initialize the four data fields for each car. You may use the following details, or create your own:
    [ID = 11; Driver = "Gimli"; Description = "Killer Axe"; Value = $300]
    [ID = 12; Driver = "Bilbo"; Description = "Ol' Hobbit"; Value = $200]
    [ID = 13; Driver = "Boromir"; Description = "Steward's Heir"; Value = $160]
    
  • Print out the details of the objects you created using their instance variables. (I strongly recommend you write a static method that takes a DerbyCar as a parameter and prints that car's details to the screen.)
  • Crash the first car into the second car; then crash the first car into the third car. For each crash, reduce the value of the car performing the crash by 10%, and reduce the value of the car hit by 50%. (Do this separately for each crash to get the correct results.)
  • Print out the cars' descriptions and values again.
  • Repair the last two cars (which were the victims of the first car's crashes) by $50 each.
  • Print out the cars' descriptions and values again.

Assuming the values given above, your demolition derby should print something like this:

Created 3 cars:
Car 11 ["Killer Axe", driven by Gimli. Value: $300.0]
Car 12 ["Ol' Hobbit", driven by Bilbo. Value: $200.0]
Car 13 ["Steward's Heir", driven by Boromir. Value: $160.0]

Smashed Killer Axe into the other two cars.

After crashes:
Car 11 ["Killer Axe", driven by Gimli. Value: $243.0]
Car 12 ["Ol' Hobbit", driven by Bilbo. Value: $100.0]
Car 13 ["Steward's Heir", driven by Boromir. Value: $80.0]

Repaired Ol' Hobbit and Steward's Heir by $50 each.

After repairs:
Car 11 ["Killer Axe", driven by Gimli. Value: $243.0]
Car 12 ["Ol' Hobbit", driven by Bilbo. Value: $150.0]
Car 13 ["Steward's Heir", driven by Boromir. Value: $130.0]

What to Submit

Attach your Assignment7.java file to an email. (I already have a copy of DerbyCar.java, so you don't need to send me that.)

FAQ

How do I make this static method for printing? Do I have to pass each field to the method?
No, the point of having objects is so that you can pass one object around with all the instance variables inside it. So, you can write a method like this:
public static printCar(DerbyCar car) {
  //print out car.IDNumber, car.description, etc.
}
And then in main, you can call printCar and pass in a whole car object to be printed, like this:
DerbyCar firstCar = new DerbyCar();
//initialize all the fields of firstCar, and then
printCar(firstCar);

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
2 - Coding Standards
Follows required coding standards.
2 - Compiles
And works with the provided version of DerbyCar.java
2 - Prints details (all 4 data fields)
Does this initially, after the crashes, and after the repairs. Uses the 3 objects (and their instance variables) to do this.
2 - Crashes
Reduces the value of the attacking car by 10%, and reduces the value of the victim car by 50%. Uses the appropriate objects (and their instance variables) to do this.
1 - Repairs
Adds $50 to the value of last two cars. Uses the appropriate objects (and their instance variables) to do this.


~ztomasze Index : TA Details: ICS111: Assignment 7
http://www2.hawaii.edu/~ztomasze
Last Edited: 22 May 2008
©2008 by Z. Tomaszewski.