Question: Your task is to take the race track program presented in class and add several parts to it. Add a new class called Motorcycle .
Your task is to take the race track program presented in class and add several parts to it.
Add a new class called Motorcycle. It should extend vehicle which will allow it to be a part of the array with other vehicles. Motorcycles have great speed (the speed you should set for your motorcycle instance should be 95) but are dangerous. Redefine ModifySpeed method so that the motorcycle will travel a random amount between 0 and their max speed. Additionally, since motorcycle extends vehicle you must redefine Go. Do so like Car, and Truck but add a conditional statement that checks for the motorcycles chance to crash which is 2% per loop. If the motorcycle crashes then it should not progress in the race any further (but it will restart in any future races).
Next we need to redefine the way passengers are kept track of in all vehicles. First passengers should be a private variable which can only be accessed by appropriate accessor and mutator methods (also called getter and setter). Finally we should add a new final variable for the max occupancy of the vehicle. The max occupancy should be checked in the mutator method for passengers. The max occupancy should be defined as:
Car = 5
Truck = 3
Motorcycle = 1
If more passengers than are allowed attempt to pile into a vehicle the program should indicated this to the user and continue with the max passenger amount.
We want to change the race program from being 1 race to being a whole season. Do so by adding a looping structure to the main method. The season is defined as 25 races, and at the completion of all races the vehicle with the most wins should be printed (if 2 vehicles tie some tie-breaker should be conducted). For this section you may want to change when the vehicles get printed out so that they only print after races.
The following code is what needs to be changed.
package RaceTrackOriginal; /** * This is the program driver (where the program starts) * It is in charge of creating the race and it's participants and telling them to "go" in the race. * @author vjl8401 */ public class RaceTrack //driver { public final static int raceDuration = 1000; //store the length of the race (can be accessed anywhere in code) public static void main(String arg[]) { //Instantiating my object instances Vehicle c1 = new Car(1,3,85); //this one I'm storing as the base class (vehicle) Polymorphism says I can do this and it is only intended as an example Car c2 = new Car(2,4,100); //this one I store as the sub class (car). There are differences with the one above but they do not come into play here Truck t1 = new Truck(3,2,90,250); //This is an array of the Vehicle datatype Vehicle[] allVehicles = new Vehicle[3]; //placing the vehicles into an array allVehicles[0] = c1; allVehicles[1] = c2; //remember just because I store these as vehicles doesn't mean that the allVehicles[2] = t1; //methods for them has changed. Each still stores its own go method. //infinite loop (well without the base case it is) while (true) //this will run until a race participant crosses the finish line (passes raceDuration) { int max=0; //tell the cars to "go" one by one for (int i=0; i raceDuration) { break; } } System.out.println("We have a winner!!! *** Vehicle "+RaceTrack.GetFurthestVehicle(allVehicles)+" ***"); } //just a helper method to find out which vehicle won the race public static int GetFurthestVehicle(Vehicle[] allVehicles) { int max=0; int VIN=0; for (int i=0; i Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
