Building a Droid free project on Learning Java

Build a Droid, learning java

Hello,
I need your help. I am new in programming, I am stucked on this project building a Droid! Can anyone post the hole Project. There are things I can not understand and need help. I need to see the hole code to figure it out. The hints doesnt help. Thank you!

> public class Droid{
>   
>   int batteryLevel = 100 - 10;
>   String name;
>   
>   public String toString(){
>     return "Hello I'm the droid: " + name;
>   }
>   
>   public void performTask(String task){
>     System.out.println(name + " is performing: " + task);
>   }
>   
>   public Droid(String droidName){
>     name = droidName;
>     
>   }
>   public static void main(String[] args){
>     
>     Droid Codey = new Droid("Codey");
>     System.out.println(Codey);
>     System.out.println(Codey.performTask);
>   }
>   
> }
1 Like

Finally figured something out, but still don’t understand the point 5, 11 and 13 of this project about the batteryLevel.

my updated Code:

public class Droid{

     int batteryLevel;
     String name;

   public String toString(){
     return "Hello, I'm the droid: " + name;
  }

   public void performTask(String task){
    System.out.println(name + " is performing task: " + task);
   }

   public Droid(String droidName){
      name = droidName;
   }

   public static void main(String[] args){

    Droid Codey = new Droid("Codey");
     Droid Joe = new Droid("Joe");
    System.out.println(Codey);
     System.out.println(Joe);
    Codey.performTask("dancing");
     Joe.performTask("levitating");
     }
 }
  1. Inside the constructor, assign the parameter value of droidName to the appropriate instance field.

Set the value of batteryLevel to 100 . Every new instance of Droid will have a batteryLevel of 100 .

  1. Performing tasks is hard work. After the print statement, set batteryLevel to be 10 less than it was before.

We’ll need to reassign the instance field to be the current value minus 10.

  1. Create new instances and play around with methods. Here are some ideas to get you started:
  • Create a energyReport() method that prints the instance’s batteryLevel .
  • Create another instance.
  • Create a method energyTransfer() that exchanges batteryLevel between instances.

Thank you in advance!

-5. You need to set the batteryLevel to 100 inside the constructor.

-11. Decrease the value of batteryLevel by 10 in this performTask method.

-13. energyReport method will just print the batteryLevel

1 Like
public Droid(String droidName){
      name = droidName;
      batteryLevel = 100;
   }
public void performTask(String task){
    System.out.println(name + " is performing task: " + task);
    batteryLevel = batteryLevel - 10;
   System.out.println(batteryLevel);
   }
public void energyReport(int battery){
     System.out.println(batteryLevel);
   }

is it right?
Still don’t understand 13, energyReport and energyTransfer

Thank you!

 Droid(String droidName) {
    name = droidName;
    batteryLevel = 100;
  }

Yes, the constructor is correct this way, as every object that’s created from this gets a battery level of 100 now.

Your method energyReport doesn’t need a parameter ‘(int battery)’. You can leave it empty as you’re not going to pass a Battery level-value when calling the method. The purpose of the method is only to give you the current status of the battery level. So, you can leave it like this:

public void energyReport(){
     System.out.println(batteryLevel);
   }

And you would call the method in the main-method this way:

Codey.energyReport();

The energyTransfer-method does need a parameter, as you’re wanting the method to pass the Battery level-value of ‘Codey’ to ‘Joe’ or vice versa. The way you could write this, is:

public void energyTransfer(int newBatteryLevel) {
    this.batteryLevel = newBatteryLevel;
  }

I called the parameter newBatteryLevel which is of type int. You would call the method as followed:

Codey.energyTransfer(Joe.batteryLevel);

As a parameter, I passed the value of Joe’s current battery level. The method then changes Codey’s battery level to that of Joe’s.

The same thing would happen vice versa if you called: Joe.energyTransfer(Codey.batteryLevel);

8 Likes

Thank you Sir for your help and time!

Is there some other way of doing energyTransfer?
There are few reasons I want to know if there is some other way around it

  1. command **this.**batteryLevel , is unknown to the newbies
  2. your solution is acctually rewriting “codeys” and “Joes” battery.Level for each others(like codey has 30% and Joe 80% and once you run the code Joe.energyTransfer(Codey.batteryLevel); ,both of them have 80%

Thanks in advance!

10 Likes

HELP!

Don’t want to go forward unless I understand this.
I have the same problem. what is the last step of step 13 asking for?

What is being asked to do here with “Create a method energyTransfer() that exchanges batteryLevel between instances.” ?

I am totally lost.

1 Like

wouldn’t it be better if you put batteryLevel under task as a println aswell here’s an example

public class Droid{

String name;

int batteryLevel;

public Droid(String droidName){

name= droidName;

batteryLevel = 100;

}

public String toString(){

return "hello im the droid " + name;

}

public void performTask(String task){

System.out.println(name +" is performing the task: " + task);

batteryLevel= batteryLevel-10;

System.out.println("the current battery level is "+ batteryLevel);

}

public static void main(String args){

Droid Codey = new Droid("Codey");

System.out.println(Codey);

Codey.performTask("dancing");

Codey.performTask("cooking");

Codey.performTask("cleaning");

Codey.performTask("brushing");

}

}
Output
hello im the droid Codey
Codey is performing the task: dancing
the current battery level is 90
Codey is performing the task: cooking
the current battery level is 80
Codey is performing the task: cleaning
the current battery level is 70
Codey is performing the task: brushing
the current battery level is 60

@mateo1235
@design6056035085

Hi both and all, I was really stuck on this too and came up with:

  public void energyTransfer(Droid droid2){    
    int tempBatt = droid2.batteryLevel; //created a temp copy of droid2's battery level to use when it gets over written in the code below
    droid2.batteryLevel = batteryLevel; //change droid2's battery level to the level of the initial Droid called
    batteryLevel = tempBatt; //change the battery level of the inital Droid called to droid2's old battery level
    System.out.println(name + " and " + droid2.name + " have transferred batteries");
  }

code in main:

    codey.energyTransfer(fred);

Although we haven’t been told we can pull a whole object (in this case a Droid I called fred) instead of just a string,int or boolean. It seemed to be the only way to interact with both of them in the same method.

I’d also appreciate any thoughts on the above because even with a lot of googling this doesn’t seem like the standard solution.

Hi Everyone,

I think that I figured this out using only the knowledge that we learnt throughout the Java course thus far:

public class Droid{

String name;

int batteryLevel;

//new parameter added in constructor to pass the transferedBattLevel

public Droid(String droidName, int transferedBattLevel){

 name = droidName;

 // add the transferedBattLevel when creating a new instance

 batteryLevel = 100 + transferedBattLevel;

}

public String toString(){

 return "Hello, I'm the droid: " + name;

}

public void performTask(String task){

 System.out.println(name + " is performing task: " + task);

 batteryLevel = batteryLevel - 10;

}

public void energyReport(){

System.out.println("Droid: " + name + "'s battery level is: " + batteryLevel);

}

public int energyTransfer(int droid1BattLevel) {

 int battLevelToTransfer = droid1BattLevel;

 // set return Value to equal the first droid's battery level

 return battLevelToTransfer;

}

public static void main(String args){

 //initialize a variable that will be used to transfer the battery level from one instance to the next

 int transferedBattLevel = 0;

 

 // pass transferedBattLevel to create the first instance - initially set to 0 for first instance

 Droid droid = new Droid("Codey", transferedBattLevel); 

 droid.performTask("dancing");

 droid.performTask("eating");

 droid.performTask("cleaning");

 droid.energyReport();

 

 // set transferedBattLevel to equal the return value from the energyTransfer method

 transferedBattLevel = droid.energyTransfer(droid.batteryLevel);

 

 //pass transferedBattLevel value from first instance to second instance

 Droid droid2 = new Droid("Digits", transferedBattLevel);

 droid2.energyReport();

}

}

Thanks a lot @katert you helped my confusion and clarified instructions provided in Project.

Thank you very much. May I know why do you use this command?