The Artima Developer Community
Sponsored Link

Java Answers Forum
Problems

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ernie Douglas

Posts: 18
Nickname: y2j
Registered: Nov, 2002

Problems Posted: Nov 25, 2003 7:09 AM
Reply to this message Reply
Advertisement
Could someone help me with this program, i have managed to get it all working however, if i go past the number of weight or height, an error message comes up which i can't get out of unless exiting all program, there are a few other bugs but not quite what, could someone help?

import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
 
public class Statistics {
public static void main ( String args[] )
{
int classTotal, mark; //hold user entered numbers
int extremeobese = 0, veryobese = 0,
obese = 0, overweight = 0,
healthy = 0, underweight = 0;
 
 
// for the count of each category
    int count=0, largest=0, smallest=100, total=0;
//displaying results
    double average = 0.0; //float to display correct average
    double bmi = 0.0;
    double height;
    double extremeobesePC = 0.0,
    veryobesePC = 0.0,
    obesePC = 0.0,
    overweightPC = 0.0,
    healthyPC = 0.0,
    underweightPC = 0.0, BMIlarge=0.0, BMIsmall=100.0;
//displaying averages
 
 
    String inputString;
//get total in class number
    inputString = JOptionPane.showInputDialog("Enter the Number of Dieters in the Class (0 - 25): " );
    classTotal = Integer.parseInt ( inputString );
//validate this number
 
    
    while (classTotal < 0 || classTotal > 25) {
    JOptionPane.showMessageDialog(null, "Must be between 0 and 25",
    "Input Error", JOptionPane.ERROR_MESSAGE );
    inputString = JOptionPane.showInputDialog("Incorrect Entry - Please Re-Enter: ");
    classTotal = Integer.parseInt (inputString);
}
    if (classTotal == 0)
{
    JOptionPane.showMessageDialog(null, "The Program Is Closing Down",
    "Exit Messeage", 
    JOptionPane.INFORMATION_MESSAGE );
}
    else
{
//get marks
    for (int i = 1; i <= classTotal; i++)
{
    inputString = JOptionPane.showInputDialog(null,
    "Dieter Number" + i + " Enter Weight (0 - 200)kg: ");
    mark = Integer.parseInt (inputString);
//validate this number
    while (mark < 0 || mark > 200) {
    JOptionPane.showMessageDialog(null, 
    "Must Be Between 0 and 200",
    "Input Error", 
    JOptionPane.ERROR_MESSAGE);
 
    JOptionPane.showMessageDialog(null,
    "Incorrect Entry - Please Re-Enter: " );
    mark = Integer.parseInt (inputString);
}//end while
    total += mark; // running total to calculate average
    if (mark > largest) // check for largest number
    largest = mark;
 
    if (mark < smallest) // check for smallest number
    smallest = mark;
 
    inputString = JOptionPane.showInputDialog(
    "Dieter Number" + i + " Enter Height (0 - 2.50)m: ");
    height = Double.parseDouble (inputString);
//validate this number
    while (height < 0 || height > 2.50) {
    JOptionPane.showMessageDialog(null, 
    "Must Be Between 0 and 2.50m",
    "Input Error", 
    JOptionPane.ERROR_MESSAGE);
 
    JOptionPane.showMessageDialog(null,
    "Incorrect Entry - Please Re-Enter: " );
    height = Integer.parseInt (inputString);
}//end while
 
 
bmi = mark/(height*height);
//calculate category and increment category total
    
    if (bmi >= 40){
    extremeobese++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are Extremely Obese");
    }
    else
    if (bmi >= 35 || bmi <= 39.9) {
    veryobese++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are Very Obese");
    }
    else
    if (bmi >= 30 || bmi <= 34.9){
    obese++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are Obese");
    }
    else
    if (bmi >= 25 || bmi <= 29.9){
    overweight++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are Over Weight");
    }
    else
    if (bmi >= 18.5 || bmi <= 24.9){
    healthy++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are Healthy");
    }
    else
    {
    underweight++;
    JOptionPane.showMessageDialog(null, "Your bmi is" + bmi + "You Are under Weight");
    }
} //end for
    DecimalFormat twoPlaces = new DecimalFormat ("0.00");
//format float to 2 decimal places
 
 if (bmi > BMIlarge) // check for largest number
    BMIlarge = bmi;
 
    if (bmi < BMIsmall) // check for smallest number
    BMIsmall = bmi;
    average = (double) total / classTotal;
//ensure correct average using
//cast to float
//calculate percentages
    extremeobesePC = ((double) extremeobese *100)/classTotal;
    veryobesePC = ((double) veryobese *100)/classTotal;
    obesePC = ((double) obese *100)/classTotal;
    overweightPC = ((double) overweight *100)/classTotal;
    healthyPC =((double) healthy *100)/classTotal;
    underweightPC = ((double) underweight *100)/classTotal;
 
 
 
    JTextArea outputArea = new JTextArea ( 6, 40 ); //results display
    String output = "Extremely Obese            Number: " + extremeobese + 
    "                   Percentage " + twoPlaces.format(extremeobesePC) + "\n";
    output += "Very Obese                       Number: " + veryobese + 
    "       Percentage " + twoPlaces.format(veryobesePC) + "\n";
    output += "Obese                            Number: " + obese + 
    "                   Precentage " + twoPlaces.format (obesePC) + "\n";
    output += "Over Weight                      Number: " + overweight + "                  Percentage " + 
    twoPlaces.format (overweightPC) + "\n";
    output += "Healthy                          Number: " + healthy + 
    "                   Percentage " + twoPlaces.format (healthyPC) + "\n";
    output += "Under Weight                     Number: " + underweight + 
    "                   Percentage " + twoPlaces.format (underweightPC) + "\n\n\n\n";
 
    output += "Highest Weight: " + largest + "\n";
    output += "Lowest Weight: " + smallest + "\n";
    output += "Average Weight: " + twoPlaces.format (average) + "\n\n\n";
 
    output += "Highest bmi: " + twoPlaces.format (BMIlarge) + "\n";
    output += "Lowest bmi: " + twoPlaces.format (BMIsmall) + "\n";
 
    outputArea.setText ( output );
//title
    JOptionPane.showMessageDialog(null, outputArea, 
    "Student Marks - Ernie Douglus",
    JOptionPane.INFORMATION_MESSAGE );
}
    System.exit( 0 );
}
} 

Topic: share source codes - JBank Previous Topic   Next Topic Topic: Limiting characters displayed

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use