Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.

Similar presentations


Presentation on theme: "COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions."— Presentation transcript:

1 COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions

2 Objective/Essential Standard Essential Standard 7.00 Apply Advanced Logic Indicator 7.03 Apply Built-in Math Class Functions (3%)

3 Math Class Functions Visual Studio provides the programmer with multiple built-in functions from the Math class, including the following: Math Class Functions  Absolute Value (Abs)  Square Root (Sqrt)  Sign (Sign)  Rounding (Round) The Math class is not limited to just these four functions.

4 Math Class Functions The Math class provides programmers with numerous functions. We will only look at a few of these functions. Abs(num)  Returns the absolute value of a positive or negative number Sqrt(num)  Returns the square root of a number Sign(num)  Returns the sign of a number Round (num, places)  Returns number nearest to the specified value.

5 Using Abs(num) The Abs() function works with the following data types:  Decimal, Double, Int16, Int32, Int64, SByte, Single Abs() FunctionintNum Value after execution intNum = Math.Abs(4)4 sngNum = Math.Abs(-4.123)4.123 intNum = Math.Abs(0)0

6 Using Sqrt(num) Returns the square root of a number as a double Sqrt() FunctiondblNum Value after execution dblNum = Math.Sqrt(4)2.0 dblNum = Math.Sqrt(0)0.0 dblNum = Math.Sqrt(-4)NaN (NaN represents a value that is not a number)

7 Using Sign(num) The Sign() function works with the following data types:  Decimal, Double, Int16, Int32, Int64, SByte, Single Sign() FunctionintNum Value after execution intNum = Math.Sign(4)1 intNum = Math.Sign(-4) intNum = Math.Sign(0)0

8 Round Function The Round() function works with either a decimal or a double. The variable/value that represents the number of places should be an Integer. Round() FunctiondblNum Value after execution dblNum = Math.Round(5.23651, 2)5.24 decNum = Math.Round(5.23651)5.0 dblNum = Math.Round(5.5)6.0 decNum = Math.Round(5.225, 2)5.23 Note that.5 does round up here.

9 Try It! Create a new application called mathExample  Save it into the location as instructed by your teacher. Add the following controls. When the button is clicked, the appropriate answer should be displayed in the lblAnswer label.  When the btnRound is clicked, it should display an input box to ask for the number of decimal places. ControlNameText/Items LabellblPromptEnter a Number: LabellblAnswer ButtonbtnAbsAbsolute Value ButtonbtnSqrtSquare Root ButtonbtnSignSign ButtonbtnRoundRound It

10 Try It! Solution Private Sub btnAbs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbs.Click Dim dblNum As Double Dim input As String Try input = txtInput.Text dblNum = Convert.ToDouble(input) lblAnswer.Text = "The Absolute Value is " & Math.Abs(dblNum) Catch Ex As Exception MessageBox.Show(“Enter numeric values”) End Try End Sub

11 Try It! Solution Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click Dim dblNum As Double Dim input As String Try input = txtInput.Text dblNum = Convert.ToDouble(input) lblAnswer.Text = "The Square Root Value is " & Math.Sqrt(dblNum) Catch Ex As Exception MessageBox.Show(“Enter numeric values”) End Try End Sub

12 Try It! Solution Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click Dim dblNum As Double Dim input As String Try input = txtInput.Text dblNum = Convert.ToDouble(input) If (Math.Sign(dblNum) > 0) Then lblAnswer.Text = dblNum & " is a positive number" ElseIf (Math.Sign(dblNum) < 0) Then lblAnswer.Text = dblNum & " is a negative number." Else lblAnswer.Text = dblNum & " is a zero." End If Catch Ex As Exception MessageBox.Show(“Enter numeric values”) End Try End Sub

13 Try It! Solution Private Sub btnRound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRound.Click Dim dblNum As Double Dim input As String Dim intPlaces As Integer Try input = txtInput.Text dblNum = Convert.ToDouble(input) intPlaces = Convert.ToInt16(InputBox("Enter the number of places", "Places to round")) lblAnswer.Text = "The Absolute Value is " & Math.Round(dblNum, places) Catch Ex As Exception MessageBox.Show(“Enter numeric values”) End Try End Sub

14 Conclusion This PowerPoint provided an overview of several methods in the Math class and the formatting method in the String class in Visual Studio. For more information on this topic  http://msdn.microsoft.com/en- us/library/32s6akha(v=VS.90).aspx http://msdn.microsoft.com/en- us/library/32s6akha(v=VS.90).aspx  http://msdn.microsoft.com/en-us/library/system.math.aspx http://msdn.microsoft.com/en-us/library/system.math.aspx


Download ppt "COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions."

Similar presentations


Ads by Google