Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** 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.

Similar presentations


Presentation on theme: "***** 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."— Presentation transcript:

1 ***** 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() = 

2 ***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class Ex. 1... 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)

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: -0.2928932188134524 Root #2: -1.7071067811865475 Example from Program “RandomNumbers” (See text CDROM Chapter 3) 2x 2 + 4x + 1

4 ***** 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)

5 ***** 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)

6 ***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class Ex. Run 1... A random integer: 1386436351 From 0 to 9: 9... A random float (between 0-1): 0.9721317 Run 2... A random integer: -1822275137 From 0 to 9: 5... A random float (between 0-1): 0.1125257 Example from Program “RandomNumbers” (See text CDROM Chapter 3)

7 ***** 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.

8 ***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class See the chart below for a partial summary of available formats: FormatMethod125.358 Formatted standard notation - 2 decimal placesformat(“0.##”)125.36 standard notation - 3 decimal placesformat(“0.###”)125.358 scientific notation - 2 decimal placesformat(“0.##E0”)1.25E2

9 ***** 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 = 125.358; 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)); }... 125.36 125.358 1.254E2


Download ppt "***** 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."

Similar presentations


Ads by Google