Math Class
Math Class
By now, you are familiar with creating classes, creating methods inside those classes, declaring objects
of that class in the main method and calling methods of that class
C# comes with some predefined useful classes that can be used when needed. Math class is one such
class. You need to do the following
2) List 5 methods of the class, what do they do and if they take any parameters (how many along with
the data types of the parameters), does it return any value (along with the purpose of the return value
and data type). Complete the table below
Method Name Purpose of the Does the method What is the data Does the method What is the data
method take any type of each return a value type of the
parameters parameter and (Yes/No) return value and
(Yes/No) purpose of each what is the
parameter purpose of the
return value
1)Math.Sqrt(Double) Returns the Yes Double – used for Yes Double – To
square root of the number you provide the
a specified want to calculate square rooted
number the square root of value
2) Math.Round Rounds a Yes Double – Used for Yes Returns a value
value to the the number you of the same type
nearest want to round. as the input
integer or to parameter
the specified
number of
fractional
digits.
3) Math.Floor Returns the No Float – To specify Yes Double – To
largest the number you represent the
integral value want to find the largest integral
less than or largest integer to to value less than or
equal to the equal to it. equal to the
specified specified number
number.
4) Math.DivRem Calculate the Yes Long – Used for the Yes N/A returns the
quotient of number you want to remainder in an
two numbers divide. out parameter.
and returns
the remainder
in an output
parameter
Example: Let’s say there is a BankAcount class with two methods – Get Balance and DepositAmount
Method Name Purpose of the Does the method take What is the data Does the method What is the data
method parameters (Yes/No) type of each return a value type of the
parameter and (Yes/No) return value and
purpose of each what is the
parameter purpose of
return value
1) GetBalance Returns the No Not Applicable(N/A) Yes float
bank balance returns the
balance in the
bank
2) Adds amount to Yes float – for the No Not Applicable
DepositAmount the bank amount to be (N/A)
account deposited
3)
4)
3) Step 2 must be completed prior to starting step 3. For step 3, use Math class repl to demonstrate the
use of the methods. One example per method. Do not copy code off the internet. It needs to be your
code. This step will be done in repl.it