Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an application that lets the user enter his or her weight (in pounds) and

ID: 3776489 • Letter: C

Question

Create an application that lets the user enter his or her weight (in pounds) and height (in inches). The application should display the user's body mass index (BMI). The BMI is often used to determine whether a person is overweight, underweight, or at optimal weight. A person's BMI is calculated with the following formula: bmi = weight * 703 / Math.Pow(height, 2) (Hint: See Math.Pow method on page 174-175) The program should also display Status in a label control indicating whether the person has Optimal Weight, is Underweight, or is Overweight. A person's weight is considered to be Optimal if his or her BMI value is between 18.5 and 25. If the BMI value is less than 18.5, the person is considered to be Underweight. If the BMI value is greater than 25, the person is considered to be Overweight. Implement Data Conversion and Input Validation Implement data conversion validation for proper data type using the TryParse method to ensure that the user enters an appropriate value for the weight (in pounds) and height (in inches). If the user does not enter an appropriate value a message should be displayed and focus should return to the text box which caused the issue and it should be cleared. Implement input validation for accuracy that ensures that the value of both weight (in pounds) and height (in inches) is greater than zero. If either weight or height is not greater than zero a message should be displayed and focus should return to the text box which caused the issue and it should be cleared. Implement Value-Returning Methods Implement the following value-returning methods: A method named CalculateBMI that accepts the value of weight (in pounds) and height (in inches) and calculates the BMI value using the formula provided above and returns the result (use appropriate return type) back to the calling method (i.e., the calculate button click event) to be displayed on the form. A method named GetStatus that accepts the value of BMI and determines the status (Optimal Weight, Overweight, Underweight) and returns the result (use appropriate return type, i.e., a string data type) back to the calling method (i.e., the calculate button click event) to be displayed on the form. (Hint: This means that the decision (selection) structure for determining Status based upon BMI MUST be placed in the GetStatus method).

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BMICalculator {
  
   private JFrame bMImainFrame;
   private JLabel bMIheadLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public BMICalculator(){
      prepareGUI();                                // preparing GUI
   }

   public static void main(String[] args){
      BMICalculator BMICalculator = new BMICalculator();    
      BMICalculator.showTextFieldDemo();
   }

   private void prepareGUI(){                       
      bMImainFrame = new JFrame("BMICalculator");
      bMImainFrame.setSize(400,400);
      bMImainFrame.setLayout(new GridLayout(3, 1));
      bMImainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }      
      });  
      bMIheadLabel = new JLabel("", JLabel.CENTER);      
      statusLabel = new JLabel("",JLabel.CENTER);  

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      bMImainFrame.add(bMIheadLabel);
      bMImainFrame.add(controlPanel);
      bMImainFrame.add(statusLabel);
      bMImainFrame.setLocationRelativeTo(null);
      bMImainFrame.setVisible(true);
   }

   private void showTextFieldDemo(){
      bMIheadLabel.setText("BMICalculator");

      JLabel weightlabel= new JLabel("Weight: (Pounds)", JLabel.RIGHT);
      JLabel hightLabel = new JLabel("Hight: (inches)", JLabel.CENTER);
      final JTextField weightText = new JTextField(6);
      weightText.requestFocus();
      final JTextField hightText = new JTextField(6);

      JButton okButton = new JButton("OK");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String wData = weightText.getText();
            String hData = hightText.getText();
            double wValue;
            double hValue;
            try{
               wValue = Double.parseDouble(wData);
               try{
                  hValue = Double.parseDouble(hData);
                  double result_value = calculateBMI(wValue,hValue);                         // calling calculateBMI function
                  String result_string = getStatus(result_value);                            // calling getStatus function
                  statusLabel.setText(" BMI :- "+result_value+" Status :- "+result_string);// print results to status label
               }
               catch(NumberFormatException E){
                  JOptionPane.showMessageDialog(
               bMImainFrame, "Enter hight in valid Format");                        // validating input formats
                  hightText.setText("");                                            // clearing text box
                  hightText.requestFocus();                                      // seting focus to text field which caused issue
               }
            }
            catch(NumberFormatException E){
               try{
                  hValue = Double.parseDouble(hData);
                  JOptionPane.showMessageDialog(
                  bMImainFrame, "Enter Weight in valid Format");                 // showing alter to user about invalid format
                  weightText.setText("");
                  weightText.requestFocus();
               }
               catch(NumberFormatException A){
                  JOptionPane.showMessageDialog(
                  bMImainFrame, "Enter weight and hight in valid Format");
                  hightText.setText("");
                  weightText.setText("");
                  weightText.requestFocus();
               }
            }
         }
      });

      controlPanel.add(weightlabel);
      controlPanel.add(weightText);
      controlPanel.add(hightLabel);     
      controlPanel.add(hightText);
      controlPanel.add(okButton);
      bMImainFrame.setVisible(true);
   }
   private double calculateBMI(double weight,double hight){
      double bmi = weight * 703 / Math.pow(hight, 2);                                  // calculating BMI
      return bmi;                                                                      // returning value without printing
   }
   private String getStatus(double result_value){                           
      if(result_value < 18.5){
         return "Underweight";                                             // returning status without printing
      }
      else if (result_value > 25){
         return "Overweight";
      }
      else{
         return "Optimal";
      }
   }
}

// end of program

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote