← Unit 3 · all exercises
Saved in this browser only

A battery

Write a Battery starting fully charged at 100. use takes off the given percentage but never below 0. charge adds but never above 100. getLevel returns the charge, and isEmpty returns whether it has reached 0.

class Battery { Battery(); void use(int amount); void charge(int amount); int getLevel(); boolean isEmpty(); }

Battery.java
Loading editor…
Results

Examples

  • Battery b = new Battery(); b.use(30); b.getLevel() → 70
  • Battery b = new Battery(); b.use(150); b.getLevel() → 0cannot go below zero

There are also 3 hidden tests you can't see. Solve the problem, not the examples.