Back to 111 Main Page

Mobius strip

Assignment 9

Task

Explore overloaded constructors, instance methods, encapsulation, and static variables.

Steps

You are going to expand the DerbyCar class from Assignment 7 by adding methods and encapsulating the instance variables.

Write a class named DerbyCar with the following details:

Instance variables. A DerbyCar should have four instance variables to hold an int ID number, a String driver name, a String description, and a double dollar value. You may name these variables whatever you like, but they should all be private.

Static variable. A DerbyCar should have a single private static int variable that provides consecutive ID numbers for each car. It should start the count at 10 and be incremented once each time a new DerbyCar is created.

Constructors. A DerbyCar should have two constructors:

  • public DerbyCar(String driver, String desc, double value)
  • public DerbyCar(String driver, double value)

Both set the new car's ID number automatically to be one more than the last car created. (That is, the first car should be numbered 10, the next 11, and so on.) The second constructor sets the car's description to "Car ##", where ## is that car's ID number.

Accessor and mutator instance methods. A DerbyCar should have the following public "getter and setter" instance methods:

  • public int getIDNumber()
  • public String getDriver()
  • public void setDriver(String driver)
  • public String getDescription()
  • public void setDescription(String desc)
  • public double getValue()

Note how the ID number and value of a car cannot be set directly.

Other instance methods.

  • public void crashInto(DerbyCar target) -- Reduces the value of this attacking car by 10%, and reduces the value of the target victim car by 50%.
  • public void repair(double amount) -- Repairs this car, increasing its value by the given amount.
  • public String toString() -- Returns a String representation of this car clearly showing the values of its four instance variables. (See sample output below for an example.)

Testing

If you like, you can also add a main method to your DerbyCar class. In that method, you can write your own tests for your other DerbyCar methods.

Your DerbyCar must compile with the following tester program: DemolitionDerby.java. (This verifies that you correctly matched the method signatures specified above.) The program creates a few cars and calls all the various DerbyCar methods. While your output does not need to be formatted exactly as below, your values should be the same if your code is correct.

Sample Output

Created 3 cars:
Car 10 ["Batmobile", driven by Batman. Value: $3000.0]
Car 11 ["Herkimer Battle Jitney", driven by the Mystery Men. Value: $2000.0]
Car 12 ["KITT", driven by Michael Knight. Value: $2500.0]

Smashed Batmobile into the other two cars.
After crashes:
Car 10 ["Batmobile", driven by Batman. Value: $2430.0]
Car 11 ["Herkimer Battle Jitney", driven by the Mystery Men. Value: $1000.0]
Car 12 ["KITT", driven by Michael Knight. Value: $1250.0]

Repaired Herkimer Battle Jitney by $500.
Repaired KITT by $800.
After repairs:
Car 10 ["Batmobile", driven by Batman. Value: $2430.0]
Car 11 ["Herkimer Battle Jitney", driven by the Mystery Men. Value: $1500.0]
Car 12 ["KITT", driven by Michael Knight. Value: $2050.0]

Updating car details:
Driver of Car 10 (Batman) will be replaced by Robin.
Changing description of Car 11
Adding a late entry (with a value of only $0.5).
After changes:
Car 10 ["Batmobile", driven by Robin. Value: $2430.0]
Car 11 ["Finest Nonlethal Military Vehicle Ever Made", driven by the Mystery Men
. Value: $1500.0]
Car 12 ["KITT", driven by Michael Knight. Value: $2050.0]
Car 13 ["Car 13", driven by Tester John. Value: $0.5]

What to submit

Attach your DerbyCar.java to an email.

FAQs

Do you have any examples of this sort of thing?
These copies of Person.java and PersonTester.java have been expanded from the Assignment 8 examples. Person now demonstrates overloaded constructors and a static counter variable.
Could you tell me what setDescription and setDriver is? I'm not too sure what I am suppose to do with those methods.
These are mutator methods that let you change the value of a car's description or driver. The current value is replaced by the value passed in as a parameter. I've added a setYearOfBirth method to the Person class to show you what this might look like:
  public void setYearOfBirth(int yob) {
    this.yearOfBirth = yob;
  }
How do I set a new car's ID number? Something needs to be incremented here? Do I need two variables to do this?
You do need to use two separate variables here.

The first is a static variable that acts as a counter. This is the source of each consecutive ID number. (You can think of this variable as holding the ID number of the next car to be created.) This variable will start at 10 and be incremented every time the constructor is run. Have a look at the counter variable in the Person class above for an example of such a variable. Note that the counter in Person is initialized to 0; in a DerbyCar, it will start at 10.

Remember that a static variable is associated with the class, and so all instances share a copy of this variable. This means we can not directly use this static variable as a car's ID number. If we did, every car's ID number would be incremented each time another car was created.

So each car needs an instance variable that holds that car's personal ID number. Each car's ID number is assigned from the counter variable in the car's constructor:
  this.IDNumber = DerbyCar.counter;
(Again, don't forget to increment the counter after this.)
How do I write crashInto and repair?
In A7, you had to perform these operations manually in the main method. (Remember that the solution to A7 has been posted.) For instance, to repair car1 by $100, you would have had to do something like this:
    car1.value = car1.value + 100;
In other words, you are increasing the value field of the car1 car by 100.

For A9, you are moving this behavior into a DerbyCar method. If you look at the DemolitionDerby code, car1 can now be repaired by $100 like this:
    car1.repair(100);
Of course, for this to work, you need to write the repair method in the DerbyCar class. Remember that, when writing an instance method, this refers to the object the method was invoked upon. So, assuming that Java is currently trying to run the line car1.repair(100), the variables within the repair method contain something like this:
  public void repair(double amount) {
    //this == car1 (in main)
    //amount == 100
  }
On the other hand, if we executed the line car3.repair(500.75), then the repair method would run with the following information instead:
  public void repair(double amount) {
    //this == car3 (in main)
    //amount == 500.75
  }
Therefore, you can write the repair method with a single line that increases the value field of the "this" car by the passed amount.

Similarly, you can keep the following in mind while writing the crashInto method:
  public void crashInto(DerbyCar target) {
    //this == "the attacking car" (reduce its value by 10%)
    //target == "the target car" (reduce its value by 50%)
  }
How do I write the toString method?
Instead of printing out the four details of a car as you did in A7, you are going to return a String containing those details. In short, instead of:
  System.out.println(this.IDNumber + " " + ...);
you'll have:
  return this.IDNumber + " " + ...;
See the Person class for an example toString method. (Sorry, I went over this one the board in class, but I've only just now added the code to the Person class.)

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Coding Standards
Follows required coding standards.
3 - Compiles
Runs with DemolitionDerby.java
1 - Four private instance variables and one private static variable
As requested above
1 - Constructors
Has the two requested constructors that properly initialize all of a new car's fields.
1.5 - Six accessor and mutator methods
Each correctly returns or sets the appropriate instance variable. (Value and ID number cannot be set.)
1.5 - Other instance methods
crashInto, repair, and toString each performs correctly.


~ztomasze Index : TA Details: ICS111: Assignment 9
http://www2.hawaii.edu/~ztomasze
Last Edited: 10 Mar 2008
©2008 by Z. Tomaszewski.