Loading...
Loading...
A phone has a fixed total storage in GB. Installing an app needs enough free space, and removing more than is used is impossible.
Write a class Smartphone with private totalStorage and storageUsed, boolean install(int gb), boolean remove(int gb) and two getters that are worked out from the fields: getAvailable() and getUsagePercent(). Sizes of 0 or less are invalid.
install x or remove x.FREE f GB, p% USED, with p printed with one digit after the decimal point. Otherwise INVALID SIZE, NOT ENOUGH SPACE, FREE f GB or CANNOT REMOVE MORE THAN USED.Example
Input
64 3 install 40 install 30 remove 10
Output
FREE 24 GB, 62.5% USED NOT ENOUGH SPACE, FREE 24 GB FREE 34 GB, 46.9% USED
Only 24 GB is free, so the 30 GB app does not fit.
Try the problem first. Open one hint at a time.
Do not store available as a field: work it out as totalStorage - storageUsed, so it can never be out of date.
Use 100.0 in the percentage so the division keeps its decimals.
Print %% inside printf to show a % sign.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor