Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 RaceTrackdriver
public final static int raceDuration ;
public static void mainString arg
Instantiating my object instances
Engine engine new Engine;
Vehicle c new Carengine,; this one Im storing as the base class vehicle
Car c new Carnew Engine;
Truck t new Trucknew Engine;
Motorcycle m new Motorcycle new Enginef;
motorcycle won every single season. other vehicles have a chance
This is an array
Vehicle allVehicles new Vehicle;
placing the vehicles into an array
allVehicles c; polymorphism isa
allVehicles c; remember just because I store these as vehicles doesn't mean that the
allVehicles t; methods for them has changed. Each still stores its own go method.
allVehicles m;
int wins new int;store the number of wins
forint stage; stage;stage
boolean raceInProgresstrue;
System.out.printlnSTAGE stage;
while raceInProgress
int max;
tell the cars to go one by one
for int i; i raceDuration
System.out.printWinner of Stage stage:
Vehicle RaceTrackGetFurthestVehicleallVehicles
;
winsRaceTrackGetFurthestVehicleallVehicles;
System.out.println;
forint a;a allVehicles.length;a
allVehiclesareset;
raceInProgressfalse;
Systemout.println;
int pole wind;check to see who had the most wins
forint i;i;i
System.out.printlnVehicle allVehiclesiVIN : winsi wins";
ifwinsiwind
wind winsi;
polei;
System.out.printlnSEASON WINNER: VEHICLE pole;
just a helper method to find out which vehicle won the race
public static int GetFurthestVehicleVehicle allVehicles
int max;
int VIN;
for int i; i maxOccupancy
System.out.printlnOver capacity; only the maximum number of passengers will be allowed on
All others must leave the vehicle.";
this.passengersmaxOccupancy;
else
this.passengerspassengers;
public int getRaceProgress
return RaceProgress;
public void setRaceProgressint raceProgress
this.RaceProgress raceProgress;
Constructors used to create the objects:
Vehicleint vin, Engine horsePower, int maxPass
passengers ;
RaceProgress ;
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 equalsObject other
return this.VIN VehicleotherVIN;
public void reset
RaceProgress ;
hasCrashedfalse;mainly for motorcycle; reset progress and crash
class Enginemove next to car class for easier referring
int speed;
public Engineint speed
this.speed speed;
public int SpeedModifier
returns speed to maxSpeed
return intMathrandomspeedspeed;
class Car extends Vehicle car is a vehicle
whatever comments were here
public Carint i Engine e int passengers, int maxPassengersWorking constructor
superiemaxPassengers;
this.setPassengerspassengers;
public String toString
return "Car::"super.toString;
This is overwriting the superbase classes method
car satisfies the vehicles Go requirement
public void Go
it calls upon this, so this needs a lot of this
this.setRaceProgressthisgetRaceProgress engine.SpeedModifierthisgetPassengers;
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.
Truckint i Engine e int passengers, int maxPassengers, int towWeight
superi e maxPassengers;
this.setPassengerspassengers;
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started