Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10.13 ( Arithmetic Calculator App) Write an app that allows the user to enter a

ID: 3549130 • Letter: 1

Question

10.13 ( Arithmetic Calculator App) Write an app that allows the user to enter a series of numbers and manipulate them. The app should provide users with the option of adding or multiplying the numbers. Users should enter each number in a TextBox. After entering the number, the user clicks a Button, and the number is inserted in a ListBox. The GUI should behave as in Fig. 10.20.

a) Copying the template to your working directory. Copy the directory C: Examples ch10 Exercises ArithmeticCalculator to your C: SimplyVB2010 directory.

b) Opening the apps template file. Double click ArithmeticCalculator. sln in the ArithmeticCalculator directory to open the app.

c) Adding a ListBox to display the entered numbers. Add a ListBox. Place and size it as in Fig. 10.20.

d) Creating an event handler for the Enter Button. Create the Click event handler for the Enter Button. If the result of a previous calculation is displayed, this event han-dler should clear the result, clear the ListBox and disable the addition and multipli-cation Buttons. It should then insert the current number in the Operands list: ListBox. When the ListBox contains at least two numbers, the event handler should then enable the addition and multiplication Buttons.

e) Summing the values in the ListBox. Define the Click event handler for the Add Button. This event handler should compute the sum of all the values in the Oper-ands list: ListBox and display the result in resultLabel.

f) Multiplying the values in the ListBox. Define the Click event handler for the Mul-tiply Button. This event handler should compute the product of all the values in the Operands list: ListBox and display the result in resultLabel.

g) Running the app. Select Debug > Start Debugging to run your app. Enter two val-ues, then click the Add and Multiply Buttons. Verify that the results displayed are correct. Also, make sure that the Add and Multiply Buttons are not enabled until two values have been entered. Enter a new value and verify that the previous result and the ListBox are cleared. Enter two more values, then click the Add and Multiply Buttons. Verify that the results displayed are correct.

h) Closing the app. Close your running app by clicking its close box.

i) Closing the IDE. Close the Visual Basic IDE by clicking its close box.

Explanation / Answer

vb.net Code:
  1. Public Class ArithmeticCalculator
  2. Private Property operands As Integer
  3. Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
  4. Dim operand As Integer
  5. If enterOperandTextBox.Text <> String.Empty Then
  6. OperandsListBox1.Items.Add(enterOperandTextBox.Text)
  7. OperandsListBox1.ClearSelected()
  8. enterOperandTextBox.Clear()
  9. End If
  10. operand = 0
  11. If OperandsListBox1.Items.Count = 0 Then
  12. enterButton.Enabled = True
  13. addButton.Enabled = False
  14. multiplyButton.Enabled = False
  15. End If
  16. End Sub
  17. Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
  18. Dim result As Integer
  19. Dim operandCounter As Integer
  20. result = 0
  21. operandCounter = 0
  22. Do While operandCounter < OperandsListBox1.Items.Count
  23. result = OperandsListBox1.Items(operands)
  24. result += operands
  25. operandCounter += 1
  26. Loop
  27. If operandCounter <> 0 Then result = result + OperandsListBox1.Items.Count
  28. resultLabel.Text = result
  29. End Sub
  30. Private Sub multiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplyButton.Click
  31. Dim result As Integer
  32. Dim operandCounter As Integer
  33. result = 0
  34. operandCounter = 0
  35. Do While operandCounter < OperandsListBox1.Items.Count
  36. result = OperandsListBox1.Items(operands)
  37. result += operands
  38. operandCounter += 1
  39. Loop
  40. If operandCounter <> 0 Then result = result * OperandsListBox1.Items.Count
  41. resultLabel.Text = result
  42. End Sub
  43. End Class
  1. Public Class ArithmeticCalculator
  2. Private Property operands As Integer
  3. Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
  4. Dim operand As Integer
  5. If enterOperandTextBox.Text <> String.Empty Then
  6. OperandsListBox1.Items.Add(enterOperandTextBox.Text)
  7. OperandsListBox1.ClearSelected()
  8. enterOperandTextBox.Clear()
  9. End If
  10. operand = 0
  11. If OperandsListBox1.Items.Count = 0 Then
  12. enterButton.Enabled = True
  13. addButton.Enabled = False
  14. multiplyButton.Enabled = False
  15. End If
  16. End Sub
  17. Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
  18. Dim result As Integer
  19. Dim operandCounter As Integer
  20. result = 0
  21. operandCounter = 0
  22. Do While operandCounter < OperandsListBox1.Items.Count
  23. result = OperandsListBox1.Items(operands)
  24. result += operands
  25. operandCounter += 1
  26. Loop
  27. If operandCounter <> 0 Then result = result + OperandsListBox1.Items.Count
  28. resultLabel.Text = result
  29. End Sub
  30. Private Sub multiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplyButton.Click
  31. Dim result As Integer
  32. Dim operandCounter As Integer
  33. result = 0
  34. operandCounter = 0
  35. Do While operandCounter < OperandsListBox1.Items.Count
  36. result = OperandsListBox1.Items(operands)
  37. result += operands
  38. operandCounter += 1
  39. Loop
  40. If operandCounter <> 0 Then result = result * OperandsListBox1.Items.Count
  41. resultLabel.Text = result
  42. End Sub
  43. End Class
Public Class ArithmeticCalculator Private Property operands As Integer Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click Dim operand As Integer If enterOperandTextBox.Text <> String.Empty Then OperandsListBox1.Items.Add(enterOperandTextBox.Text) OperandsListBox1.ClearSelected() enterOperandTextBox.Clear() End If operand = 0 If OperandsListBox1.Items.Count = 0 Then enterButton.Enabled = True addButton.Enabled = False multiplyButton.Enabled = False End If End Sub Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click Dim result As Integer Dim operandCounter As Integer result = 0 operandCounter = 0 Do While operandCounter < OperandsListBox1.Items.Count result = OperandsListBox1.Items(operands) result += operands operandCounter += 1 Loop If operandCounter <> 0 Then result = result + OperandsListBox1.Items.Count resultLabel.Text = result End Sub Private Sub multiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplyButton.Click Dim result As Integer Dim operandCounter As Integer result = 0 operandCounter = 0 Do While operandCounter < OperandsListBox1.Items.Count result = OperandsListBox1.Items(operands) result += operands operandCounter += 1 Loop If operandCounter <> 0 Then result = result * OperandsListBox1.Items.Count resultLabel.Text = result End Sub End Class
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote