Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Circle in class Circle cannot be applied to given types  RSS feed

 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, My code is ending with this error.  What I mainly do not understand is that, method I am creating is requiring double to be sent and I am sending double but when I run the code, it shows as if double was not sent..

Circle.java


TestCircle.java


Error:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you define the Circle constructor that accepts a double as an argument?
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teacher that put this assignment down did not ask me to create a constructor. She wanted me to only create a method within the Circle.java and work with that Do I have to have a constructor too? If so, how do I make that constructor point to the method I wrote so it does what I needed to do?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch

[edit]Add this link to the Java™ Tutorials, which I think will help with your problem. By the way, how are you turning that input into a BigDecimal?
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.. I know couple other web languages like PHP, CSS, HTML.. even some JavaScript.. but Java is nothing like them.. So it is throwing me off at multiple levels..
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your method is defined as findArea(double radius) then that method can be made static, since it does not depend on a particular Circle instance (similar to how Math.sqrt(double x) is a static method.

If, on the other hand, if your method is defined as getArea() - with no argument - then you would need call it from a particular instance of Circle.

Also, you should use the pre-defined value of pi (Math.PI) rather than using the less accurate 3.14159f.
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred. Assignment requires findArea method to be called as an object from TestCircle.java. So I cannot make it static and use it in the file that has static void main in it. Assignment wants me to use 3.14159 instead of Math.PI.. I Know it would be way better that way but I have to use 3.14159 with this code..

I just can't understand why it's saying required no arguments when the findArea method inside Circle.java does require double....
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't make sense to have a Circle method that has no explicit constructor (and therefore uses the default no-argument constructor) and then has only a non-static findArea() method.

A Circle class should have a constructor that takes the radius as an argument. The constructor would save that radius in an instance variable.
Then then there would be a findArea() method with no argument, that computes the area based on the radius stored from the constructor call.
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does make sense. There is this hint in the assignment that might have something to do with it:
Hint: You were not asked to create a constructor. However, you can still create an object and you still have a constructor. Why?
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This surprisingly does the job, I still do not understand how though..


Circle c = new Circle();
c.findArea(4.5);
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The how is:

You have a method on your class called findArea.  That method is an instance method, (it's not static) so that means it can only be called via an object of Circle.
Consequently you need to construct one, which is what's happening in the line:


This creates a Circle object and assigns its reference to the variable c.
Now you have something you can actually call the method through.

Earlier what you were doing was telling Java to construct a Circle object using a constructor that takes a parameter.
However there was no such constructor defined in Circle, so the compiler pointed this out.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:This surprisingly does the job, I still do not understand how though..


Circle c = new Circle();
c.findArea(4.5);
And Fred Kleinschmidt will explain why you can get that code to compile and why it is bad code. Maybe I'll explain it too, but going at it the other way round from Dave Tolls.
The findArea method, as FS told you yesterday, does not take any information from the object and does not send any information to the object. What it does is take information from the outside world (via its parameters) and send it back to the outside world(via its returned value). If you find the most dubious classification of methods ever, you find that is a 1368; that method counts mathematically as a function. In that case, as FS says, you shou‍ld consider making the method static, and static methods shou‍ld be called on the name of the class, not the name of the reference to an object. The reason for using the class name is that static method are not ploymorphic and you can get dangerous confusion if you use the name of an object reference to call a static method on.

I have had to shorten the big numbers in your original post because otherwise the post was too difficult to read. If you want a BigDecimal you can do this sort of thing:-The compiler will catenate those Strings for you. Don't create a BigDecimal from a double because you will get problems about precision. Remember those double literals of yours can only be read in part; the double datatype runs out of precision after the first 16 digits. Also, if you have more than about 155 digits before the decimal point in your number, squaring it might produce infinity as a result because you overflow the range of the datatype. The square root of the largest double value is about 1.34078079299425963249e154.
 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:Teacher that put this assignment down did not ask me to create a constructor. She wanted me to only create a method within the Circle.java and work with that Do I have to have a constructor too? If so, how do I make that constructor point to the method I wrote so it does what I needed to do?
...
Hint: You were not asked to create a constructor. However, you can still create an object and you still have a constructor. Why?

Here's my $0.02:

You should always consider context. In this case, think of the learning goals for the exercise. These can be inferred from some of things you've told us:

1. Understand Java's default constructor mechanism - you were asked not to create a constructor and then you discovered that you can still create an object. That's because if there is no explicit constructor, Java provides a default constructor that takes no arguments.

2. Understand instance methods - you are asked to write an instance method rather than a static method. Granted, this is not a very good example and others have already explained why an instance method doesn't make much sense here.

3. (For the teacher) To be able to automate the verification of submitted work - by asking you to use specific method names and literal values, the teacher is probably using a small test program that's similar to your TestCircle class to make it easier to check all the submitted assignments.
 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another $0.02:

You might still be confused as to why it doesn't make much sense for the findArea() method you wrote to be an instance method rather than a static method.

You can think of instance methods as something that all objects of a class can do but in a way that's specific to each object. The specificity comes from some knowledge or attribute (data/information) that is unique to each object.

For example, two people may both respond to the question "What's your phone number?" but their answers are probably going to be very different. Likewise, each of them would probably respond differently if you told them to put on their favorite shirt. However, if you ask these same two people the question "What's the sum of 1 and 3?" they will both give you the same answer. The first question is like calling an instance method, getPhoneNumber().  Telling them to put on their favorite shirt would be like calling a putOnFavoriteShirt() instance method. The second question is like calling a static method, findSum(a, b). A static method doesn't depend on knowledge/information that's unique to each object whereas an instance method does.

In the case of the findArea() method you wrote, you are passing in the radius of a circle. This is the only information needed to calculate the circle's area so in this case it doesn't make sense for findArea() to be an instance method because the radius information is already provided externally.  If, however, you had a constructor that took the radius of the Circle object that will be created, then a method like getArea() would make more sense because it would use the radius specified when the instance was created to perform the calculation.


 
Junilu Lacar
Sheriff
Posts: 11145
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should clarify something from that last example though.

It's perfectly legal, but not recommended practice, to call a static method on an instance. That is,

I wanted to point this out because I realized that my previous example of asking two people for the sum of 1 and 3 is like what we're doing here. The analogy kind of breaks down in this case because there's no real-world equivalent to calling something like Person.getSum(a, b). All calls will give the same result.

I hope I haven't confused you even more with this.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you did have a constructor that takes a single argument (the radius), and an instance method getArea() that takes no arguments and returns a double.
Then you could do something like this:

Since the constructor saved the radius as an instance variable, there is no need to pass the radius to the method - it just uses the instance variable.

You could also write other methods that take no arguments, such as getDiamdeter(), getCircumference(), etc.:

So once you have tokl the circle what its radius is, you don't have to remember that radius every time you want to compute the area, or diameter, or anything else
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you:
- Junilu Lacar
- Dave Tolls
- Fred Kleinschmidt
- Campbell Ritchie

It makes sense to me know.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our pleasure
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!