Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a C# program that allows the user to enter the number of calories and fat grams in a food. The application should display the

Create a C# program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat.
One gram of fat has 9 calories, so: Calories from fat = fat grams *9
The percentage of calories from fat can be calculated as:
Percentage of calories from fat = Calories from fat / total calories
Input validation: Make sure the number of calories are not less than 0. Also, the number of calories from fat cannot be greater than the total number of calories. If that happens, display an error message indicating that either the calories or fat grams were incorrectly entered.
Use the following test data to determine if the application is calculating properly:
Calories and Fat Percentage Fat
200 calories, 8 fat grams Percentage of calories from fat: 36%150 calories 2 fat grams Percentage of calories from fat: 12%(a low-fat food)
500 lories, 30 fat grams Percentage of calories from fat: 54%
I know my math is off the test didn't show 36% it gave me an insane number instead - I also need a checkbox tp put in whether the food item is a high or low food item
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}//End initialization
private void calcButton_Click(object sender, EventArgs e)
{
try
{
//Calories from fat = fat grams *9
//number range 0 to 5000
//Percentage of calories from fat = calories from fat / total calories
//Number range 0 to 300
//Get variables
int calories;
int fatGrams;
//Declare user input
calories = int.Parse(caloriesBox.Text);
fatGrams = int.Parse(fatGramsBox.Text);
//Check calories range and fat grams range
if
(calories >=0 && calories <=5000 && fatGrams >=0 && fatGrams <=300)
{
//Calculate the calories from fat
int caloriesFromFat = fatGrams *9;//1 gram of fat =9 calories
// Validate that calories from fat is not greater than total calories
if (caloriesFromFat <= calories)
{
//Calculate the percentage of calories from fat
double percentageOfCaloriesFromFat =(double)caloriesFromFat / calories *100;
// Display the result
messageLabel.Text = caloriesFromFat.ToString()+ percentageOfCaloriesFromFat.ToString("n2")+"%";
}
else
{
MessageBox.Show("Invalid values. Calories from fat cannot be greater than total calories.");
}
}
else
{
MessageBox.Show("Invalid values. Calories should be between 0 and 5000. Fat grams should be between 0 and 300.");
}
}catch (Exception ex)
{
// Display error message
MessageBox.Show(ex.Message);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
// clear textboxes and reset focus
caloriesBox.Text ="";
fatGramsBox.Text ="";
caloriesBox.Focus();
}
private void exitMenu_Click(object sender, EventArgs e)
{
//Close application
this.Close();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

Analyze the impact of labor unions on health care.

Answered: 1 week ago

Question

Assess three motivational theories as they apply to health care.

Answered: 1 week ago

Question

Discuss the history of U.S. labor unions.

Answered: 1 week ago