Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I achieve the final objective? With the truck class change the constructor so that an int for the horsepower is passed into the

How do I achieve the final objective? "With the truck class change the constructor so that an int for the horsepower is passed into the truck constructor instead of the entire engine (this is the only data needed to create an engine) use this to create the engine within the vehicle (truck can pass the int up into vehicle if you add a constructor for this in vehicle). Since truck/Vehicle creates its own engine within the object it is safer code than creating it in main or who-knows-where."
I achieved the other objectives, but I have no idea how to start. This is the code:
import java.util.*;
/**
* 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 RaceTrack2//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
Engine engine = new Engine(67);
Vehicle c1= new Car(1,engine,2,5); //this one I'm storing as the base class (vehicle)
Car c2= new Car(2,new Engine(94),4,5);
Truck t1= new Truck(3,new Engine(87),2,3,250);
Motorcycle m1= new Motorcycle(4, new Engine(90),2,1,0.05f);
//0.02- motorcycle won every single season. 0.05- other vehicles have a chance
//This is an array
Vehicle[] allVehicles = new Vehicle[4];
//placing the vehicles into an array
allVehicles[0]= c1; //polymorphism ("isa")
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.
allVehicles[3]= m1;
int wins[]= new int[4];//store the number of wins
for(int stage=0; stage<20;stage++){
boolean raceInProgress=true;
System.out.println("STAGE "+(stage+1));
while (raceInProgress)
{
int max=0;
//tell the cars to "go" one by one
for (int i=0; i raceDuration)
{
System.out.print("Winner of Stage "+(stage+1)+":
*** Vehicle "+RaceTrack2.GetFurthestVehicle(allVehicles)+"***
");
wins[RaceTrack2.GetFurthestVehicle(allVehicles)-1]++;
System.out.println();
for(int a=0;a < allVehicles.length;a++){
allVehicles[a].reset();
}
raceInProgress=false;
}
}System.out.println();
}
int pole=0, wind=0;//check to see who had the most wins
for(int i=0;i<4;i++){
System.out.println("Vehicle "+ allVehicles[i].VIN +": "+ wins[i]+" wins");
if(wins[i]>wind){
wind = wins[i];
pole=i;
}
}
System.out.println("SEASON WINNER: VEHICLE "+(pole+1));
}
//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 maxOccupancy){
System.out.println("Over capacity; only the maximum number of passengers will be allowed on.
All others must leave the vehicle.");
this.passengers=maxOccupancy;
}else{
this.passengers=passengers;}
}
public int getRaceProgress(){
return RaceProgress;
}
public void setRaceProgress(int raceProgress){
this.RaceProgress = raceProgress;
}
//Constructors (used to create the objects):
Vehicle(int vin, Engine horsePower, int maxPass)
{
passengers =1;
RaceProgress =0;
VIN = vin;
engine = horsePower;
maxOccupancy = maxPass;
}
/**
* This is the main function that progresses the vehicles through the race
* this should be called each loop of the program (this must be redefined in each
* subclass of vehicle)
*/
abstract public void Go(); //this is correctly coded, the abstract method only has its header, and no body - the body must be overwritten in subclasses
/**
* Part of object. This is invoked when an instance of this class is attempted to be used as a string (like during System.out.println)
*/

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

What are some of the benefits of being a critical thinker? (p. 231)

Answered: 1 week ago

Question

LO5 Explain how to generate effective recruitment advertisements.

Answered: 1 week ago