Loading...
Loading...
A smart home app has Device (on or off), Light extends Device (also counts how many times it was switched on) and final class DimmableLight extends Light (also has a brightness from 1 to 100, starting at 100). Each level overrides turnOn() or status() and calls the super version.
dim b sets the brightness and turns the light on; dim 0 only turns it off and keeps the old brightness. run h adds watts × brightness × h / 100 Wh (rounded down) when the light is on.
on, off, dim b, run h or status.status: name ON b% or name OFF. At the end: SWITCHED ON k TIMES and ENERGY e Wh.Example
Input
Lamp 10 6 on run 2 dim 50 run 3 status off
Output
Lamp ON 50% SWITCHED ON 1 TIMES ENERGY 35 Wh
10 × 100 × 2 / 100 = 20 Wh, then 10 × 50 × 3 / 100 = 15 Wh. dim on a light that is already on is not a new switch-on.
Try the problem first. Open one hint at a time.
Device has a protected boolean on; Light.turnOn() counts only when on is still false, then calls super.turnOn().
DimmableLight.status() returns super.status() plus b% when the light is on.
Handle dim 0 by calling turnOff() and leaving brightness alone.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor