Public Class frmRectangleArea Dim area As Decimal Dim numberOfRectangles As Inte
ID: 3593464 • Letter: P
Question
Public Class frmRectangleArea
Dim area As Decimal
Dim numberOfRectangles As Integer
Dim smallestRectangle As Decimal = 999999999
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim length As Decimal = CDec(txtLength.Text)
Dim width As Decimal = CDec(txtWidth.Text)
area = width * length
numberOfRectangles = numberOfRectangles + 1
smallestRectangle = Math.Min(smallestRectangle, area)
txtArea.Text = area.ToString
txtNumberOfRectangles.Text = numberOfRectangles.ToString
txtSmallestRectangle.Text = smallestRectangle.ToString
txtLength.Select()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
numberOfRectangles = 0
area = 0D
smallestRectangle = 999999999
txtLength.Text = ""
txtWidth.Text = ""
txtArea.Text = ""
txtNumberOfRectangles.Text = ""
txtSmallestRectangle.Text = ""
txtLength.Select()
End Sub
End Class
Question. In visual studio, analyzing this code, Comment on why SmallestRectangle is initialized with a large integer rather than 0 and Comment on how the min method works to return the smallest rectangle.
Explanation / Answer
Dim smallestRectangle As Decimal = 999999999
This smallest rectangle variable is initialize with big integer value because in this code to the find the minimum value using the math.min(val1,val2) it will always returns the value which entered by the user if less than 999999999 if user enter value is means area value is greater than 999999999 this value it will return the area value.
Suppose if we pass the value of the Math.Min(999999999,10) it will return the 10 value.
if Math.Min(999999999,10000000000) then it will return the this 10000000000 value.
Math.Min(val1,val2)
In the above syntax val1 and val2 are the two decimal values to find the smallest among them. so it will always return the small value between then.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.