-
-
Save codecademydev/f044a722b2aff8e9a48d483bae25343a to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Droid { | |
int batteryLevel; | |
String name; | |
public Droid(String droidName){ | |
name = droidName; | |
batteryLevel = 100; | |
} | |
public String toString() { | |
return "Hello, I'm the droid: " + name; | |
} | |
public void performTask(String task) { | |
System.out.println(name + " is performing task: " + task); | |
batteryLevel -= 10; | |
} | |
public void energyReport() { | |
int currentBattery = batteryLevel; | |
System.out.println("Current battery level is: " + currentBattery); | |
} | |
public void energyTransfer(int percentToTransfer, Droid a) { | |
batteryLevel = batteryLevel - percentToTransfer; | |
a.batteryLevel = percentToTransfer + batteryLevel; | |
System.out.println(a + " has been transferred " + percentToTransfer + "%!"); | |
} | |
public static void main(String[] args) { | |
Droid firstDroid = new Droid("Codey"); | |
Droid secondDroid = new Droid("Gilda"); | |
System.out.println(firstDroid); | |
firstDroid.performTask("dancing"); | |
firstDroid.performTask("singing"); | |
firstDroid.energyReport(); | |
secondDroid.energyTransfer(52, firstDroid); | |
firstDroid.energyReport(); | |
secondDroid.energyReport(); | |
secondDroid.performTask("streaming"); | |
secondDroid.energyReport(); | |
secondDroid.energyTransfer(39, firstDroid); | |
secondDroid.energyReport(); | |
firstDroid.energyReport(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment