Exercise "Build a Droid" step 13 - method energyTransfer() - need help!

Hey guys,

I have a question regarding part of this exercise:

https://www.codecademy.com/courses/learn-java/projects/build-a-droid

I got everything working up to the last part of step 13. I’ll post my whole code for this exercise below but my problem is specifically with this section:

public void energyTransfer(String droidName,int amountToTransfer){
    this.batteryLevel = this.batteryLevel - amountToTransfer;
    droidName.batteryLevel = droidName.batteryLevel + amountToTransfer;
    System.out.println("Transferred "+amountToTransfer+"% energy from "+this.droidName+" to "+droidName);
    System.out.println(this.droidName+"'s energy level is now at "+this.batteryLevel+"%.");
    System.out.println(droidName+"'s energy level is now at "+droidName.batteryLevel+"%.");
  }

Not sure if what I’m trying to do is going completely into the wrong direction so feel free to throw this whole section overboard. What I tried to do is this: I want to reference the instance field droidName as a parameter for this method so that

droidName.batteryLevel 

becomes

my2ndDroid.batteryLevel 

when you call

myDroid.energyTransfer(my2ndDroid, 5);

I would be very grateful for any helpful tips or comments. I only started with Java two days ago and I realize that I may have “painted myself into a corner” on this one :wink:

Complete code for this exercise:

public class Droid{
  
  public String droidName;
  public int batteryLevel;
  
  // Droid constructor
  public Droid(String name){
    batteryLevel = 100;
    droidName = name;
    System.out.println("- - - - -");
    System.out.println("Creating new Droid...");
    System.out.println("- - - - -");
    System.out.println("Hi! I'm a Droid and my name is " + droidName + "! My current battery levels are at "+batteryLevel+"%!");
  }
  
  public String toString(){
  	return "Hi! I'm a Droid and my name is " + droidName + "! My current battery levels are at "+batteryLevel+"%!";
  }
  
  public void performTask(String task){
    System.out.println(droidName + " is performing the task: " + task);
    batteryLevel = batteryLevel - 10;
    this.energyReport();
  }
	
  public void energyReport(){
    System.out.println(droidName+"'s current battery level is at "+ batteryLevel+"%!");
  }
  
  public void energyTransfer(String droidName,int amountToTransfer){
    this.batteryLevel = this.batteryLevel - amountToTransfer;
    System.out.println("Transferred "+amountToTransfer+"% energy from "+this.droidName+" to "+droidName);
    System.out.println(this.droidName+"'s energy level is now at "+this.batteryLevel+"%.");
    System.out.println(droidName+"'s energy level is now at "+"%.");
  }
  
  public static void main(String[] args){
    
    Droid myDroid = new Droid("Codey");
    
    myDroid.performTask("cleaning");
    
    myDroid.performTask("cooking");
    
    myDroid.performTask("washing the dishes");
  
    Droid my2ndDroid = new Droid("Andrew");
    
    my2ndDroid.performTask("dusting");
    
    myDroid.performTask("bitching");
    
    myDroid.energyTransfer(my2ndDroid.droidName, 5);
    
  }
  
}
1 Like

when you define the method have it look like

public void transferEnergy(Droid otherDroid, int energyToTransfer){}

That’s because you want to pass in a variable with a data type of Droid (which we created using a class) not a data type of String. This allows you to access the states of that parameter like normal. e.g. otherDroid.batteryLevel or otherDroid.name

3 Likes

Hi.

Here’s my working version of the code including the energy transfer challenge.
Thanks to msl0033 for the tip above.

I’ve included a custom check - if requested amount of energy is larger than 50% of donor’s current battery level, the request is rejected.

public class Droid {

  String name;
  int batteryLevel;

  public Droid(String droidName) {
    name = droidName;
    batteryLevel = 100;
  }

  public String toString() {
    return "Hello, I'm the droid: " + name + "\n";
  }

  public void performTask(String task) {
    System.out.println("Droid " + name + " is performing task: " + task + "\n");
    batteryLevel -= 10;
  }

  public void stateBatteryLevel() {
    System.out.println("Droid " + name + "'s battery level is: " + batteryLevel + "\n");
  }

  public void shareEnergy(Droid otherDroid, int energyAmount) {
    //Check if battery level. Droid can't transfer more than 50% of its current battery level.
    if (energyAmount <= batteryLevel / 2) {
      batteryLevel -= energyAmount;
      otherDroid.batteryLevel += energyAmount;
      System.out.println("Energy transfer complete\n");
      stateBatteryLevel();
      otherDroid.stateBatteryLevel();
    } else {
      System.out.println(name + "'s battery level is too low! Try transferring smaller amount of energy\n");
    }
  }

  public static void main(String[] args) {

    Droid codey = new Droid("Codey");
    System.out.println(codey);
    codey.performTask("skating");
    codey.stateBatteryLevel();

    Droid sigma = new Droid("Sigma");
    System.out.println(sigma);
    sigma.performTask("dancing");
    sigma.stateBatteryLevel();

    sigma.shareEnergy(codey, 50); // This request will be rejected
    sigma.shareEnergy(codey, 45); // This request will be accepted
    codey.shareEnergy(sigma, 70); // This request will be rejected
    codey.shareEnergy(sigma, 45); // This request will be accepted
  }
}