import java.util.Scanner;
public class Race {
public static void main(String[] Args) {
Read information from the use
XXXXXXXXXXScanner scanIn = new Scanner(System.in);
XXXXXXXXXXSystem.out.print("\nHow long is the race > ");
XXXXXXXXXXdouble length = scanIn.nextDouble();
XXXXXXXXXXSystem.out.print("\nRace with Speedsters (yes/no) > ");
XXXXXXXXXXboolean speedster = (scanIn.next().toLowerCase()).equals("yes");
XXXXXXXXXXSystem.out.print("\nRace with Jalopies (yes/no) > ");
XXXXXXXXXXboolean jalopy = (scanIn.next().toLowerCase()).equals("yes");
Build an a
ay of racers
XXXXXXXXXXVehicle[] contestants = new Vehicle[8];
XXXXXXXXXXcontestants[0] = new RaceCar(110, "Corvette", 2);
XXXXXXXXXXcontestants[1] = new RaceCar(111, "Trans-Am", 1);
XXXXXXXXXXcontestants[2] = new RaceCar(109.99, "Cruiser");
XXXXXXXXXXcontestants[3] = new RaceCar(110.2, "BumbleBee", 3);
Only add jalopies if the user wants
/* UNCOMMENT ME FOR JALOPIES TO WORK
XXXXXXXXXXif (jalopy) {
XXXXXXXXXXcontestants[4] = new Jalopy(113.2, "Subaru", 3);
XXXXXXXXXXcontestants[7] = new Jalopy(113.3, "Toyota", 4);
}
*
Only add speedsters if the user wants
/* UNCOMMENT ME FOR SPEEDSTERS TO WORK
XXXXXXXXXXif (speedster) {
XXXXXXXXXXcontestants[5] = new Speedster(120.3, "Mach-5");
XXXXXXXXXXcontestants[6] = new Speedster(122.2, "Ford");
}
*
XXXXXXXXXXint carNum = 0;
XXXXXXXXXXint i = 0;
XXXXXXXXXXdouble min = contestants[0].timeToFinish(length);
XXXXXXXXXXdouble time = 0.0;
XXXXXXXXXXfor (Vehicle car : contestants) {
XXXXXXXXXXif (car != null) {
XXXXXXXXXXtime = car.timeToFinish(length);
XXXXXXXXXXSystem.out.print("" + contestants[i]);
XXXXXXXXXXSystem.out.printf(" finished in %3.2f Minutes\n\n", time);
XXXXXXXXXXif (time < min) {
XXXXXXXXXXmin = time;
XXXXXXXXXXcarNum = i;
}
}
XXXXXXXXXXi++;
}
XXXXXXXXXXSystem.out.print("The WINNER is ******\n" + contestants[carNum] +
" with a time of ");
XXXXXXXXXXSystem.out.printf("%3.2f minutes\n", min);
}
}
**
* Models a simple vehicle.
*
public abstract class Vehicle {
/** Speed of the vehicle *
private double speed;
/** Name of the vehicle *
private String name;
/**
*
Default
constructor.
* @param speed speed of the vehicle
* @param name name of the vehicle
*
public Vehicle(double speed, String name) {
XXXXXXXXXXthis.speed = speed;
XXXXXXXXXXthis.name = name;
}
/**
* In genreral the time it takes to finish a race is the length of the
* course divided by the car's speed. The speed of a car is found by
* taking the default speed and adding any speedboost and subtracting
* anything that slows the car down.
*
* @param length length of the track (not wo
ied about units)
* XXXXXXXXXXyou can assume miles.
*
public abstract double timeToFinish(double length);
/**
* Change the speed of the vehicle
* @param speed new speed for this vehicle
*
public void setSpeed(double speed) {
XXXXXXXXXXthis.speed = speed;
}
/**
* Change the name of the vehicle
* @param name new name for this vehicle
*
public void setName(String name) {
XXXXXXXXXXthis.name = name;
}
/**
* Find the name of this vehicle
* @return vehicle's name
*
public String getName() {
XXXXXXXXXXreturn name;
}
/**
* Find the max speed of this vehicle
* @return vehicle's speed
*
public double getSpeed() {
XXXXXXXXXXreturn speed;
}
public String toString() {
XXXXXXXXXXreturn name + " with a speed of " + speed;
}
}