Build A Droid - Step 13(Energy Transfer)

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);

  }
}

bro have you got the solution.
and have you completed your course.

in your print statement where you are referencing the droid objects, call for their name instance variable. I.E, instead of just “otherDroid” which will call the toString() method. use “otherDroid.name” to print the name of the object. do this for both objects in your energyTransfer() method.

System.out.println(otherDroid.name + " has transferred " + amount + "% of its battery to " + name);

The course had not taught me the use of this “this” keyword. With that, I was able to write a solution for the energyTransfer method:

public void energyTransfer(Droid otherDroid, int amount) {
this.batteryLevel = this.batteryLevel + amount;
otherDroid.batteryLevel = otherDroid.batteryLevel - amount;
System.out.println(otherDroid.name + " has transferred " + amount + "% to " + name);
System.out.println(name + " is now at " + batteryLevel + “% battery.”);
System.out.println(otherDroid.name + " is currently at " + otherDroid.batteryLevel + “%”);
}

1 Like

yeah, still doesn’t teach that in 2023 …

well done for finding the answer independently though!