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?


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:


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

//called in main

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:


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

Hope this helps! :slight_smile:

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 " + + " and his battery level is: " + batteryLevel + ". " + + "'s battery level is now: " + energyTaker.batteryLevel + ".");

in main method:

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


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

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(  + " and " +  + " 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.energyTransfer(Zack.batteryLevel, Zack);

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!! :smiley:

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 :smile:

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( + "'s battery level is exchanged with " +;
        System.out.println( + "'s current battery level now is: " + a.batteryLevel + ".");
        System.out.println( + "'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;
public void performTask(String task){
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");
    codey.performTask("Turn on TV");
    codey.performTask("Turn off TV");
    codey.energyTransfer(20, happy);