Build a Droid - Need help with energyTransfer method

I am pretty stumped on how to transfer one objects batteryLevel to the others. Can somebody please explain?

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

Here’s a thread about it in stack overflow

I’m in the same boat. I don’t understand what to put inside the energyTransfer() method.
I looked at the link from ruby5230943909
but the answers seemed a bit advanced. I don’t believe we’ve learnt about 'this. ’ yet so I think there must be a different way.
Anyone have any tips?

This is what I did

This is the method

   public void energyTransfer(int power, Droid transferTo){  
     batteryLevel -= power;
    transferTo.batteryLevel += power;
}

Then I called it by typing

c3po.energyTransfer(20, codey);

Does this help?

Phew! At least i’m not the only one stuck in this point lol

What i understand from the question is that you must exchange battery levels between droids. For example: droid1 has 70% battery, droid2 has 20% battery. Magic method appears and then droid1 has 20% battery and droid2 has 70% battery.

Anybody came up with a solution to this one?

What suggested toblerpwn is working (if you want to transfer some amount of energy), however I have also made this solution based on clues from StackOverlfow. This will transfer full energy from one robot to another. It isn’t perfect, cause its my first week with Java :slight_smile:

//Method:

public void energyTransfer(Droid aux) {
this.batteryLevel = this.batteryLevel +aux.batteryLevel;
aux.batteryLevel = 0;
}

//called in main
robot1.energyTransfer(codey);

If you are still trying to figure this out, this worked for me:

public void energyTransfer(Droid aux) {
int aux2 = this.batteryLevel;
this.batteryLevel = aux.batteryLevel;
aux.batteryLevel = aux2;
}

And call it in your main:

System.out.println(alexa);
System.out.println(codey);    
alexa.energyTransfer(codey);
System.out.println(alexa);
System.out.println(codey);

I printed the values twice to see the before and after.

Hope this helps! :slight_smile:

Hello, could you explain this part of your code

 Droid transferTo 

why is this seperate 2 parametars, what do they refer to?

Thank you

My solution:

  public void energyTransfer(Droid energyTaker, int energyAmount) {
    batteryLevel = batteryLevel - energyAmount;
    energyTaker.batteryLevel = energyTaker.batteryLevel + energyAmount;
    System.out.println(name + " transferred " + energyAmount + "% energy to " + energyTaker.name + " and his battery level is: " + batteryLevel + ". " + energyTaker.name + "'s battery level is now: " + energyTaker.batteryLevel + ".");
  }

in main method:

    Droid codey = new Droid("Codey");
    Droid samantha = new Droid("Samantha");
    codey.energyTransfer(samantha, 40);

Result:

Codey transferred 40% energy to Samantha and his battery level is: 60. Samantha's battery level is now: 140.