# Exercise: Droid

Exercise: Droid

So I did everything good till the energy transfer, somehow it’s not working as intended,
1)at the first transfer the energy was good, it went from 80 to 100 with a transfer of 52, and the instance that I took energy from, had a value of 48, all good,
2) then I did a task of the instance I took energy from and energy when from 48 to 38. //after every task it takes 10 energy.
3)I tried to transfer 39 energy to see if it goes to 0 or -1, since it had 38, and it worked and the energy was -1%, all good, then I decided to check currenty battery level of both instances that one was good, -1, but somehow the instance I was giving the energy has a value of 38, and even if I change the % to transfer it still gives the value of 38?.

Hi,

If you provide your code we can help you! Just remember to format with the </> before pasting.

@java0166897890 sorry i thought you linked to the cc exercise haha

1 Like

Oh, I did write the link, if you click ‘‘Exercise: Droid’’ it will redirect you to the github.
I will post it here anyways

``````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;
}

batteryLevel -= 10;
}

public void energyReport() {
int currentBattery = batteryLevel;
System.out.println("Current battery level is: " + currentBattery);
}

public void energyTransfer(int percentToTransfer, Droid a) {
batteryLevel = batteryLevel - percentToTransfer;
a.batteryLevel = percentToTransfer + batteryLevel;
System.out.println(a + " has been transferred " + percentToTransfer + "%!");

}

public static void main(String[] args) {
Droid firstDroid = new Droid("Codey");
Droid secondDroid = new Droid("Gilda");
System.out.println(firstDroid);
firstDroid.energyReport();
secondDroid.energyTransfer(52, firstDroid);
firstDroid.energyReport();
secondDroid.energyReport();
secondDroid.energyReport();
secondDroid.energyTransfer(39, firstDroid);
secondDroid.energyReport();
firstDroid.energyReport();

}
}
``````

It looks like your energyTransefer could use some revision.
For example, take away all the println statements and run this

``````        Droid firstDroid = new Droid("Codey");
Droid secondDroid = new Droid("Gilda");

secondDroid.energyTransfer(52, firstDroid);

secondDroid.energyTransfer(39, firstDroid);
secondDroid.energyReport();
firstDroid.energyReport();
``````

Is this doing the proper math as you want?

The second line in particular runs into issues.

``````batteryLevel = batteryLevel - percentToTransfer;
a.batteryLevel = percentToTransfer + batteryLevel;
``````
1 Like

it does not, weird, first it did. They ask in the exercise for us to work with two instances in one method, that is batteryTransfer, they don’t really specify if it sums or subtract or anything.
edit: but I would like it to do that, if I transfer from a (52%) to b, then when I check a battery it has 48% and if will add the 52% to b battery or stay at 100% because it’s max it can have.

If you reverse the order of the lines it should work better. The reason is that the second line uses a batteryLevel that’s already been changed, so it’s not the original amount you wanted to track.

1 Like

I already solve the problem with this one:

``````batteryLevel -= percentToTransfer;
a.batteryLevel += percentToTransfer;
``````

The problem was I had to sum percent to a.batteryLevel instead of only batteryLevel

1 Like

Ah yea, concise

1 Like

@toastedpitabread: I was also stuck on the energy transfer. Reading over this thread definitely helped. I do have question.

In the code above, the method parameters for the energyTransfer method are (int percentToTransfer, Droid a) — I assume the second parameter is requiring a specific instance of the Droid class to be passed into the method. Is this correct? In doing so, it appears that the batteryLevel field of that instance can be manipulated via <a.batteryLevel >.
Am I understanding this correctly? Can you provide some guidance on what is actually happening in the above scenario? I do not recall seeing this in the lesson, are there resources for this?

Hi,

I’ll add a disclaimer that I don’t have access to the lesson so I don’t know what the instructions are. I’m just looking at it from a conceptual view from how it was laid out (not trying to improve the structure or anything).

The energyTransfer() method will belong to each instantiation of class Droid. It takes a value from the host droids batteryLevel (by manipulating `int percentToTransfer`) and passes it to a target droids battery level (the target droid specified by `Droid a`). You are correct that it has to be specific.