In: Computer Science
Show: Create an application 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
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 calories, 30 fat grams Percentage of calories from fat: 54%
Public Class Form1
Dim calorie As Integer
Dim fatInGram As Integer
Dim percentage As Double
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
lblOutput.Text = ""
'Validate the input
If ValidateInput() Then
'calculate percentage
percentage = (fatInGram * 9) / calorie
'display in text box as %
txtPercentage.Text = percentage.ToString("0.00%")
'display the label in case of low fat food
If percentage < 0.3 Then
lblOutput.Text = "a low-fat food"
End If
End If
End Sub
Private Function ValidateInput()
'if not able to parse in integer and calorie is less than 0 display error message
If Not Integer.TryParse(txtCalorie.Text, calorie) OrElse calorie <= 0 Then
MessageBox.Show("Invalid Calorie", "Fat Percentage Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
'if not able to parse in integer and fat in Gram is less than 0 display error message
If Not Integer.TryParse(txtgram.Text, fatInGram) OrElse fatInGram <= 0 Then
MessageBox.Show("Invalid fat in gram", "Fat Percentage Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
'if fat in gram * 9 is greater than calories then display error message
If fatInGram * 9 > calorie Then
MessageBox.Show("either the calories or fat grams were incorrectly entered", "Fat Percentage Calculator", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
Return True
End Function
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtPercentage.Clear()
txtCalorie.Clear()
txtgram.Clear()
lblOutput.Text = ""
txtCalorie.Focus()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
=========form1.designer.vb============
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.txtCalorie = New System.Windows.Forms.TextBox()
Me.txtgram = New System.Windows.Forms.TextBox()
Me.txtPercentage = New System.Windows.Forms.TextBox()
Me.btnCalculate = New System.Windows.Forms.Button()
Me.btnClear = New System.Windows.Forms.Button()
Me.btnExit = New System.Windows.Forms.Button()
Me.lblOutput = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(63, 55)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(292, 20)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Enter the number of calories in the food:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(50, 108)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(305, 20)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Enter the number of fat grams in the food:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(50, 167)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(305, 20)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Percentage of calories that come from fat:"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'txtCalorie
'
Me.txtCalorie.Location = New System.Drawing.Point(361, 52)
Me.txtCalorie.Name = "txtCalorie"
Me.txtCalorie.Size = New System.Drawing.Size(188, 26)
Me.txtCalorie.TabIndex = 3
'
'txtgram
'
Me.txtgram.Location = New System.Drawing.Point(361, 102)
Me.txtgram.Name = "txtgram"
Me.txtgram.Size = New System.Drawing.Size(188, 26)
Me.txtgram.TabIndex = 4
'
'txtPercentage
'
Me.txtPercentage.Enabled = False
Me.txtPercentage.Location = New System.Drawing.Point(361, 164)
Me.txtPercentage.Name = "txtPercentage"
Me.txtPercentage.Size = New System.Drawing.Size(188, 26)
Me.txtPercentage.TabIndex = 5
'
'btnCalculate
'
Me.btnCalculate.Location = New System.Drawing.Point(68, 276)
Me.btnCalculate.Name = "btnCalculate"
Me.btnCalculate.Size = New System.Drawing.Size(172, 44)
Me.btnCalculate.TabIndex = 6
Me.btnCalculate.Text = "Calculate"
Me.btnCalculate.UseVisualStyleBackColor = True
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(304, 276)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(172, 44)
Me.btnClear.TabIndex = 7
Me.btnClear.Text = "Clear"
Me.btnClear.UseVisualStyleBackColor = True
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(543, 276)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(172, 44)
Me.btnExit.TabIndex = 8
Me.btnExit.Text = "Exit"
Me.btnExit.UseVisualStyleBackColor = True
'
'lblOutput
'
Me.lblOutput.AutoSize = True
Me.lblOutput.Location = New System.Drawing.Point(279, 230)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.Size = New System.Drawing.Size(0, 20)
Me.lblOutput.TabIndex = 9
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(9.0!, 20.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.lblOutput)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnCalculate)
Me.Controls.Add(Me.txtPercentage)
Me.Controls.Add(Me.txtgram)
Me.Controls.Add(Me.txtCalorie)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Fat Percentage Calculator"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As Label
Friend WithEvents Label2 As Label
Friend WithEvents Label3 As Label
Friend WithEvents txtCalorie As TextBox
Friend WithEvents txtgram As TextBox
Friend WithEvents txtPercentage As TextBox
Friend WithEvents btnCalculate As Button
Friend WithEvents btnClear As Button
Friend WithEvents btnExit As Button
Friend WithEvents lblOutput As Label
End Class