im on a challenge which is where i create a droid, then i create a second droid, both droids have names and int battery levels
but the last step is asking me to create a method which allows the droids to transfer battery level between them, im really unsure how to do this if anyone know please give some tips/explanation/template so i can try hone this method, thank you slight_smile:
here is my code:
public class Droid{
int batteryLevel;
String yabadabadoo;
public Droid(String droidName){
yabadabadoo = droidName;
batteryLevel = 100;
}
public String toString(){
return "Hello, i’m the droid: " + yabadabadoo;
}
public void performTask(String task){
batteryLevel = batteryLevel - 10;
System.out.println(yabadabadoo + " is performing task: " + task);
}
public int energyReport(){
return batteryLevel;
}
public static void main(String args){
Droid codey = new Droid(“codey”);
System.out.println(codey);
codey.performTask(“dancing”);
System.out.println(codey.energyReport());
Droid jack = new Droid(“Jack”);
System.out.println(jack);
System.out.println(jack.energyReport());
}
}
1 Like
Hello jackburton0958773581(whew)
Remember to use “</>” when you post your code!
If you know which droid you are transferring to then you can just tell it to transfer from one to the other.
Example: I have droid1 and droid2 as my droids.
public void energyTransfer(int percent,){
batteryLevel = batteryLevel - percent;
droid2.batteryLevel = droid2.batteryLevel + percent;
Otherwise, you can give each droid an integer ID. When you create a new one, check using a for-loop to see if a droid already has the ID. Then, you can get the ID of the droid you are transferring to by creating a new method. Then after you have the ID, you can use that to find which droid. See my example above to do the actual transferring.
I won’t say anymore because this is all about thinking outside the box! 

1 Like
Wait I am such a dumbo, If you have a list of droid names, then instead of the int ID
you can use Droid name
or something like that. Remember, an object can be used similarly to a variable when we are talking about methods. The difference is that you can “call” information about the object as needed.
Hope this helps!!!
1 Like
public void energyTransfer(Droid droid1, Droid droid2, int energy{
//Your code here
}
I found this way, the parameters droid1 and droid2, are objects of Droid class (instances).
1 Like
Yes, I think that will work. BUT! It might be smarter to use droid1.energyTransfer()
than having both of the droids there. This, of course, is considering that I understand what you mean. So if possible, explain what you are trying to accomplish with the code and I can help you with that!
1 Like