Programming Assignment
Practice Concepts
●Registering events
●Protecting inputs through logic
Problem Description
For PA1, you are to create a Windows Forms Application using the .NET Framework. Name this
project
LastName_FirstName_PA1
. In this application you will create a tip calculator that takes two
inputs from the user: A Meal Cost value (double), and a Tip Percentage amount (double). The
application should do the following:
1.)
The form text should display the name of your app (i.e. Tip Calculator)
2.)
The form should offer two inputs: one to enter a meal cost value and one to enter a tip
percentage amount. Make sure all inputs are labeled clearly.
3.)
Both inputs should be protected against invalid values (i.e. non-text values, or negative values)
4.)
The form should have a button that initiates the calculation and output of the tip amount. The
tip can be calculated by using the formula: tip = meal cost * percentage in decimal form.
5.)
If the entered tip percentage is less than 1, assume the percentage is already in decimal form.
For example, if the user enters 0.5, this represents 50%.
6.)
If the entered tip percentage is greater than or equal to 1, assume it needs to be converted to
decimal form prior to tip calculation (percentage decimal form = percentage / 100)
Notes: When protecting your inputs, you may use many different ways to inform the user of an invalid
input. You may use a MessageBox, change the color of the form, use additional labels as warnings or
informational outputs, etc. Use your creativity!
Test Conditions
●
Your program should be free of syntax errors and build and compile properly.
●
Your program successfully calculates the tip if percentage is entered as decimal form or as
regular percentage form.
●
Your program informs the user if an invalid input condition exist
Code
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs)
Handles btnCalculate.Click
Dim cost, per As Decimal
txtTip.Clear()
If (txtCost.Text <> "" And txtTipPer.Text <> "")
Then
cost = Convert.ToDecimal(txtCost.Text)
per = Convert.ToDecimal(txtTipPer.Text)
If (cost > 0 And per > 0) Then
txtCost.BackColor = Color.White
txtTipPer.BackColor = Color.White
If (per < 1) Then
txtTip.Text = (cost * per).ToString("C")
Else
txtTip.Text = (cost * per / 100).ToString("C")
End If
Else
If (cost < 0) Then
txtCost.BackColor = Color.Red
End If
If (per < 0) Then
txtTipPer.BackColor = Color.Red
End If
MessageBox.Show("Value can't be negative.")
End If
Else
If (txtCost.Text = "") Then
txtCost.BackColor = Color.Red
End If
If (txtTipPer.Text = "") Then
txtTipPer.BackColor = Color.Red
End If
MessageBox.Show("Value can't be balcnk.")
End If
End Sub
End Class
Form Design
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.