import java.util.Scanner; public class Race { public static void main(String[] Args) { // Read information from the user XXXXXXXXXXScanner scanIn = new Scanner(System.in);...

1 answer below »
Java language


import java.util.Scanner; public class Race { public static void main(String[] Args) { // Read information from the user Scanner scanIn = new Scanner(System.in); System.out.print("\nHow long is the race > "); double length = scanIn.nextDouble(); System.out.print("\nRace with Speedsters (yes/no) > "); boolean speedster = (scanIn.next().toLowerCase()).equals("yes"); System.out.print("\nRace with Jalopies (yes/no) > "); boolean jalopy = (scanIn.next().toLowerCase()).equals("yes"); // Build an array of racers Vehicle[] contestants = new Vehicle[8]; contestants[0] = new RaceCar(110, "Corvette", 2); contestants[1] = new RaceCar(111, "Trans-Am", 1); contestants[2] = new RaceCar(109.99, "Cruiser"); contestants[3] = new RaceCar(110.2, "BumbleBee", 3); // Only add jalopies if the user wants /* UNCOMMENT ME FOR JALOPIES TO WORK if (jalopy) { contestants[4] = new Jalopy(113.2, "Subaru", 3); contestants[7] = new Jalopy(113.3, "Toyota", 4); } */ // Only add speedsters if the user wants /* UNCOMMENT ME FOR SPEEDSTERS TO WORK if (speedster) { contestants[5] = new Speedster(120.3, "Mach-5"); contestants[6] = new Speedster(122.2, "Ford"); } */ int carNum = 0; int i = 0; double min = contestants[0].timeToFinish(length); double time = 0.0; for (Vehicle car : contestants) { if (car != null) { time = car.timeToFinish(length); System.out.print("" + contestants[i]); System.out.printf(" finished in %3.2f Minutes\n\n", time); if (time < min)="" {="" min="time;" carnum="i;" }="" }="" i++;="" }="" system.out.print("the="" winner="" is="" ******\n"="" +="" contestants[carnum]="" +="" "="" with="" a="" time="" of="" ");="" system.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) { this.speed = speed; this.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 worried about units) * you 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) { this.speed = speed; } /** * Change the name of the vehicle * @param name new name for this vehicle */ public void setName(String name) { this.name = name; } /** * Find the name of this vehicle * @return vehicle's name */ public String getName() { return name; } /** * Find the max speed of this vehicle * @return vehicle's speed */ public double getSpeed() { return speed; } public String toString() { return name + " with a speed of " + speed; } }
Answered Same DayOct 23, 2021

Answer To: import java.util.Scanner; public class Race { public static void main(String[] Args) { // Read...

Arun Shankar answered on Oct 24 2021
151 Votes
Main.java
Main.java
class Main {
  public static void main(String[] args) {

    Race r = n
ew Race(2.3, "tesla", 5, 4);
    System.out.println(r.timeToFinish(20));
  }
}
Race.java
Race.java
class Race extends Vehicle
{
  double slower;
  double boost;
  // Constructor 
  Race(double _speed, String _name, double a, double b)
  {
    super(_speed, _name);
    slower = a;
    boost = b;
  }
  public double timeToFinish(double length)
  {
    double s = super.getSpeed() + boost - slower;
    return length / s;
  }
}
Vehicle.java
Vehicle.java
/**
 * Models a simple vehicle.
 */
public abstract class Vehicle {
    /** Speed of the ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here