4️⃣

10.4 The Math Class

The Math Class

As you have already learned, Java has built-in arithmetic operators for basic math. But what if you need to raise a number to the power of another number? What if you want a good approximation of pi? The Math class is just what you need. Below is an example program with commonly used methods and constants that are within the Math class (you do not need to import it).

System.out.println(Math.PI); // approximation of pi
System.out.println(Math.E); // approximation of e

System.out.println(Math.max(1, 5)); // maximum of 2 numbers
System.out.println(Math.min(1, 5)); // minimum of 2 numbers

System.out.println(Math.pow(2, 5)); // exponents
System.out.println(Math.exp(1)); // e ^ some number
System.out.println(Math.log(Math.E)); // natural log (ln)
System.out.println(Math.log10(100)); // log base 10

System.out.println(Math.sqrt(25)); // square root

System.out.println(Math.toRadians(60)); // convert from deg to rad
System.out.println(Math.toDegrees(2 * Math.PI)); // convert from rad to deg

System.out.println(Math.sin(Math.toRadians(60))); // sine (argument must be in radians!)
System.out.println(Math.cos(Math.toRadians(60))); // cosine (radians!)
System.out.println(Math.tan(Math.toRadians(60))); // tangent (radians!)

System.out.println(Math.asin(Math.toRadians(60))); // arc/inverse sine (argument must be in radians!)
System.out.println(Math.acos(Math.toRadians(60))); // arc/inverse cosine (radians!)
System.out.println(Math.atan(Math.toRadians(60))); // arc/inverse tangent (radians!)

System.out.println(Math.abs(-5)); // absolute value

System.out.println(Math.floor(5.7)); // round down to nearest integer
System.out.println(Math.ceil(3.3)); // round up to nearest integer
System.out.println(Math.round(3.5)); // round normally to nearest unit

Practice

Interest

Write a program called Interest, which calculates the final amount in an account which earns interest that is compounded continuously. (Don’t worry about what this means, just use this formula to calculate it.)

In the main method, prompt the user to enter the principal, annual interest rate (as a decimal), and period of time (in years). Then, print the final amount based on those values.

Use String formatting (you can use String.format() or System.out.printf() for this) to make sure that only 2 decimals are displayed.

Bonus points if you can also use formatting to include commas for large numbers (for example, try to display $1,000.00 instead of $1000.00).

Random Numbers

Fill a 4 x 6 integer array with 24 random values from 7 to 77. Use the randInt(min, max) method that is given to you to do this. To call the method, type randInt(7, 77) and assign it to an array element. For example:

list[0][0] = randInt(7, 77);Print the values in 4 rows of 6 elements. Keep track of the sum of all the values in the array. Display the sum on its own line under the array values. Keep track of the maximum and minimum value in the array. Display the max and min below the sum.Hint: You will need to set the max to Integer.MIN_VALUE and min to Integer.MAX_VALUE when you first initialize them. This ensures that when you iterate through the randomly generated numbers in your array, the max and min are updated correctly. (You can also set max to 6 and min to 78.)

💡

Note: Interested in seeing how the randInt() method works? You can read this Stack Overflow response.

Power

Fill a 5 x 4 integer array according to the following pattern:

5 x 4

TitleCol 1Col 2Col 3Col 4
Row 1
4
9
16
25
Row 2
8
27
64
125
Row 3
16
81
56
625
Row 4
32
243
1024
3125
Row 5
64
729
4096
15625

Print the values in 5 rows of 4 elements. Use nested loops to fill the values into the array, rather than just declaring the values initially.

Previous Section

Next Section

Add a page link block here (if there is no next section, delete this column)

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.