Attribute VB_Name = "Ch4" Sub ch4_ch2_11_1_ex() Dim n As Integer, i As Integer, sum As Double, numInput As Double On Error GoTo ErrorHandler n = InputBox("Enter n") On Error GoTo 0 sum = 0 For i = 1 To n On Error GoTo ErrorHandler numInput = InputBox("Enter number " + CStr(i)) On Error GoTo 0 sum = sum + numInput Next i MsgBox "The sum is " + CStr(sum) On Error GoTo ErrorHandler2 MsgBox "The mean is " + CStr(Round(sum / n, 2)) Exit Sub ErrorHandler: If Err = 13 Then 'Type mismatch. Notice the default property for Err is number. 'We can omit .number here. MsgBox "Your input is incorrect. Please input again." Else Err.Raise Err 'If not type mismatch, raise the error so that Macro Error box appears. End If Resume ErrorHandler2: If Err = 6 Or Err = 11 Then 'Overflow or division by zero MsgBox "The mean is 0" Else Err.Raise Err End If Resume Next End Sub Public Sub ch4_ch2_11_2_ex() Dim n As Integer, sum As Double, numInput As Double n = 0 sum = 0 Do n = n + 1 With ch2_11_2_inputBox .lblCount = n 'Update the count label With .txtNumberInput 'Update the selection and focus .SelStart = 0 .SelLength = Len(.Value) .setFocus End With .Show 'Show the input box If IsNumeric(.txtNumberInput) Then 'If Add button is clicked numInput = .txtNumberInput ElseIf .txtNumberInput = "Finish" Then n = n - 1 Exit Do Else Exit Sub End If End With sum = sum + numInput Loop Unload ch2_11_2_inputBox If n = 0 Then n = 1 MsgBox "The sum is " + CStr(sum) MsgBox "The mean is " + CStr(Round(sum / n, 2)) End Sub Public Sub ch4_ch3_11_ex() Dim aLine As Line, aQuadraticCurve As QuadraticCurve Dim aPoint As Point Dim aCurve As New Collection 'data entry Dim i As Integer, n As Integer On Error GoTo errorHandler1 n = InputBox("Please input the number of lines you wish to input:") For i = 1 To n aPoint.X = InputBox("Please enter Point X of Line " & i) aPoint.Y = InputBox("Please enter Point Y of Line " & i) Set aLine = New Line aLine.initialize aPoint, InputBox("Please enter Slope of Line " & i) aCurve.Add aLine Next i n = InputBox("Please input the number of quadratic curves you wish to input:") For i = 1 To n Set aQuadraticCurve = New QuadraticCurve aQuadraticCurve.initialize _ InputBox("Please input a of Quadratic Curve " & i), _ InputBox("Please input b of Quadratic Curve " & i), _ InputBox("Please input c of Quadratic Curve " & i) aCurve.Add aQuadraticCurve Next i Dim str As String For i = 1 To aCurve.Count str = "Equation of Curve " & i & ": " & aCurve(i).getEquation 'notice Polymorphism str = str & vbCrLf & "Y-intercept: " & aCurve(i).getYIntercept If TypeOf aCurve(i) Is QuadraticCurve Then 'notice the use of TypeOf...Is to check type Set aQuadraticCurve = aCurve(i) str = str & vbCrLf & "Extreme point: (" & aQuadraticCurve.getExtremePoint.X & _ ", " & aQuadraticCurve.getExtremePoint.Y & ")" End If MsgBox str Next i errorHandler1: If Err = 13 Then MsgBox "Error input! Enter again", vbExclamation Resume End If End Sub Public Sub ch4_ch3_11_ex2() Dim aLine As Line, aQuadraticCurve As QuadraticCurve Dim aPoint As Point Dim aCurve As New Collection 'data entry Do With ch3_10_inputBox .Show 'if finish input button is pressed If .txtA = "" And .txtB = "" And .txtC = "" And .txtPointX = "" And .txtPointY = "" And .txtSlope = "" Then Exit Do Else 'if the radio button of Line is selected If .rbnLine Then Set aLine = New Line aPoint.X = .txtPointX aPoint.Y = .txtPointY aLine.initialize aPoint, .txtSlope aCurve.Add aLine 'if the radio button of QuadraticCurve is selected ElseIf .rbnQuadraticCurve Then Set aQuadraticCurve = New QuadraticCurve aQuadraticCurve.initialize .txtA, .txtB, .txtC aCurve.Add aQuadraticCurve End If End If End With Loop Unload ch3_10_inputBox Dim i As Integer, str As String For i = 1 To 4 str = "Equation of Curve " & i & ": " & aCurve(i).getEquation 'notice Polymorphism str = str & vbCrLf & "Y-intercept: " & aCurve(i).getYIntercept If TypeOf aCurve(i) Is QuadraticCurve Then 'notice the use of TypeOf...Is to check type Set aQuadraticCurve = aCurve(i) str = str & vbCrLf & "Extreme point: (" & aQuadraticCurve.getExtremePoint.X & _ ", " & aQuadraticCurve.getExtremePoint.Y & ")" End If MsgBox str Next i End Sub