Java Error - "Invalid Method Declaration, Required Return Type"

Now we are studying how to use several classes in Java, and there is a project offering to create a class Circlethat will contain radiusand diameter, and then refer to it from the main class to find the diameter. This code continues to receive an error (mentioned in the header)

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

Thanks for any help, -AJ

Update 1 : Good, but I didn’t have to declare the third line public CircleR(double r)as double, right? In the book I am studying, example does not do this.

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. 
     } 

, public Circle(double r).... , public CircleR(double r)? - , , , .

+5
4

, public Circle (double r).... , public CircleR (double r)? - , , , .

, . ,

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; 
  }
 ....
} 

,

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

, .

public CircleR(double r) 

to

public Circle(double r)

( CircleR) CircleR.

,

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

, Froyo John B.

.

.

+23

( ) .

public double diameter(){...
+9

double

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

I had a similar problem when adding a class to the main method. It turns out that this is not a problem, I did not check the spelling. So, as a noob, I found out that spelling can and will spoil things. These posts helped me “see” my mistake, and now everything is fine.

-1
source

All Articles