/** * >>>>>>>>>>>>>>>>>> COPY TO BLUEJ AND RUN!!!! <<<<<<<<<<<<<<<<<< * MAKE SURE YOU UNDERSTAND WHAT IS GOING ON!!!!! * Examples of using the Math methods */ public class MathLibraryExample { public static void main(String[] args) { int i = 7; int j = -9; double x = -72.3; double y = 0.34; // The absolute value of a number is equal to // the number if the number is positive or // zero and equal to the negative of the number // if the number is negative. System.out.println("Math.abs(" + i + ") is " + Math.abs(i)); System.out.println("Math.abs(" + j + ") is " + Math.abs(j)); System.out.println("Math.abs(" + x + ") is " + Math.abs(x)); System.out.println("Math.abs(" + y + ") is " + Math.abs(y)); System.out.println(""); // You can round off a floating point number // to the nearest integer with round() System.out.println("Math.round(" + x + ") is " + Math.round(x)); System.out.println("Math.round(" + y + ") is " + Math.round(y)); System.out.println(""); // The "ceiling" of a number is the // smallest integer greater than or equal to // the number. Every integer is its own // ceiling. System.out.println("Math.ceil(" + i + ") is " + Math.ceil(i)); System.out.println("Math.ceil(" + j + ") is " + Math.ceil(j)); System.out.println("Math.ceil(" + x + ") is " + Math.ceil(x)); System.out.println("Math.ceil(" + y + ") is " + Math.ceil(y)); System.out.println(""); // The "floor" of a number is the largest // integer less than or equal to the number. // Every integer is its own floor. System.out.println("Math.floor(" + i + ") is " + Math.floor(i)); System.out.println("Math.floor(" + j + ") is " + Math.floor(j)); System.out.println("Math.floor(" + x + ") is " + Math.floor(x)); System.out.println("Math.floor(" + y + ") is " + Math.floor(y)); System.out.println(""); // min() returns the smaller of the two arguments you pass it System.out.println("Math.min(" + i + "," + j + ") is " + Math.min(i,j)); System.out.println("Math.min(" + x + "," + y + ") is " + Math.min(x,y)); System.out.println("Math.min(" + i + "," + x + ") is " + Math.min(i,x)); System.out.println("Math.min(" + y + "," + j + ") is " + Math.min(y,j)); System.out.println(""); // There's a corresponding max() method // that returns the larger of two numbers System.out.println("Math.max(" + i + "," + j + ") is " + Math.max(i,j)); System.out.println("Math.max(" + x + "," + y + ") is " + Math.max(x,y)); System.out.println("Math.max(" + i + "," + x + ") is " + Math.max(i,x)); System.out.println("Math.max(" + y + "," + j + ") is " + Math.max(y,j)); System.out.println(""); // The Math library defines a couple // of useful constants: System.out.println("Math.Pi is " + Math.PI); System.out.println("Math.E is " + Math.E); System.out.println(""); // pow(x, y) returns the x raised // to the yth power. System.out.println("Math.pow(2.0, 3.0) is " + Math.pow(2.0,3.0)); System.out.println("Math.pow(10.0, 3.5) is " + Math.pow(10.0,3.5)); System.out.println("Math.pow(8, -1) is " + Math.pow(8,-1)); System.out.println(""); // sqrt(x) returns the square root of x. for (i=0; i < 4; i++) { System.out.println( "Math.sqrt(" + i + ") is " + Math.sqrt(i)); } System.out.println(""); // Finally there's one Random method // that returns a pseudo-random number // between 0.0 and 1.0; System.out.println("Here's a random number: " + Math.random()); System.out.println("Here's another: " + Math.random()); } }