Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Newbie needs quick help with methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

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"

Very thankful for any help.


here's the code:



Thanks!
 
Marshal
Posts: 66237
250
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Simon Eriksson
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much!!

I'll think about this, and i'm very much looking forward to your next reply!!
 
Campbell Ritchie
Marshal
Posts: 66237
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 theassignment 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:
  •  
    Campbell Ritchie
    Marshal
    Posts: 66237
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Simon Eriksson wrote:Thank you . . .

    That's a pleasure
     
    Ranch Hand
    Posts: 73
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Simon Eriksson wrote:Hi!
       void program() {

           // --- Output ---------
           userInput();
           calculating();     <-- here
       }

    calculating() ; method requires int[] parameter
     
    lowercase baba
    Posts: 12773
    51
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 70
    Eclipse IDE Debian Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Here is more information:

    Implementation of Tuple class in Java

    Here is more information about declaring java variables private:

    Variable Access Specifier in Java How To Short Guide
     
    Campbell Ritchie
    Marshal
    Posts: 66237
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tuples? What is the relevance of tuples to the current problem?
     
    Mark Ii
    Ranch Hand
    Posts: 70
    Eclipse IDE Debian Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!