Loading...
Loading...
A laptop's battery level is a percentage that can never go above 100 or below 0. Charging past 100 stops at 100, and using more than is left stops at 0.
Write a class Laptop with a private battery, a constructor that refuses a start value outside 0 to 100, int charge(int p) and int use(int p). Each method returns how much was really added or used. Amounts of 0 or less are invalid.
charge x or use x.CHARGED a, BATTERY b% or USED a, BATTERY b%, where a is the real change, or INVALID AMOUNT x.Example
Input
50 3 charge 70 use 30 use 90
Output
CHARGED 50, BATTERY 100% USED 30, BATTERY 70% USED 70, BATTERY 0%
Charging stops at 100 (only 50 added), and the last use stops at 0.
Try the problem first. Open one hint at a time.
Math.min(100, battery + p) keeps the value at or below 100; Math.max(0, battery - p) keeps it at or above 0.
Save the old value first; the real change is the difference between the new and the old value.
The methods also ignore amounts of 0 or less, so the object is safe even if a caller forgets the check.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor