Header Ads

you are required to create a C# WPF application using Visual Studio that calculates the Body Mass Index

Problem Statement:



The body mass index (BMI) is a measure that uses your height and
weight to work out if your weight is healthy. BMI takes into account natural
variations in body shape, providing a calculated value for a healthy weight
range for a particular height. 








If your BMI is:





  • below 18.5 – you're in the underweight range



  • between 18.5 and 24.9 – you're in the healthy weight range


  • between 25 and 29.9 – you're in the overweight range


  • 30 or over – you're in the obese range




People who have overweight or obese, compared to those with healthy weight,
are at increased risk for many serious diseases and health conditions. These
include:



High blood pressure
(hypertension).



Type 2 diabetes.



Coronary heart disease.



Sleep apnoea and breathing
problems.



Many types of cancer.



Low quality of life.



Mental illness such as clinical
depression, anxiety, and other mental disorders.



Body pain and difficulty with
physical functioning.



Maintaining a healthy weight is important for overall health and can help
you prevent and control many diseases and conditions. 



Keep in view the above discussion, you are required to create a
C# WPF application using Visual Studio that calculates the
Body Mass Index with the following functional requirements:



Application Interface:



When the application starts, the
following screen should be displayed as shown in the sample output
below:


 









There should be your
OWN VUID in RED color that should be visible at the top left
corner as shown in the sample output.



There should be two labels of
“Height” and “Weight” along with two “text boxes” visible at the center of
the screen similar to as shown in the sample output. 



There should be a combo box button
below the textbox of weight and it should contain two combo box items,
“Standard” and “Metric” as shown in the sample output in .GIF file.



After the combo box, there should
be a button labeled as “CalculateBMI” and another button labeled as “Clear”,
similar to as shown in the sample output.



Below the “CalculateBMI” button,
there should be two labels, “Result” and “Comments” and in the horizontal
space after these labels, there should be two hidden labels which
will only be visible if the button “CalculateBMI” is clicked as shown in the
sample output in .GIF file.



There should be a text block
having text “MY BMI Calculator” rotated with an angle of 8 degrees at
the righter space after the text boxes of Height and Weight, similar to as
shown in the sample output.



The whole background should be
colored of your own choice as shown in sample output. The buttons
”CalculateBMI” and “Clear” should have a background color of your own
choice. The background color below your VUID should be different from the
whole background color, as shown in the sample output.



You have to use the all the
appropriate properties like margin, padding, font sizes, colors, font
weights etc to make a similar application as shown in the sample
output. 






Execution Requirements:



Firstly, without entering any
values in any text box, you have to click on the “CalculateBMI” button. So,
on clicking the ”CalculateBMI” button, a message should be displayed having
the text “please enter the height and weight”.


 









Then you will enter the height as
71 inches and weight as 150 pounds and click “CalculateBMI”
button. After clicking the button, the hidden labels should be visible with
correct values and comments. 



The Calculated BMI value should be
rounded-off to only two fractional digits after the decimal.


 









Then you will select the combobox
item to “Metric” on combo box without changing the values, (using same
values as height = 71 CM and weight = 150 KG). On changing the
combobox item to “Metric”, the labels of the Height (inches) and
Weight (Pounds) should be changed to “Height (CM)” and
“Weight (Kg)” as shown in the sample output in .GIF file.



Now, Click the “CalculateBMI”
button again, the result and comments should change with correct BMI value
and comments.



From the Calculated BMI value, you
have to use the appropriate comment as per the following table:



                   
                     
                   







Then you will click the “Clear”
button. On clicking it, all the text boxes should be emptied and BMI value
and BMI comments should be cleared and should be made hidden, same like they
were hidden at the start of the application.



Formulas for Calculating BMI:



For BMI in Standard: Use
the following formula in your application for calculating BMI in Standard
(I.e. Height in inches and Weight in
Pounds)



BMI_in_Standard = 703 x (weight / (height x height));



For BMI in Metric: Use the
following formula in your application for calculating BMI in Metric (I.e.
Height in CM and Weight in KG)



 BMI_in_Metric = 10000 x (weight / (height x height));



Solution:






Program:


using System;


using System.Windows;


using System.Windows.Controls;


using System.Windows.Media;






namespace BMICalculator


{



    public partial class MainWindow : Window


    {



        // Initialize variables for height, weight, and
units



        double height = 0;



        double weight = 0;



        string units = "Standard";







        public MainWindow()


        {



            InitializeComponent();







            // Set background color of
application



            Background = new
SolidColorBrush(Color.FromRgb(240, 240, 240));







            // Set background color of
header



            header.Background = new
SolidColorBrush(Color.FromRgb(200, 200, 200));







            // Set font size and weight of
labels and buttons



            labelHeight.FontSize = 18;



            labelWeight.FontSize = 18;



            labelResult.FontSize = 18;



            labelComments.FontSize =
18;



            labelResult.FontWeight =
FontWeights.Bold;



            labelComments.FontWeight =
FontWeights.Bold;



            buttonCalculate.FontSize =
18;



            buttonClear.FontSize = 18;







            // Set padding for labels and
buttons



            labelHeight.Padding = new
Thickness(10);



            labelWeight.Padding = new
Thickness(10);



            labelResult.Padding = new
Thickness(10);



            labelComments.Padding = new
Thickness(10);



            buttonCalculate.Padding = new
Thickness(10);



            buttonClear.Padding = new
Thickness(10);







            // Set background color of
buttons



            buttonCalculate.Background = new
SolidColorBrush(Color.FromRgb(0, 122, 204));



            buttonClear.Background = new
SolidColorBrush(Color.FromRgb(255, 128, 128));


        }







        private void buttonCalculate_Click(object
sender, RoutedEventArgs e)


        {



            // Check if height and weight
values have been entered



            if (textBoxHeight.Text == "" ||
textBoxWeight.Text == "")



            {



               
MessageBox.Show("Please enter the height and weight");



                return;



            }







            // Get height and weight
values



            height =
double.Parse(textBoxHeight.Text);



            weight =
double.Parse(textBoxWeight.Text);







            // Calculate BMI based on
units



            double bmi = 0;



            if (units == "Standard")



            {



                bmi = (weight *
703) / (height * height);



            }



            else if (units == "Metric")



            {



                bmi = weight /
(height * height);



            }







            // Round BMI to two decimal
places



            bmi = Math.Round(bmi, 2);







            // Display result and
comments



            labelResult.Content = "BMI: " +
bmi;



            labelResult.Visibility =
Visibility.Visible;



            if (bmi < 18.5)



            {



               
labelComments.Content = "Underweight";



            }



            else if (bmi >= 18.5
&& bmi <= 24.9)



            {



               
labelComments.Content = "Healthy weight";



            }



            else if (bmi >= 25 && bmi <=
29.9)



            {



                labelComments.Content = "Overweight";



            }



            else if (bmi >= 30)



            {    



                labelComments.Content = "Obese";



            }



        labelComments.Visibility =
Visibility.Visible;



        }



  private void buttonClear_Click(object sender, RoutedEventArgs
e)


    {



        // Clear all values and results



        textBoxHeight.Text = "";



        textBoxWeight.Text = "";



        labelResult.Visibility =
Visibility.Hidden;



        labelComments.Visibility =
Visibility.Hidden;


    }







    private void comboBoxUnits_SelectionChanged(object sender,
SelectionChangedEventArgs e)


    {



        // Update units and labels based on combo box
selection



        units =
comboBoxUnits.SelectedItem.ToString();



        if (units == "Standard")


        {



            labelHeight.Content = "Height
(inches)";



            labelWeight.Content = "Weight
(pounds)";


        }



        else if (units == "Metric")


        {



            labelHeight.Content = "Height
(cm)";



            labelWeight.Content = "Weight
(kg)";


        }


    }


}


}






OUTPUT:





















No comments

Powered by Blogger.