• Register
1 vote
50 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

Please log in or register to answer this question.

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,260 points

Related questions

0 votes
1 answer 21 views
21 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, 2020 Code Learner 9.8k points
0 votes
1 answer 26 views
26 views
Problem: Hi there! I am a beginner. I saw this program on Internet: class test { private int variable; public test() { variable = 9; System.out.println("Value of variable is " + variable); } } public class Main { public static void main(String[] args) ... following output: Hello World Value of variable is 9 I am unable to understand the output of this program. Can anybody please explain it for me?
asked Mar 7 Code Learner 9.8k points
1 vote
2 answers 45 views
45 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, 2020 Gavin 15.3k points
0 votes
1 answer 81 views
81 views
Anything I missed?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: Can anyone help me? I have problem regarding method range of object
asked Mar 27 Madhav96 2.3k points
1 vote
2 answers 135 views
135 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, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know ... of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".
asked Apr 20 munim01 21k points
0 votes
1 answer 12 views
12 views
Problem Can anybody help me in solving this, I am new to java.I don't understand what is a return type in java?
asked Mar 10 neeraj 10.5k points
0 votes
1 answer 538 views
538 views
Problem: Hi there! I am a bit curious about what happens if I skip the return type in function declaration. Does the compiler generate error or the program executes normally? If it executes normally, what return type is used as function return type by default?
asked Nov 28, 2020 Code Learner 9.8k points
0 votes
1 answer 6 views
6 views
Problem: Please assist me in resolving this issue: which method will return the element that has the id attribute with the specified value
asked Apr 1 rakib1 51.5k points