this is what we had done public class Racecar { private String driver; private i
ID: 3532473 • Letter: T
Question
this is what we had done
public class Racecar
{
private String driver;
private int carNumber;
private int gasLevel;
private int tireLevel;
private boolean pitStop;
private double currentLap;
/**
* Constructs a new Racecar with the specified driver and car number.
* If the car number is invalid (not in the range 0-99) then it is set
* to 0. The gas level and tire level are set to 99. The car is not in
* a pit stop. The current lap is set to 0.0.
*/
public Racecar(String driver, int carNumber)
{
this.driver = driver;
// if the car number is invalid, set it to 0
if (carNumber < 0 || carNumber > 99) {
this.carNumber = 0;
} else {
this.carNumber = carNumber;
}
this.gasLevel = 99;
this.tireLevel = 99;
this.pitStop = false;
this.currentLap = 0.0;
}
/**
* Returns the driver name.
*/
public String getDriver()
{
return this.driver;
}
/**
* Returns the car number.
*/
public int getCarNumber()
{
return this.carNumber;
}
/**
* Returns the current gas level.
*/
public int getGasLevel()
{
return this.gasLevel;
}
/**
* Returns the current tire level.
*/
public int getTireLevel()
{
return this.tireLevel;
}
/**
* Returns whether or not this car is in a pit stop.
*/
public boolean inPitStop()
{
return this.pitStop;
}
/**
* Returns the current lap.
*/
public double getCurrentLap()
{
return this.currentLap;
}
/**
* Decreases the gas level by 1. Ensures that the gas is never negative.
*/
public void decreaseGas()
{
if (this.gasLevel > 0) {
this.gasLevel--;
}
}
/**
* Decreases the tire level by 1. Ensures that the tire level is never negative.
*/
public void decreaseTire()
{
if (this.tireLevel > 0) {
this.tireLevel--;
}
}
/**
* Resets the tire and gas levels to 99.
*/
public void pitStop()
{
this.tireLevel = 99;
this.gasLevel = 99;
this.pitStop = false;
}
}
public class TestRacecar
{
public static void main(String[] args)
{
Racecar car1 = new Racecar("Todd", 1);
// print initial values of a new Racecar
System.out.println("The driver name of car1 is: " + car1.getDriver());
System.out.println("The car number of car1 is: " + car1.getCarNumber());
System.out.println("The current gas level of car1 is: " + car1.getGasLevel());
System.out.println("The current tire level of car1 is: " + car1.getTireLevel());
System.out.println("car1 is currently in a pit stop: " + car1.inPitStop());
System.out.println("The current lap of car1 is: " + car1.getCurrentLap());
System.out.println();
// use gas
car1.decreaseGas();
car1.decreaseGas();
car1.decreaseGas();
System.out.println("Gas level after decreasing three times is: " + car1.getGasLevel());
// use tires
car1.decreaseTire();
car1.decreaseTire();
car1.decreaseTire();
car1.decreaseTire();
System.out.println("Tire level after decreasing four times is: " + car1.getTireLevel());
// test pit stop
car1.pitStop();
System.out.println("Gas level after a pit stop is: " + car1.getGasLevel());
System.out.println("Tire level after a pit stop is: " + car1.getTireLevel());
System.out.println();
// test invalid car numbers
Racecar carNeg25 = new Racecar("Todd", -25);
Racecar car1000 = new Racecar("Todd", 1000);
System.out.println("Car number for carNeg25 is: " + carNeg25.getCarNumber());
System.out.println("Car number for car1000 is: " + car1000.getCarNumber());
System.out.println();
// test negative gas and tire level
for (int i = 0; i < 1000; i++) {
car1.decreaseGas();
car1.decreaseTire();
}
System.out.println("Gas level after decreasing 1000 times is: " + car1.getGasLevel());
System.out.println("Tire level after decreasing 1000 times is: " + car1.getTireLevel());
}
}
and this is a homework
First we must add a few things to our race care:
To simulate the passage of time create a tick() method that our simulator can call.
Here is a peusdocode for what it should do:
public void tick() {
if we dont need a pit stop:
increase the lapCount by .15 + a random amount of hundredths (,01,.02,.03) between 0 and .1 decreaseGas();
there is a ten percent chance we will decrease the gas one more
decreaseTireCondition();
there is a ten percent chance we will decrease the tire condition one more
if gasLevel < 10 or tireCondition < 10 indicate we need a pitstop
If we need a pitstop do one.
}
Create a method that returns a string that indicates the postion on the track of the car:
Our track is Daytona:
You will need to convert the feet to a percent and compare it to the decimal portion of the lap count.
Map of Daytona track.
override the toString() method to return the name of the driver the postion on the track and the lap number
For example:
Bob picks up speed down the back stretch on lap 197
Fred picks up speed down the back stretch on lap 198
Alex flashes down the front stretch toward the finish line on lap 199
Danica flashes down the front stretch toward the finish line on lap 198
Note the lap should be displayed as an integer. Also print out that the driver headed to the pit stop if the pit stop indicator is true.
The Simulation
The simulation should loop through the cars and call the tick method.
Eventually one of the cars should go past the number of laps for a complete (200 or more)
Psuedocode:
Keep looping while the race is not over
call method to update each of the cars
if the race is not over print all the cars
if the race is over stop looping
Print out the winner
Print out the second place driver
The end of the simulation might look like this:
Bob picks up speed down the back stretch on lap 197
Fred picks up speed down the back stretch on lap 198
Alex flashes down the front stretch toward the finish line on lap 199
Danica flashes down the front stretch toward the finish line on lap 198
___________________________________________
Bob dives into turn three on lap 197
Fred explodes into turn four on lap 198
Alex flashes down the front stretch toward the finish line on lap 199
Danica down the front stretch toward turn one on lap 19
Position Ends at (in feet) down the front toward turn one 1900 Into turn one 2700 Out of turn one 3500 Into turn two 4300 Out of turn two 5100 BackStretch 8100 Into turn three 8900 Out of turn three 9700 Into turn four 10500 Out of turn four 11300 Down Front Stretch toward the finish line 13200Explanation / Answer
public void tick() {
double random = new Random().nextInt(10)/100;
if (!pitStop){
currentLap += (0.15 + random) ;
decreaseTire();
}
if( gasLevel < 10 || tireLevel < 10){
pitStop = true;
}
}
@Override
public String toString() {
return "Racecar [driver=" + driver + ", carNumber=" + carNumber
+ ", gasLevel=" + gasLevel + ", tireLevel=" + tireLevel
+ ", pitStop=" + pitStop + ", currentLap=" + currentLap + "]";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.