Build a Droid Help

https://www.codecademy.com/courses/learn-java/projects/build-a-droid

These are the errors I get whenever I run my code
Droid.java:33: error: invalid method declaration; return type required
public energyReport() {
^
Droid.java:36: error: invalid method declaration; return type required
public energyTransfer() {
^
2 errors

Help me find my mistake

public class Droid {
  // Main function 
  public static void main (String[] args) {
    int batteryLevel = 100; 
    String name = name; 
    Droid Codey = new Droid(); 
    System.out.println(Codey); 
    Codey.preformTask("dancing"); 
}

  public Droid(){
    int batteryLevel;
     //Instance fields 
    String name; 

  }

public void preformTask(String task) {
  System.out.println(name + " is preforming task: " + task);
  int batteryLevel = batteryLevel - 10; 

}
// Constructor method 
public int Droid (String droidName) {
  int batteryLevel = 100; 

}

public String toString() {
  System.out.println("Hello, I'm the droid: " + name); 
}

public energyReport() {
  System.out.println(batteryLevel); 
}
public energyTransfer() {
  energyTransfer.batteryLevel = batteryLevel(energyTransfer); 
}
}
1 Like

Hi!

It’s as the error says “invalid method declaration; return type required”.

Remember that in java, all method declarations require a return type.
Like when. you declare public String toString(), you’re saying that this method will return a String type.

Examples of return types: int, double, string, void (for when you don’t want to return anything. It’s useful for just printing statements or doing operations that don’t need returns).

There are more return types and you can check this in Oracle’s documentation for future reference: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html.

Within the

you should have void since you don’t need to return anything.
While with toString you don’t need really need it either.
Also with energyReport() it might be better to just add a void in-between public and energyReport same thing with energyTransfer()

Follow the error messages:

  • toString() is already a java method so you can’t overwrite that (you’ll need a different name for it).

  • Line 18, you write name but never declare it in the method or pass it in a parameter. This is similar with the others.

Another thing I noticed, you already have an instance field: public Droid()(you might want to lower case the droid) so you don’t need the public void droid()

(Line 27)As toastedpitabread said, you need to write something like this:

public void toString(String droidName){
    System.out.println("Hello I am " + droidName);
  }

I won’t tell you the exact things about the rest but I think you can figure it out now.

The String name should be in between the main and the class part. That way the name can be used throughout your whole code.

The int batteryLevel would also be there but you set it to 100 when you make a new instance of a droid.

Also you might want to rethink the energyTransfer part, it will not transfer the batteryLevel from a droid to another. (I might be wrong, I haven’t tried it that way)

I think this is enough to eliminate quite a few errors, if it still has problems then we can help you again. :+1: :+1: :+1:

1 Like