public class Robot {
private String name;
private int energy;
public Robot(String name) {
this.name = name;
this.energy = 100;
}
public void work(int hours) {
this.energy -= (hours * 15);
if (this.energy < 0) this.energy = 0;
}
public void recharge(int hours) {
this.energy += (hours * 20);
if (this.energy > 100) this.energy = 100;
}
public int getEnergy() { return this.energy; }
public String getName() { return this.name; }
}
Main Method — Step Through
1
Robot bot = new Robot("Atlas"); // Create the object
2
System.out.println(bot.getEnergy()); // Check energy
3
bot.work(3); // Work 3 hours
4
System.out.println(bot.getEnergy()); // Check energy
5
bot.recharge(2); // Recharge 2 hours
6
System.out.println(bot.getEnergy()); // Check energy
7
bot.work(8); // Work 8 hours (heavy!)
8
System.out.println(bot.getEnergy()); // Check energy
Ready to start
Console Output
No object created yet.
Click Step to create the Robot.