Loading...
Loading...
A driving game keeps a car's speed between 0 and its top speed. Speeding up past the top speed stops at the top speed, and braking harder than the current speed stops the car at 0.
Write a class Car with fields model, speed and maxSpeed and methods void accelerate(int by) and void brake(int by) that keep these rules. Print the speed after every command.
accelerate x or brake x.model speed km/h.Example
Input
Nexon 120 4 accelerate 50 accelerate 100 brake 30 brake 200
Output
Nexon 50 km/h Nexon 120 km/h Nexon 90 km/h Nexon 0 km/h
The second command stops at 120, and the last brake stops the car at 0.
Try the problem first. Open one hint at a time.
A new object's int field starts at 0, so the car starts standing still.
Change the speed first, then fix it if it went past a limit.
All the rules live in the two methods; main only reads commands and calls them.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor