• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

BMI GUI

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*HI, So this is my BMI code up to date. My next assignment is to make this a GUI. I have not made one before and am just curious.
If anyone has any tips for a new programmer they would be more than welcome.
Thanks!
*
* this program is designed to receive user input of weight in pounds and height in inches
* it than calculates the users BMi
* determines which weight classification the user false under
* and displays the results. This code is designed to handle exceptions
* The program than writes the output data to a the file BMIResults5.txt
* This took me about another hour to modify. I got stuck trying to write my toString to the file.
*  My file kept coming out empty.
* Honor Code: This is my own code. I only used the exception handling zip file *** sent us and the pearson
* exception handling video
* */


                   MAIN
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code button, but you didn't quite get it right. You had line breaks inside the body of the tags, and when I got rid of those line breaks, didn't it get better

Some good code there: I am pleased to see you aren't trying to mix business logic and display code in the same class. I can see all sorts of things you could improve, however.
  • 1:You can (should?) delete most of those comments.
  • Comments like...are probably unnecessary because you are saying underweight. The comment is also not 100% correct. <br /> Another problem with comments is that they make the lines too long. <br /> You might do well to have a constant private static final UNDERWEIGHT_THRESHOLD = 18.5; instead of the magic number.
  • 2: On the same lines, the two constants in lines 13 and 14 should be marked final too.
  • 3: I try to use only < and > if possible, because they are easier to understand than <= and >=.
  • I have a rule of thumb about repeated ifs: what you write depends on whether the boundary value is in the lower category or the higher category. The fewer comparison symbols you use, the less chance there is of making the mistake you made. Hint to find it:- <br /> What is the category for bmi=25.5? <br />
  • a: All the ifs must go in order.
  • You have got that bit right already.
  • b: If the boundary value is in the lower category go large→small and use only >
  • c: If the boundary value is in the upper category go small→large and use only <
  • I think bmi=18.4999999999999 is underweight and bmi=18.5 is underweight and bmi=18.50000000000001 is normal. The boundary value is in the lower category. So write your ifs like this:-If bmi=18.5 counted as normal, you would have the boundary in the upper category and would write the ifs ascending. Like for the mark you will get for that exercise:-
  • 4: Separate the keyboard input into a different class. You want to be able to use the BMI calculator like this:-
  • Search my posts for “Scanner utility class”. Look in the Java in General and Beginning Java fora. You will find a utility class for reading from the keyboard with Scanners, but if you are unlucky, you will find it in small parts, or you might even not notice a nasty error I made once.
    Ages ago, Rob Spoor taught me that you shouldn't catch input mismatch exceptions. You use a loop like this:-It is necessary to get rid of the incorrect input, which is what line 5 does. That loop will never cause an input mismatch exception to be thrown. You may swap the order of lines 4 and 5.
     
    Amy leilani
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh wow, thank you so much! I am so happy I found this site!
    And the lines look way better! I will make sure to get it right next time.
    I can't wait to update my code. I'll post back when it's updated.
    Thanks so much!
    A
     
    Campbell Ritchie
    Marshal
    Posts: 71070
    292
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Amy leilani wrote:. . . thank you . . .

    That's a pleasure

    I am so happy I found this site! . . .

    Gee, shucks, yer gettin' us all 'barrassed.
     
    Amy leilani
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    NICE!! Thanks so much! I cleaned up my determineBMIClass and it looks WAY better! I got my GUI going and ready for submission. Thanks for your help!
     
    Campbell Ritchie
    Marshal
    Posts: 71070
    292
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done
     
    What are your superhero powers? Go ahead and try them on this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic