• Register
1 vote
25 views

Problem:

Well, I am a new student of java programming. Nowadays I am learning how to use multiple classes in a java program. Based on my learning I’ve written a code that gave me an error, “Invalid method declaration; return type required

I am attaching the snippet here:

public class Circle 
    { 
      public CircleR(double r) 
      { 
       radius = r; 
      } 

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

Can you, please tell me where I am doing the wrong thing?

12 7 7
15,250 points

1 Answer

2 votes

Solution:

When defining constructors of a class, they should have the same name as its class. You are getting the error because your constructor has a different name from its class.

To run the program error-free you need to change the class name or the constructor name.  

public CircleR(double r)

to the constructor name

public Circle(double r)

or the class name from:

public class Circle

to

public class CircleR

Keep discovering new things. Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: Hello there! I am working with C# in Visual Studio and I am learning user-defined functions in C#. When I try to execute my program, it throws an error that says method must have a return type. I have checked my function and I find nothing wrong with it. ... ;t know what’s the problem and what should I do. If someone could tell me, that would be a big favor. Thanks in advance
asked Oct 16 Sheeza 3.5k points
1 vote
2 answers 34 views
34 views
Problem: Hello java programmers, Myself is a new student in java. I am practicing basic java methods and objects. Finding beginner-level problems and trying to solve them. I recently got a line that says, all local variables in a method have default values. What does it mean? Can you please write a simple code that makes me understand this well? Thanks guys.
asked Mar 26 Gavin 15.3k points
1 vote
2 answers 55 views
55 views
Problem: Hello Kodlogs, I have a simple java code that I wrote a few minutes back I was trying to compare numbers using methods. The program throws the error again and again: this method must return a result of type int. I couldn’t figure out the problem. Could you please shed some light on this ... []) { int x = 8; int y = 9; int z = 20; System.out.println(largest(x, y, z)); } } Many thanks
asked Mar 27 Gavin 15.3k points
1 vote
1 answer 44 views
44 views
Problem: I am a new Java programmer who is learning new things every day throughout doing the wrong things I have a giant piece of java code that I wrote for my practise. A certain portion of my code is generating an error and saying Error: 'void' type not allowed here. Why ... a close look into it and let me know if you can find out any problem with it. I can’t wait to hear from you. Thanks
asked Apr 26 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
I want to make a static reference to the non-static method.
asked Sep 22 Daniel Anderson 4k points