3.3 the math object


Published on

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Slide Notes:You can use the Sin operation to get the sine of the specified angle in radians.You can use the ArcSin operation to get the angle in radians, given the sin value.You can use the GetDegrees operation to convert the value of an angle from radians to degrees.You can use the Cos operation to get the cosine of the specified angle in radians.Code:TextWindow.WriteLine("Enter the angle in degrees and get the sine of the angle:")number = TextWindow.Read()TextWindow.WriteLine("The sine of the angle is " + Math.Sin(number))TextWindow.WriteLine("The arcsine of the angle is " + Math.ArcSin(Math.Sin(number)))TextWindow.WriteLine("The angle in degrees is " + Math.GetDegrees(Math.ArcSin(Math.Sin(number))))TextWindow.WriteLine("Enter the angle in degrees and get the cosine of the angle:")number = TextWindow.Read()TextWindow.WriteLine("The cosine of the angle is " + Math.Cos(number))
  • SlideNotes: The Pi property of the Math object returns the value of pi, which is 3.14.Code:TextWindow.Write("Enter radius of a circle:")Radius = TextWindow.Read()Area = Math.Pi * Math.Power(Radius, 2)TextWindow.WriteLine("Area of the Circle is " + Area) 
  • Notes:You can use the Abs operation of the Math object to get the absolute value of a number. For example, if the given number is -50, the Abs operation will return the value as 50.Code:TextWindow.WriteLine("Enter two numbers for subtraction: ")Number1 = TextWindow.Read()Number2 = TextWindow.Read()Substraction = Number1 - Number2Textwindow.WriteLine("The answer is " + Math.Abs(substraction))
  • Code:TextWindow.Write("Enter the name of the student: ")Name = TextWindow.Read()TextWindow.WriteLine("Enter the student's marks in six subjects:")For I = 0 To 5 Subject[i] = TextWindow.Read() Total = Total + Subject[i]EndForPercentage = Total / 6TextWindow.WriteLine("Total Marks:" + Total)TextWindow.WriteLine("Percentage:" + Math.Floor(percentage))
  • Code:TextWindow.WriteLine("Enter number to get its log value: ")Number = TextWindow.Read()TextWindow.WriteLine("Log value of " + Number + " is: " + Math.Log(Number))
  • Code:GraphicsWindow.BackgroundColor = "Black"GraphicsWindow.Width = 600GraphicsWindow.Height = 500For i = 0 To 800GraphicsWindow.FontSize = Math.GetRandomNumber(30) x = Math.GetRandomNumber(GraphicsWindow.Width) y = Math.GetRandomNumber(GraphicsWindow.Height)GraphicsWindow.DrawText(x, y, "*")Program.Delay(10)EndFor
  • Code:TextWindow.WriteLine("Enter the first number:")Number1 = TextWindow.Read()TextWindow.WriteLine("Enter the second number:")Number2 = TextWindow.Read()min = Math.Min(Number1, Number2)If (Number1 = Number2) ThenTextWindow.WriteLine(“These numbers are the same")Else TextWindow.WriteLine("The smaller number is:" + min) EndIf
  • Code:TextWindow.Write("Enter a number to get its square root: ")Number = TextWindow.Read()TextWindow.WriteLine("Square root of the entered number is " + Math.SquareRoot(Number))
  • Code:start:TextWindow.Write("Enter a number to check if it is even or odd: ")number = TextWindow.Read()If Math.Remainder(number, 2) = 0 ThenTextWindow.WriteLine(number + " is an even number.")ElseTextWindow.WriteLine(number + " is an odd number.")EndIfGoto start
  • Solution 1:Rectangle = Shapes.AddRectangle(100, 100)Shapes.Move(Rectangle, 250, 150)For i = 0 To Math.GetRandomNumber(30) Program.Delay(500)Shapes.Rotate(Rectangle, i * 25) EndForSolution 2:start:GraphicsWindow.Width = 600GraphicsWindow.Height = 450TextWindow.Write("Enter a radius for the circle: ")Radius = TextWindow.Read()Area = Math.Pi * Radius * RadiusHeight = Area / 2Width = Area / 2x = Math.GetRandomNumber(GraphicsWindow.Width / 2)y = Math.GetRandomNumber(GraphicsWindow.Height / 2)GraphicsWindow.DrawEllipse(x, y, Height, Width)Goto start
  • 3.3 the math object

    1. 1. Microsoft® Small Basic<br />The Math Object<br />Estimated time to complete this lesson: 1 hour<br />
    2. 2. The Math Object<br />In this lesson, you will learn about:<br />Using different operations of the Math object.<br />Using different properties of the Math object.<br />
    3. 3. The Math Object<br />The Math object offers many mathematical functions that you can use in your programs.<br />It includes various operations and properties, such as:<br />Do complex mathematical calculations boggle your mind at times? Don’t worry! <br /><ul><li> Cos
    4. 4. GetRandomNumber
    5. 5. Sin
    6. 6. SquareRoot
    7. 7. Remainder
    8. 8. Pi
    9. 9. Abs
    10. 10. ArcSin
    11. 11. Floor
    12. 12. GetDegrees
    13. 13. Log
    14. 14. Min</li></li></ul><li>Operations of the Math Object<br />Let’s learn about some operations of the Math object by writing a simple program.<br />In this example, you use the Sin and the Cos operation of the Math object to get the sine and the cosine of the angle you entered. You can also use the ArcSin operation to get the angle in radians from the sine value. Next, you can use the GetDegrees operation to convert the angle from radians to degrees. <br />
    15. 15. The Pi Property<br />The value of pi is an important aspect of some mathematical calculations. You can use the Pi property of the Math object to retrieve the value of pi in your calculations.<br />Let’s use this property to calculate the area of the circle.<br />output<br />In this example, you use the Pi property of the Math object to retrieve the value of pi. Then you use this value in the formula to get the area of the circle. <br />
    16. 16. The Abs Operation<br />Abs is another useful operation provided by the Math object. Let’s check it out.<br />With the help of the Abs operation, you can get the absolute value of the given number. For example, if you subtract a large number from a smaller number, it will result in a negative numeral.<br />In this example, you subtract two numbers. If the first number is smaller than the second, Abs operation returns a positive number.<br />
    17. 17. The Floor Operation<br />While creating your Small Basic program, how can you get the integer value of a decimal number? <br />That’s what the Floor operation is created for. It gives an integer value which is less than or equal to the specified decimal number.<br />Let’s see how you can use this operation in a program to calculate a student’s average grade.<br />In this example, you enter the grades received by a student in six subjects. Then, you use the Floor operation to get the student’s average as an integer value.<br />
    18. 18. The Log Operation<br />While doing complex calculations, you often need the log value of a particular number.<br />The Math object in Small Basic offers the Log operation to get the logarithm (base 10) value of the specified number.<br />In this example, you use the Log operation to get the log of 22.3.<br />
    19. 19. The GetRandomNumber Operation<br />Now, let’s discuss the GetRandomNumber operation of the Math object. <br />You can use this operation to get a random number between 1 and the specified maximum number.<br />Let’s use this operation in a program.<br />In this program, you draw the ‘*’ shape on the graphics window in different sizes and at different locations.  <br />You first set the height, width, and background color of the graphics window. Then you use the GetRandomNumber operation to set the font size. The font size will be between 1 and 30 because you have specified 30 as the parameter for the GetRandomNumber operation.  <br />Next, you use this operation to set the x- and y-coordinates of the asterisks at random.<br />
    20. 20. The Min Operation<br />The Math object provides another operation called the Min operation. You can use this operation to compare two numbers and get the smaller number of the two.<br />Let’s apply this operation in a program.<br />In this example, you accept two numbers and use the Min operation to compare the two numbers and display the smaller number in the text window. You also ensure that if the entered numbers are equal, the text window displays the statement “These numbers are the same.”<br />
    21. 21. The SquareRoot Operation<br />Let’s check out the SquareRoot operation of the Math object. You can use this operation to get the square root of a specified number.<br />In this example, you enter a number and use the SquareRoot operation to get its square root.<br />
    22. 22. The Remainder Operation<br />Let’s look at the Remainder operation of the Math object. You can use this operation to get the remainder in a division problem.<br />In this program, you want to check whether a number is even or odd.<br />You use the If condition to check if the entered number is completely divisible by 2 (the remainder is 0). If so, the number is an even number; otherwise, it is an odd number. To check the remainder, you use the Remainder operation of the Math object.<br />
    23. 23. Let’s Summarize…<br />Congratulations! Now you know how to:<br />Use different properties of the Math object.<br />Use different operations of the Math object.<br />
    24. 24. It’s Time to Apply Your Learning…<br />Write a program to draw circles of different sizes in the graphics window. Set the size of the circle by using the area of the circle and randomize the x- and y- coordinates of the circle.<br />Using the GetRandomNumber operation, write a program to move and rotate a rectangle in a random manner.<br />