Loading...
Loading...
Vehicle keeps the odometer. Car extends Vehicle also counts trips and says when a service is due (every S km since the last service). final class ElectricCar extends Car also has a battery: each km uses w Wh, and a trip longer than the charge allows is refused with BatteryLowException extends RuntimeException, which stores how many km are possible.
ElectricCar.drive(km) checks the battery, uses the energy and calls super.drive(km); Car.drive counts the trip and calls super.drive(km) again. The car starts fully charged.
drive km, charge k (adds k kWh, up to full), service or status.DROVE km ODO o BATTERY p%, plus SERVICE DUE when due, or LOW BATTERY: only m km possible. charge: BATTERY p%. service: SERVICED AT o. status: REG ODO o TRIPS t BATTERY p%. p is the charge percent, rounded down.Example
Input
KA01EV7 40 150 1000 5 drive 120 drive 200 charge 30 drive 200 status
Output
DROVE 120 ODO 120 BATTERY 55% LOW BATTERY: only 146 km possible BATTERY 100% DROVE 200 ODO 320 BATTERY 25% KA01EV7 ODO 320 TRIPS 2 BATTERY 25%
After 120 km only 22 kWh is left, enough for 146 km, so the 200 km trip waits for a charge.
Try the problem first. Open one hint at a time.
Store the charge in Wh as a long; percent is charge * 100 / capacity.
In ElectricCar.drive, throw before changing anything: if (km * whPerKm > charge) throw new BatteryLowException(charge / whPerKm);.
Car.needsService() is getOdometer() - lastService >= serviceEvery; service() sets lastService to the odometer.
The exception must be unchecked: an overriding drive may not throw a checked exception that Vehicle.drive does not declare.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor