开发者

Java error - "invalid method declaration; return type required"

We are learning how to use multiple classes in Java now, and there is a project asking about creating a class Circle which will contain a radius and a diameter, then reference it from a main class to find the diameter. This code continues to receive an error (mentioned in the title)

public class Circle
{
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
        double d = radius * 2;
        return d;
    }
}

Thanks for any help, -AJ

Upd开发者_StackOverflow社区ate 1: Okay, but I shouldn't have to declare the third line public CircleR(double r) as a double, right? In the book I am learning from, the example doesn't do that.

public class Circle 

    { 
        //This part is called the constructor and lets us specify the radius of a  
      //particular circle. 
      public Circle(double r) 
      { 
       radius = r; 
      } 

      //This is a method. It performs some action (in this case it calculates the 
        //area of the circle and returns it. 
        public double area( )  //area method 
      { 
          double a = Math.PI * radius * radius; 
       return a; 
    } 

    public double circumference( )  //circumference method 
    { 
      double c = 2 * Math.PI * radius; 
     return c; 
    } 

        public double radius;  //This is a State Variable…also called Instance 
         //Field and Data Member. It is available to code 
    // in ALL the methods in this class. 
     } 

As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.


As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.

When defining constructors of a class, they should have the same name as its class. Thus the following code

public class Circle
{ 
    //This part is called the constructor and lets us specify the radius of a  
    //particular circle. 
  public Circle(double r) 
  { 
   radius = r; 
  }
 ....
} 

is correct while your code

public class Circle
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
       double d = radius * 2;
       return d;
    }
}

is wrong because your constructor has different name from its class. You could either follow the same code from the book and change your constructor from

public CircleR(double r) 

to

public Circle(double r)

or (if you really wanted to name your constructor as CircleR) rename your class to CircleR.

So your new class should be

public class CircleR
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public double diameter()
    {
       double d = radius * 2;
       return d;
    }
}

I also added the return type double in your method as pointed out by Froyo and John B.

Refer to this article about constructors.

HTH.


Every method (other than a constructor) must have a return type.

public double diameter(){...


You forgot to declare double as a return type

public double diameter()
{
    double d = radius * 2;
    return d;
}


I had a similar issue when adding a class to the main method. Turns out it wasn't an issue, it was me not checking my spelling. So, as a noob, I learned that mis-spelling can and will mess things up. These posts helped me "see" my mistake and all is good now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜