Question: How do I achieve the objective? With the car class create copy of the engine within the constructor ( you may write a copy constructor

How do I achieve the objective? "With the car class create copy of the engine within the constructor (you may write a copy constructor for the Engine class to accomplish this) and store that appropriately. The vehicle class already takes an Engine into its constructor. You will need to modify this so that it creates a deep copy (again, using the copy constructor) and stores that instead of the direct reference to the passed in Engine." I don't know how to start. This is the code so far:
import java.util.*;
public class RaceTrack2//driver
{
public final static int raceDuration =1000;
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;
}
abstract public void Go(); //this is correctly coded, the abstract method
public String toString()
{
return "Vehicle: "+VIN+" Progress: "+RaceProgress;
}
public boolean equals(Object other)
{
return this.VIN ==((Vehicle)other).VIN;
}
public void reset()
{
RaceProgress =0;
hasCrashed=false;//mainly for motorcycle; reset progress and crash
}
}
class Engine//move next to car class for easier referring
{
int speed;
public Engine(int speed)
{
this.speed = speed;
}
public int SpeedModifier()
{
//returns speed/2 to maxSpeed
return (int)(Math.random()*speed/2)+speed/2;
}
}
class Car extends Vehicle //car "is a" vehicle
{
//whatever comments were here
public Car(int i, Engine e, int passengers, int maxPassengers)//Working constructor
{
super(i,e,maxPassengers);
this.setPassengers(passengers);
}
public String toString()
{
return "Car::"+super.toString();
}
//This is overwriting the super/base classes method
//car satisfies the vehicles Go requirement
public void Go()
{//it calls upon this, so this needs a lot of this
this.setRaceProgress(this.getRaceProgress()+ engine.SpeedModifier()-10*(this.getPassengers()-1));
}
}
//Another subclass of Vehicle (this is considered a concrete class because it is not abstract)
class Truck extends Vehicle
{
//This is data that exists only in trucks
int towWeight;//special note that vehicle cannot access this. In order for it to do so a
//cast operation must be applied to a valid truck.
Truck(int i, Engine e, int passengers, int maxPassengers, int towWeight)
{
super(i, e, maxPassengers);
this.setPassengers(passengers);
this.towWeight = towWeight;
}
public String toString()
{
return "Truck::"+super.toString();
}
//truck satisfies the vehicles Go requirement
public void Go()
{/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!