I'm new to java and i'm just trying to make a quick little program work.
I get an error when trying to call the calculating method, but i do not understand what i'm supposed to do.
The error that IDEA tells me is: "calculating(int) cannot be applied"
And well done finding the code button. It makes your code look better, and we can see you know how to format code.
Of course, you have missed the real error in that code. Who care about compiler errors when you could simply pass a reference to an int. You wrote the method to take an int, so you will have to pass an int as a parameter.
Unfortunately that isn't the best object‑oriented way to calculate the BMI. The object‑oriented way is for the object to have height and weight fields, and it would be best for them to be set by the constructor.Maybe your constructor would calculate the BMI and set a bmi field. But you can't calculate the BMI as you are doing. I shall post again in a few minutes.
posted 3 weeks ago
Thank you so much!!
I'll think about this, and i'm very much looking forward to your next reply!!
posted 3 weeks ago
Unfortunately there isn't a power operator in Java®. If you look through the JLS (=Java® Language Specification), you will find that the ^ operator does something completely different. And what you wrote is incorrect arithmetic, but correct syntax, so the compiler will let it pass happily, in blissful ignorance of the wrong result you are going to get. What you actually want isThe reason that code won't work is that you are doing integer arithmetic on the right of the = assignment operator. If the height is greater than the square root of the weight, that expression will evaluate to 0 I can think of three solutions:-
1: Declare and enter height and weight as doubles rather than ints
2: Use an explicit cast:- (double) weight / height / height
3: Use an implicit cast by introducing a double into the arithmetic 1.0 * weight / height / height:
just to clarify a bit more, since you may see this kind of error again....
You defined your "calculating" method like this:
That's line 33 in your original post. this says "I am hereby saying that there will be a method called 'calculating'. To use it, an integer array must be given to it. When it is done, it will spit back a double value".
That's all well and good. But then on your line 12, you try an call a method called "calculating", but don't give it an integer array. Java doesn't know what to do. The only method with that name REQUIRES an integer array - so it complains. It says "I don't know of anything named 'calculating' that can be executed with no parameters".
Basically, the method name and the list of parameters it takes is called the method signature. The calling line must match some version that is actually defined, and your original code doesn't have that.
Note: Campbell's advice is still correct, re: best OO way to do things. But even if you do create an object with height and weight, your method signatures must match.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
If you want to be more advanced, are willing to dig deep, play around and want to know how tuples really work, you can use custom made Tuple class and declare the tuple object private and two variables can be accessed (set/get) from a Tuple object from within the same class (Ex1BMI) only. It's kind of neat and convenient.
As tuples are a quick way to store variables I thought why not store the inputWeight and inputHeight values into tuple and pass the tuple object to calcuating() method as a parameter or store the data individually into data variable array.
I wanted to show this person another way of doing things.
1 + 1 = 10 is always one of the plausible truths for me.
It's a pleasure to see superheros taking such an interest in science. And this tiny ad: