Question: In Java: Write an application that models a demolition derby. Demolition derby events involve drivers intentionally colliding with each other in hopes of causing damage

In Java:

Write an application that models a demolition derby. Demolition derby events involve drivers intentionally colliding with each other in hopes of causing damage to other vehicles. The last vehicle in running condition is the victor. A. Use this Vehicle class without changes other than imports and a package declaration: import java.util.Random; public abstract class Vehicle { protected Random r = new Random(); protected boolean running = true; protected int id; protected int sizeInTons; // 1 to 10 protected double maneuverability; // 0 to 1 protected double condition = 10; // 0 to 10 private static int numVehicles = 0; public Vehicle() { numVehicles++; id = numVehicles; } public boolean isRunning() { return running; } public int getId() { return id; } public int getSizeInTons() { return sizeInTons; } public double getManeuverability() { return maneuverability; } public void receiveCrashDamage(double damageDone) { System.out.printf("Crash! Vehicle %d is hit and ", id); if (damageDone < condition) { condition -= damageDone; System.out.printf("receives %.2f damage; current condition %.2f ", damageDone, condition); } else { condition = 0; running = false; System.out.println("is wrecked!"); } } public abstract void attemptRunDown(Vehicle v); } Notice that crashDamage() reduces the condition of the car and that if the condition reaches zero, the car is no longer running. Also note that the constructor, which will be called automatically by the subclass constructors, increments numVehicles and then sets the id to numVehicles. B. Write two classes that extend Vehicle. Our demolition derby includes both Cars and Trucks. Cars are more maneuverable than Trucks, but Trucks do more damage in collisions. Truck with hitches also do a little extra damage. Car has a constructor that sets size using a parameter and always sets maneuverability to .8. Its attemptRunDown() method prints a message including its toString() and the toString of the vehicle it is attempting to run down with an indication of who is running down whom, for example: #1, a car weighing 2 ton(s) with maneuverability 0.7 and condition 1.81 attempts to run down #6, a truck weighing 5 tons with maneuverability 0.4 and condition 7.44 Car's attemptRunDown(Vehicle v) method then gets a random double (r.nextDouble()) and compares it to the maneuverability of v, the vehicle it is trying to run down. If the random number is greater than v's maneuverability, v is not able to evade the attack and a crash occurs. Note that this is different from the way the attack() method worked in the sample exam. The damage from a crash is the size in tons of the current vehicle divided by the size in tons of v; call v.crashDamage() sending the amount of damage as the parameter. If you get damage values of 0.00, consider what might be wrong. If there is no crash, print a message that indicates this. Truck has an additional boolean variable called hasHitch. Truck's constructor sets maneuverability to .35 and uses its two parameters to set the sizeInTons and hasHitch. Truck's attemptRunDown() method works like Car's, except that if the truck has a hitch, it does an extra .05 of damage in every collision. Write reasonable toString() methods for both the vehicle subclasses. Truck's toString() must show whether or not the truck has a hitch. It is OK if the double variables print with varying precisions, but if this bothers you, use code like this: DecimalFormat f = new DecimalFormat("##.00"); String myString = f.format(condition); C. Write the Derby class The Derby class has a list of vehicles, an instance of Random, a drive() method, and a main() that instantiates a Derby and calls drive(). drive() is an instance, not static, method, because there could be many demolition derbies occurring at the same time. It instantiates five Cars and three Trucks and adds them to the list of vehicles. Send the Car constructor a random choice of 1 or 2 (rand.nextInt(2) + 1) as the size parameter. Send the Truck constructor a random size between 2 and 5 (rand.nextInt(4) + 2) and a random boolean (rand.nextBoolean()) for hasHitch. Once the vehicles are in the list, the derby can begin. As long as there are at least two vehicles left, repeatedly choose two vehicles, v1 and v2, at random, and call v1.attemptRunDown(v2). Make sure no vehicle tries to run itself down. After each attempt, see if v2 is still running. If not (i.e. if it was just wrecked), remove it from the list. Note that this is different from the battle in the sample exam, where we kept dead zombies in the list. When only one vehicle is left, print a message with its toString() indicating that it has won. Here is a sample of the output from a run of my solution: #7, a truck weighing 3 tons with a hitch with maneuverability 0.35 and condition 10.00 attempts to run down #1, a car weighing 1 ton(s) with maneuverability 0.8 and condition 10.00 Misses! #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 10.00 attempts to run down #8, a truck weighing 4 tons with maneuverability 0.35 and condition 10.00 Misses! #4, a car weighing 2 ton(s) with maneuverability 0.8 and condition 10.00 attempts to run down #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 10.00 Misses! [many lines omitted] #2, a car weighing 2 ton(s) with maneuverability 0.8 and condition .45 attempts to run down #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 1.50 Misses! #2, a car weighing 2 ton(s) with maneuverability 0.8 and condition .45 attempts to run down #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 1.50 Misses! #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 1.50 attempts to run down #2, a car weighing 2 ton(s) with maneuverability 0.8 and condition .45 Misses! #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 1.50 attempts to run down #2, a car weighing 2 ton(s) with maneuverability 0.8 and condition .45 Crash! Vehicle 2 is hit and is wrecked! The victor is #5, a car weighing 2 ton(s) with maneuverability 0.8 and condition 1.50 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!