Q1. Rewrite the following code snippet using a "Select Case" statement, instead of the (given) "If… then…elseif" statement If intQuantity = 1 Or intQuantity = 2 Then fDiscount = 0 ElseIf intQuantity >= 3 And intQuantity <= 9 Then fDiscount = 0.1 ElseIf intQuantity >= 10 And intQuantity <= 24 Then fDiscount = 0.2 ElseIf intQuantity >= 25 fDiscount = 0.3 Else MsgBox " Invalid Quantity." End If
Q2. Rewrite the following code snippet using (a) "For…Next" and (b) "While … Wend", instead of the (given) "Do While" loop sum = 0 count = 1 Do While count <= 10 sum = sum + count count = count +1 Loop sum = 0 For i=1 to 10 Sum= sum +1 Next i sum = 0 count = 1 While count <= 10 sum = sum + count count = count +1 Wend
Project A: Simple Calculator (Prob. 4 on page 39)
Project B: Weekly Pay Calculator (Prob. 3 on page 51)
Operation & Specifications
Operation & Specifications
• Build a simple calculator as shown in the fig. below:
• Write a program that calculates total weekly pay as shown in figure.
• The user enters two numbers and then clicks one of the four arithmetic operators to display the result. The whole calculation is also displayed in the list box. Clicking the clear button allows all this to be repeated.
• The user enters the numbers of hours worked and selects the hourly rate of pay from a list box. If overtime has been done, the number of hours is also entered. Overtime hours are paid at double rate. A check box handles overtime. Clicking this should make visible the textbox for inputting the number of overtime hours.
Dim FirstNumber As Single Dim SecondNumber As Single Dim Result As Single
Private Sub chkOvertime_Click() lblHoursOvertime.Visible = True txtHoursOvertime.Visible = True End Sub
Private Sub cmdAdd_Click() FirstNumber = txtFirstNumber.Text SecondNumber = txtSecondNumber.Text Result = FirstNumber + SecondNumber lblResult.Caption = Result lstCalculations.AddItem FirstNumber & " + " & SecondNumber & " = " & Result End Sub
Private Sub cmdCalculatePay_Click() Dim HoursWorked As Integer Dim OvertimeHoursWorked As Integer Dim HourlyRate As Single Dim OvertimePay As Single Dim TotalPay As Single
Private Sub cmdClear_Click() txtFirstNumber.Text = "" txtFirstNumber.SetFocus txtSecondNumber.Text = "" lblResult.Caption = "" 'if you wish to clear the list box too then add next line lstCalculations.Clear End Sub
HoursWorked = txtHoursWorked.Text HourlyRate = lstPayRates.Text If chkOvertime.Value = 1 Then 'overtime check box selected OvertimeHoursWorked = txtHoursOvertime.Text OvertimePay = OvertimeHoursWorked * (2 * HourlyRate) TotalPay = HoursWorked * HourlyRate + OvertimePay Else TotalPay = HoursWorked * HourlyRate End If txtPay.Text = TotalPay End Sub
Private Sub cmdDivide_Click() FirstNumber = txtFirstNumber.Text SecondNumber = txtSecondNumber.Text Result = Format(FirstNumber / SecondNumber, "0.00") 'output to 2 decimal places lblResult.Caption = Result lstCalculations.AddItem FirstNumber & " / " & SecondNumber & " = " & Result End Sub Private Sub cmdMultiply_Click() FirstNumber = txtFirstNumber.Text SecondNumber = txtSecondNumber.Text Result = FirstNumber * SecondNumber lblResult.Caption = Result lstCalculations.AddItem FirstNumber & " x " & SecondNumber & " = " & Result End Sub Private Sub cmdSubtract_Click() FirstNumber = txtFirstNumber.Text SecondNumber = txtSecondNumber.Text Result = FirstNumber - SecondNumber lblResult.Caption = Result lstCalculations.AddItem FirstNumber & " - " & SecondNumber & " = " & Result End Sub Private Sub cmdQuit_Click() Unload Form1 End Sub