Hello,
Basically I got stuck on the last part of this exercise (Step 13) which was to create a method for Energy Transfer between each of my Droids. I have 2 Droids (Codey and Nups) and I transferred 10% of “Nups” to “Codey”. I got the energy transferring part alright from what it seemed like to me but for some reason, it kept printing the message from
//toString METHOD
public String toString(){
return "Hello! I'm the Droid " + name;
}
with it which I didn’t want it to. I tried some ways that I knew of to fix it but it didn’t work. Please help, I’m still a beginner at coding and only started a week ago technically.
My Entire Code:
public class Droid{
String name;
int batteryLevel;
int health;
public Droid(String droidName){
name = droidName;
batteryLevel = 100;
health = 100;
}
//toString METHOD
public String toString(){
return "Hello! I'm the Droid " + name;
}
//performTask METHOD
public void performTask(String task){
System.out.println(name + " has just " + task + ".");
batteryLevel = batteryLevel - 10;
health = health - 5;
this.energyReport();
this.healthReport();
}
//energyReport METHOD
public void energyReport(){
System.out.println(name + " has " + batteryLevel + "% Battery left.");
}
//healthReport METHOD
public void healthReport(){
System.out.println(name + " has " + health + "HP left!");
}
//energyTransfer METHOD
public void energyTransfer(Droid otherDroid, int amount){
this.batteryLevel = this.batteryLevel - amount;
System.out.println(otherDroid + " has transferred " + amount + "% of its battery to " + name);
System.out.println(otherDroid + " is currently at " + this.batteryLevel + "%");
System.out.println(name + " is now at " + (batteryLevel + amount) + "% battery.");
}
// Main METHOD
public static void main(String[] args){
Droid one = new Droid("Codey");
Droid two = new Droid("Nups");
one.performTask("choked");
two.performTask("swallowed");
one.energyTransfer(two, 10);
}
}