***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically imported. A Math object does not have to be instantiated. See the chart below for a partial summary of available methods: Math FunctionMethodExample square rootsqrt(double a) Math.sqrt(value) = value exponentiation (e a )exp(double a)Math.exp(value) = e value power function (x a )pow(double x, double a)Math.pow(value, 3) = value 3 value of PIPI() Math.PI() =
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class Ex int a, b, c; // ax^2 + bx + c double discriminant, root1, root2; Scanner scan = new Scanner (System.in); System.out.print ("Enter the coefficient of x squared: "); a = scan.nextInt(); System.out.print ("Enter the coefficient of x: "); b = scan.nextInt(); System.out.print ("Enter the constant: "); c = scan.nextInt(); Example from Program “Quadratic” (See text CDROM Chapter 3)
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class Ex. 1 // Use the quadratic formula to compute the roots. // Assumes a positive discriminant. discriminant = Math.pow(b, 2) - (4 * a * c); root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a); root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a); System.out.println ("Root #1: " + root1); System.out.println ("Root #2: " + root2);... Enter the coefficient of x squared: 2 Enter the coefficient of x: 4 Enter the constant: 1 Root #1: Root #2: Example from Program “RandomNumbers” (See text CDROM Chapter 3) 2x 2 + 4x + 1
***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class The Random class provides a convenient way to generate random numbers. The Random class generates both floating point (float) and integer random numbers. Must be imported from “java.util” package See the chart below for a partial summary of available methods: Random Number TypeMethod floatnextFloat() Integer (range: all integers)nextInteger() Integer (range: 1 to num)nextInteger(int num)
***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class Ex.... Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1);... num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2); Example from Program “RandomNumbers” (See text CDROM Chapter 3)
***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class Ex. Run 1... A random integer: From 0 to 9: 9... A random float (between 0-1): Run 2... A random integer: From 0 to 9: 5... A random float (between 0-1): Example from Program “RandomNumbers” (See text CDROM Chapter 3)
***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class The DecimalFormat class provides a convenient way to format output values. DecimalFormat class has one method format. format uses a string pattern to control: Number of decimal places Leading zeros or not Show as percentage Show in scientific notation Plus others. See Java Reference. Must be imported from “java.text” package.
***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class See the chart below for a partial summary of available formats: FormatMethod Formatted standard notation - 2 decimal placesformat(“0.##”) standard notation - 3 decimal placesformat(“0.###”) scientific notation - 2 decimal placesformat(“0.##E0”)1.25E2
***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class Ex. import java.text.DecimalFormat; public class FormatOutput { public static void main(String[] args) { double num = ; DecimalFormat fmt0 = new DecimalFormat ("0.##"); DecimalFormat fmt1 = new DecimalFormat ("0.###"); DecimalFormat fmt2 = new DecimalFormat ("0.###E0"); System.out.println(fmt0.format(num)); System.out.println(fmt1.format(num)); System.out.println(fmt2.format(num)); } E2