Note: This exercise is a continuation of the need-for-speed exercise.
In this exercise you'll be organizing races between various types of remote controlled cars. Each car has its own speed and battery drain characteristics.
Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers the car's speed in meters and decreases the remaining battery percentage by its battery drain.
If a car's battery is below its battery drain percentage, you can't drive the car anymore.
The remote controlled car has a fancy LED display that shows two bits of information:
"Driven <METERS> meters"."Battery at <PERCENTAGE>%".Each race track has its own distance. Cars are tested by checking if they can finish the track without running out of battery.
Implement the Drive method on the Car that updates the number of meters driven based on the car's speed, and reduces the battery according to the battery drainage:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
car.Drive()
// car is now Car{speed: 5, batteryDrain: 2, battery: 98, distance: 5}Note: You should not try to drive the car if doing so will cause the car's battery to be below 0.
Implement a DisplayDistance method on Car to return the distance as displayed on the LED display as a string:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
fmt.Println(car.DisplayDistance())
// Output: "Driven 0 meters"Implement the DisplayBattery method on Car to return the battery percentage as displayed on the LED display as a string:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
fmt.Println(car.DisplayBattery())
// Output: "Battery at 100%"To finish a race, a car has to be able to drive the race's distance.
This means not draining its battery before having crossed the finish line.
Implement the CanFinish method that takes a trackDistance int as its parameter and returns true if the car can finish the race; otherwise, return false:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
trackDistance := 100
car.CanFinish(trackDistance)
// => trueNote: This exercise is a continuation of the need-for-speed exercise.
In this exercise you'll be organizing races between various types of remote controlled cars. Each car has its own speed and battery drain characteristics.
Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers the car's speed in meters and decreases the remaining battery percentage by its battery drain.
If a car's battery is below its battery drain percentage, you can't drive the car anymore.
The remote controlled car has a fancy LED display that shows two bits of information:
"Driven <METERS> meters"."Battery at <PERCENTAGE>%".Each race track has its own distance. Cars are tested by checking if they can finish the track without running out of battery.
Implement the Drive method on the Car that updates the number of meters driven based on the car's speed, and reduces the battery according to the battery drainage:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
car.Drive()
// car is now Car{speed: 5, batteryDrain: 2, battery: 98, distance: 5}Note: You should not try to drive the car if doing so will cause the car's battery to be below 0.
Implement a DisplayDistance method on Car to return the distance as displayed on the LED display as a string:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
fmt.Println(car.DisplayDistance())
// Output: "Driven 0 meters"Implement the DisplayBattery method on Car to return the battery percentage as displayed on the LED display as a string:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
fmt.Println(car.DisplayBattery())
// Output: "Battery at 100%"To finish a race, a car has to be able to drive the race's distance.
This means not draining its battery before having crossed the finish line.
Implement the CanFinish method that takes a trackDistance int as its parameter and returns true if the car can finish the race; otherwise, return false:
speed := 5
batteryDrain := 2
car := NewCar(speed, batteryDrain)
trackDistance := 100
car.CanFinish(trackDistance)
// => true