Java Build a Droid Project

Is there a way to exchange the battery levels between both droids? For example, if Droid A has 70% and Droid B has 80%, Droid A would then have 80% and Droid B would have 70%? I’ve seen solutions where they used static variables and/or passing parameters that use a reference data type. However I know that’s more advanced than where the chapter is at. Thanks in advance if you can help me out.

you could have a Droid object as a parameter for the method.
I used an int for the batteryLevel in mine, and created a variable temp to help swap the values.

  public void EnergyTransfer(Droid otherDroid) {
    int temp = otherDroid.batteryLevel;
    otherDroid.batteryLevel = this.batteryLevel;
    this.batteryLevel = temp;
  }

Isn’t this more advanced than where the course is at for teaching tho? I don’t remember this in the lessons so far to have an object as a parameter.

yes, its more advanced than the stuff covered so far at that point;
I think that’s why its an optional extension to the project.

Gotcha. Was going thru the lessons 1 by 1. Thanks for the help :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.