Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

package derby;

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago