/* Author : Michael Robinson Program : mathExamples.java Purpose : To present multiple ways of processing Java build-in mathematical functions: processAbsoluteValues( i, j, x, y ); processRoundValues( x, y ); processCeilingValues( x, y ); processFlooringValues( x, y ); processMinimunValues( i, j, x, y ); processMaximunValues( i, j, x, y ); processTrigFunctionValues( i, j ); processExponentialValues(); processLogValues(); processPowerValues(); processSquareRootsValues(); processRandomValues(); Updated : January 14, 2099 */ public class mathExamples { public static void processAbsoluteValues( int i, int j, double x, double y ) { // Math.abs(i) 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( "|" + i + "| is " + Math.abs(i) ); System.out.println( "|" + j + "| is " + Math.abs(j) ); System.out.println( "|" + x + "| is " + Math.abs(x) ); System.out.println( "|" + y + "| is " + Math.abs(y) ); }//end processAbsoluteValues(int i, int j, double x, double y ) public static void processRoundValues( double x, double y ) { // Math.round(x) // Truncating and Rounding functions // You can round off a floating point number // to the nearest integer with round() System.out.println( x + " is approximately " + Math.round(x) ); System.out.println( y + " is approximately " + Math.round(y) ); }//end processRoundValues( double x, double y ); public static void processCeilingValues( double x, double y ) { // Math.ceil(x) // Ceiling of a number is the smallest integer greater than // or equal to the number. Every integer is its own ceiling. System.out.println( "Ceiling of " + x + " is " + Math.ceil(x) ); System.out.println( "Ceiling of " + y + " is " + Math.ceil(y) ); }//end processCeilingValues( double x, double y ); public static void processFlooringValues( double x, double y ) { // Math.floor(i) // 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( "Floor of " + x + " is " + Math.floor(x) ); System.out.println( "Floor of " + y + " is " + Math.floor(y) ); }//end processFlooringValues( double x, double y ) public static void processMinimunValues( int i, int j, double x, double y ) { // min() returns the smaller of the two arguments you pass System.out.println( "min(" + i + "," + j + ") is " + Math.min(i,j) ); System.out.println( "min(" + x + "," + y + ") is " + Math.min(x,y) ); System.out.println( "min(" + i + "," + x + ") is " + Math.min(i,x) ); System.out.println( "min(" + y + "," + j + ") is " + Math.min(y,j) ); }//end processMinimunValues( int i, int j, double x, double y ) public static void processMaximunValues( int i, int j, double x, double y ) { // There's a corresponding max() method // that returns the larger of two numbers System.out.println( "max(" + i + "," + j + ") is " + Math.max(i,j) ); System.out.println( "max(" + x + "," + y + ") is " + Math.max(x,y) ); System.out.println( "max(" + i + "," + x + ") is " + Math.max(i,x) ); System.out.println( "max(" + y + "," + j + ") is " + Math.max(y,j) ); }//end processMaximunValues(int i, int j, double x, double y ) public static void processTrigFunctionValues( int i, int j ) { // The Math library defines a couple of useful constants: System.out.println( "Pi is " + Math.PI ); System.out.println( "e is " + Math.E ); // Trigonometric methods. All arguments are given in radians // Convert a 45 degree angle to radians double angle = 45.0 * 2.0 * Math.PI/360.0; System.out.println( "cos(" + angle + ") is " + Math.cos(angle) ); System.out.println( "sin(" + angle + ") is " + Math.sin(angle) ); //Inverse Trigonometric methods. //All values returned as radians double value = 0.707; System.out.println( "acos(" + value + ") is " + Math.acos(value)); System.out.println( "asin(" + value + ") is " + Math.asin(value) ); System.out.println( "atan(" + value + ") is " + Math.atan(value) ); System.out.println( "atan(" + i + ") is " + Math.atan(i) ); System.out.println( "atan(" + j + ") is " + Math.atan(j) ); }//processTrigFunctionValues(int i, int j, double x, double y ) public static void processExponentialValues() { // Exponential and Logarithmic Methods // exp(a) returns e (2.71828...) raised to the power of a. System.out.println( "exp(1.0) is " + Math.exp(1.0) ); System.out.println( "exp(10.0) is " + Math.exp(10.0) ); System.out.println( "exp(0.0) is " + Math.exp(0.0) ); }//end processExponentialValues(); public static void processLogValues() { // log(a) returns the natural logarithm (base e) of a. System.out.println( "log(1.0) is " + Math.log(1.0) ); System.out.println( "log(10.0) is " + Math.log(10.0) ); System.out.println( "log(Math.E) is " + Math.log(Math.E) ); }//end processLogValues(); public static void processPowerValues() { // pow(x, y) returns the x raised to the yth power. System.out.println( "pow(2.0, 2.0) is " + Math.pow(2.0,2.0) ); System.out.println( "pow(10.0, 3.5) is " + Math.pow(10.0,3.5) ); System.out.println( "pow(8, -1) is " + Math.pow(8,-1) ); }//end processPowerValues(); public static void processSquareRootsValues() { // sqrt(i) returns the square root of i. int i = 0; for( i = -7; i < 10; i++ ) { System.out.println( "The square root of " + i + " is " + Math.sqrt(i)); } }//end processSquareRootsValues() public static void processRandomValues() { // Finally there's one Random method that returns a // pseudo-random number between 0.0 and 1.0; System.out.println( "Here's one random number: " + Math.random() ); System.out.println( "Here's another random number: " + Math.random() ); }//end public static void processRandomValues() public static void main( String args[] ) { int i = 7; int j = 9; double x = 72.5; double y = 0.34; processAbsoluteValues( i, j, x, y ); processRoundValues( x, y ); processCeilingValues( x, y ); processFlooringValues( x, y ); processMinimunValues( i, j, x, y ); processMaximunValues( i, j, x, y ); processTrigFunctionValues( i, j ); processExponentialValues(); processLogValues(); processPowerValues(); processSquareRootsValues(); processRandomValues(); System.out.println( "\nEnd of Program" ); }//end public static void main( String args[] ) }//end public class mathExamples