Kotlin Class: MathUtils class with static functions
Write a Kotlin program that creates a class 'MathUtils' with static functions to calculate the factorial, square root, and cube root of a number.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Companion Objects: In Kotlin, companion objects allow defining functions that behave like static methods in Java.
- Factorial Calculation: The factorial of a number (n!) is calculated as the product of all positive integers from 1 to n.
- Math Functions: Using built-in functions like Math.sqrt() and Math.cbrt() to calculate the square root and cube root of a number.
- Loops in Kotlin: The for loop is used to compute the factorial by multiplying numbers in a range.
- Function Calls: Understanding how to call functions within the main() function and pass arguments.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define a class named MathUtils and use a companion object to create a static function for factorial calculation.
- Hint 2: Implement a loop inside the factorial() function to compute the factorial of a given integer.
- Hint 3: Define separate functions for square root and cube root calculations using built-in math functions.
- Hint 4: In the main() function, create a variable for the input number and call the three functions to compute and print their results.
Sample Solution:
Kotlin Code:
class MathUtils {
companion object {
fun factorial(n: Int): Long {
if (n == 0 || n == 1) {
return 1
}
var result = 1L
for (i in 2..n) {
result *= i
}
return result
}
}
}
fun sqrt(n: Double): Double {
return Math.sqrt(n)
}
fun cbrt(n: Double): Double {
return Math.cbrt(n)
}
fun main() {
val number = 7
val factorial = MathUtils.factorial(number)
println("Factorial of $number is: $factorial")
val squareRoot = sqrt(number.toDouble())
println("Square root of $number is: $squareRoot")
val cubeRoot = cbrt(number.toDouble())
println("Cube root of $number is: $cubeRoot")
}
Sample Output:
Factorial of 7 is: 5040 Square root of 7 is: 2.6457513110645907 Cube root of 7 is: 1.9129311827723892
Explanation:
In the above exercise -
- First we define a class "MathUtils" and declare a "companion" object inside it. The companion object contains static functions. Inside the companion object, we define a function "factorial()" to calculate the factorial of a number using an iterative approach.
- We also define top-level functions "sqrt()" and "cbrt()" outside the "MathUtils" class. These functions use the Math.sqrt and Math.cbrt functions from the standard library to calculate the square root and cube root of a number, respectively.
- In the "main()" function, we calculate the factorial of a number using the MathUtils.factorial function. We also calculate the square root and cube root using the sqrt and cbrt top-level functions, respectively.
Kotlin Editor:
Previous: Create Customer class and send welcome email.
Next: Shape, circle, and rectangle classes with area calculation.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics