• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formula not correct  RSS feed

 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, guide me in writing the correct formula for this code. I got the question from the Dietel.


The formula is given me a wrong answer each time I used values to test the program.
Also, I do I install this the Nurse's system in my school such that she can use it to compute BMI of my students without her seeing this codes?
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula uses integer arithmetic as is, which leads to rounding errors. you should convert the values to doubles before performing the division.

Also, I do I install this the Nurse's system in my school such that she can use it to compute BMI of my students without her seeing this codes?

There isn't much in this code that is worth protecting, is there? But regardless, if you install it as a class or jar file, then it's not trivial to recover the source code (although not terrible hard, either).

I take it the computer on which this runs has no internet connectivity? Otherwise, there are any number of web sites that will calculate this for you: https://www.google.com/search?q=online+bmi+calculator
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:The formula uses integer arithmetic as is, which leads to rounding errors. You should convert the values to doubles before performing the division.

Also, I do I install this the Nurse's system in my school such that she can use it to compute BMI of my students without her seeing this codes?

There isn't much in this code that is worth protecting, is there? But regardless, if you install it as a class or jar file, then it's not trivial to recover the source code (although not terrible hard, either).

I take it the computer on which this runs has no internet connectivity? Otherwise, there are any number of web sites that will calculate this for you: https://www.google.com/search?q=online+bmi+calculator


Ok, thank you. I asked the second question because I am new to java and I was just wondering on how to turn codes to software that can be used to solve computational problems.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there are all sort of other problems in your app. Not sure if you're willing to improve it.

Some of those problems are:
1. At the top comment says, measurements are in kilograms. Not sure how the height could be measured in kilograms.
2. Class names following Java coding convention supposed to start with an upper case. So that would read BmiCalculator.
3. Indentation is less than optimum. Look to lines 8 - 34.
4. You don't use enough methods to decompose your task. Probably would be a good idea to do so, rather than cramming all code into the main() method.
5. You don't handle the cases if user decides to make himself slimmer, for instance if he considers himself of a weight of -110 Kg. Same coding issue applies to height. On some particular inputs your application would crash completely, when the division would appear to be by... [for you to define what number].

- As an extra feature for training purpose, consider letting user to put figures in other units and convert them appropriately when calculating. You'd need to know first what figures user inputted.
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi, there are all sort of other problems in your app. Not sure if you're willing to improve it.

Some of those problems are:
1. At the top comment says, measurements are in kilograms. Not sure how the height could be measured in kilograms.
2. Class names following Java coding convention supposed to start with an upper case. So that would read BmiCalculator.
3. Indentation is less than optimum. Look to lines 8 - 34.
4. You don't use enough methods to decompose your task. Probably would be a good idea to do so, rather than cramming all code into the main() method.
5. You don't handle the cases if user decides to make himself slimmer, for instance if he considers himself of a weight of -110 Kg. Same coding issue applies to height. On some particular inputs your application would crash completely, when the division would appear to be by... [for you to define what number].

- As an extra feature for training purpose, consider letting user to put figures in other units and convert them appropriately when calculating. You'd need to know first what figures user inputted.

Thanks for your observations and corrections.  Like I said, I am new in java programming and I'm self studying using Dietel. I just finished chapter two of the textbook and I'm working on the questions given.

I think I should post my solutions to the problems given in the textbook for criticism - both constructive and destructive ones would be appreciated. Thanks once again.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't want to use "nextInt" for height, if you are taking the input in meters. Integers are 0, 1, 2...most people aren't close to one of those values.  You really need to take this as either centimeters (and keep using an integer) or as meters, but take a floating point number.  I would want to input 1.8 meters, but your program would take that and round it to 1 (i think...i'm pretty sure it would simply truncate the decimal part).
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you enter 1.8 then nextInt will throw an exception...InputMismatch or something like that?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biodun Adeniji wrote:...both constructive and destructive ones would be appreciated.

We only do constructive around here, or at least we try our best to make it that way.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:If you enter 1.8 then nextInt will throw an exception...InputMismatch or something like that?

possibly...i'm honestly not sure. I've not checked the docs.  I wasn't sure if it would parse the input up to the first non-integer character or not...i think there is something that works that way, but it may not be this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I wasn't sure if it would parse the input up to the first non-integer character or not...i think there is something that works that way, but it may not be this.

You might be thinking about setting a delimiter and/or then using next()/next(Pattern)
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I wasn't sure if it would parse the input up to the first non-integer character or not...i think there is something that works that way, but it may not be this.


Unless the separator regex involves a '.' (the default is whitespace) then 1.8 will cause nextInt to fail.
And a quick look at the JavaDoc, and it's an InputMismatchException that's thrown.
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Dave Tolls wrote:If you enter 1.8 then nextInt will throw an exception...InputMismatch or something like that?

possibly...i'm honestly not sure. I've not checked the docs.  I wasn't sure if it would parse the input up to the first non-integer character or not...i think there is something that works that way, but it may not be this.

It was just a joke.
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Biodun Adeniji wrote:...both constructive and destructive ones would be appreciated.

We only do constructive around here, or at least we try our best to make it that way.

It was just a joke
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!