# 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?

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?

3 Likes

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

//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!

1 Like

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.
``````
8 Likes

Thanks to design0971970542 and some other posts, who showed me how to use the other objects as parameters, I came up with something slightly different.

The transfer method:

`````` public void energyTransfer(int newBatteryLevel, Droid energyTaker){
int oldBatteryLevel = this.batteryLevel;

this.batteryLevel = newBatteryLevel;
energyTaker.batteryLevel = oldBatteryLevel;
System.out.println(energyTaker.name  + " and " + this.name  + " exchanged energy.");
``````

the main method:

``````public static void main(String args[]){
Droid Codey = new Droid("Codey");
Droid Zack = new Droid("Zack");

//insert any tasks in here
Codey.energyReport();
Zack.energyReport();
Codey.energyTransfer(Zack.batteryLevel, Zack);
Codey.energyReport();
Zack.energyReport();
``````

Should result in:

``````//after tasks
Codey is now at 40 percent
Zack is now at 80 percent
Zack and Codey exchanged energy.
Codey is now at 80 percent
Zack is now at 40 percent
``````
1 Like

Thank you!!

Helooo, Thank you! I am 99.99% done thanks to you, just cant figure out why it says â€śhello, iâ€™m the droid:Codeyâ€ť when it should just say ONLY Codey for the place-
â€ś% energy toâ€ť + energyTaker.
energyTaker = â€śHello, Iâ€™m the droid:Codeyâ€ť for some reason.

This is what it says-

Drama transferred 10 % energy to Hello, Iâ€™m the droid: Codey and his battery level is: 90

Thank you, this helped me so much, also c3po

I used this - using the good ole swap variable method:

``````    public static void energyTransfer(Droid a, Droid b){
int temp = a.batteryLevel;
a.batteryLevel = b.batteryLevel;
b.batteryLevel = temp;

System.out.println(a.name + "'s battery level is exchanged with " + b.name);
System.out.println(a.name + "'s current battery level now is: " + a.batteryLevel + ".");
System.out.println(b.name + "'s current battery level now is: " + b.batteryLevel + ".");

}
``````
1 Like

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;
}
System.out.println(name + " is performing task: " + task);
batteryLevel -= 10;
}
public void energyReport(){
System.out.println("The energy Level of: " + name + " is: " + batteryLevel);
}
public void energyTransfer(int Amount, Droid droName){
batteryLevel -= Amount;
droName.batteryLevel += Amount;

}

``````public static void main(String[] args){
Droid codey = new Droid("Codey");
Droid happy = new Droid("Happy");
Droid sailor = new Droid("Sailor");
System.out.println(codey);
System.out.println(happy);
System.out.println(sailor);