Question

In: Computer Science

Show: Create an application that allows the user to enter the number of calories and fat...

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%


VBA

Solutions

Expert Solution

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


Related Solutions

Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator 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 *...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
Write an application that allows a user to enter any number of student quiz scores until...
Write an application that allows a user to enter any number of student quiz scores until the user enters 99. If the score entered is less than 0 or more than 10, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered, the highest score, the lowest score, and the arithmetic average. Save the file as QuizScoreStatistics.java. I am struggling with looping in Java and do not...
Program on Visual Basic, VBA Create an application that lets the user enter a number of...
Program on Visual Basic, VBA Create an application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT