Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.

Similar presentations


Presentation on theme: "1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized."— Presentation transcript:

1 1 Java Library Lecture 9 by Dr. Norazah Yusof

2 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized in a class library). A package is a collection of interrelated classes in the Java class library. Example of package: java.lang contains classes such as, Object, String, and System. java.awt provides classes such as, Button, TextField, and Graphics.

3 3 Java Library: System and PrintStream The java.lang.System class contains PrintStream objects that perform Input/Output (I/O). The java.lang.PrintStream class contains the print() and println() methods that perform output.

4 4 The import Statement Makes java classes available to programs under their abbreviated names – make the program a bit shorter and more readable. 2 possible forms: For example: the import statement import java.lang.System refers to the System class in java.lang package. import package.class import package.* Allows a specific class to be known by its abbreviated name. Allows all the classes in the specified package to be known by their short names.

5 5 The Scanner Class Java was designed primarily to receive input from a graphical user interface (GUI). Getting information from the keyboard in a console application is not convenient. We can use the Scanner class to simplify standard input

6 6 The Scanner Class (Cont) The Scanner class is defined in java.util, so we import java.util.Scanner; Scanner objects work with System.in To create a Scanner object, Scanner input1 = new Scanner (System.in) Scanner class methods are listed in page 78 in the text.

7 7 The Scanner Class (Cont) next() Reading a string. A string is delimited by spaces nextByte() Reading an integer of the byte type. nextShort() Reading an integer of the short type. nextInt() Reading an int type. nextLong() Reading an integer of the long type. nextFloat() Reading an integer of the float type. nextDouble() Reading an integer of the doublet type.

8 8 Java Library: java.lang.Math (The Math Class) public final class Math {// final class cannot be subclassed private Math() {} // private constructor cannot be invoked... public static native double sqrt (double a) throws ArithmeticException; } The java.lang.Math class provides common mathematical functions. All Math class methods are static class methods. They are invoked as follows: Math.sqrt(55.3) The Math class is a static class and can not be subclassed or instantiated.

9 9 The Math Class Class constants: PI E (A class constant is a final static variable). Class methods: Trigonometric Methods Exponent Methods Rounding Methods min, max, abs, and random Methods (All Math class methods are static class methods).

10 10 Trigonometric Methods The math class contain the following trigonometric methods: public static double sin(double radians) public static double cos(double radians) public static double tan(double radians) public static double acos(double radians) public static double asin(double radians) public static double atan(double radians) public static double toRadians(double degree) public static double toDegrees(double radians)

11 11 Trigonometric Methods sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a) Radians toRadians(90) Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 Math.cos(Math.PI / 2) returns 0

12 12 Exponent Methods There are five methods related to exponents in the Math class: public static double exp(double x) public static double log(double x) public static double log10(double x) public static double pow(double a, double b) public static double sqrt(double x)

13 13 Exponent Methods exp(double a) Returns e raised to the power of a. log(double a) Returns the natural logarithm of a. log10(double a) Returns the 10-based logarithm of a. pow(double a, double b) Returns a raised to the power of b. sqrt(double a) Returns the square root of a. Examples: Math.exp(1) returns 2.71 Math.log(2.71) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24

14 14 Rounding Methods The Math class contains five rounding methods: public static double ceil(double x) public static double floor(double x) public static double rint(double x) public static int round(float x) public static long round(double x)

15 15 Rounding Methods double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value. double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value. double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double. int round(float x) Return (int)Math.floor(x+0.5). long round(double x) Return (long)Math.floor(x+0.5).

16 16 Rounding Methods Examples 1. Math.ceil(2.1) 2. Math.ceil(2.0) 3. Math.ceil(-2.0) 4. Math.ceil(-2.1) 5. Math.floor(2.1) 6. Math.floor(2.0) 7. Math.floor(-2.0) 8. Math.floor(-2.1) 9. Math.rint(2.1) 10. Math.rint(2.0) 11. Math.rint(-2.0) 12. Math.rint(-2.1) 13. Math.rint(2.5) 14. Math.rint(-2.5) 15. Math.round(2.6f) 16. Math.round(2.0) 17. Math.round(-2.0f) 18. Math.round(-2.6)

17 17 Rounding Methods Examples 1. Math.ceil(2.1) returns 3.0 2. Math.ceil(2.0) returns 2.0 3. Math.ceil(-2.0) returns –2.0 4. Math.ceil(-2.1) returns -2.0 5. Math.floor(2.1) returns 2.0 6. Math.floor(2.0) returns 2.0 7. Math.floor(-2.0) returns –2.0 8. Math.floor(-2.1) returns -3.0 9. Math.rint(2.1) returns 2.0 10. Math.rint(2.0) returns 2.0 11. Math.rint(-2.0) returns –2.0 12. Math.rint(-2.1) returns -2.0 13. Math.rint(2.5) returns 2.0 14. Math.rint(-2.5) returns -2.0 15. Math.round(2.6f) returns 3 16. Math.round(2.0) returns 2 17. Math.round(-2.0f) returns -2 18. Math.round(-2.6) returns -3

18 18 min, max, and abs The min and max methods are overloaded to return the minimum and maximum numbers between two numbers (i.e int, long, float, or double) The abs methods are overloaded to return the absolute value of the number (int, long, float, and double)

19 19 min, max, and abs max(a, b) and min(a, b) Returns the maximum or minimum of two parameters. abs(a) Returns the absolute value of the parameter. Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1

20 20 The random Method Generates a random double value greater than or equal to 0.0 and less than 1.0 (0 <= Math.random() < 1.0). Examples: In general,

21 21 View java.lang.Math Documentation You can view the complete documentation for the Math class online from http://java.sun.com/j2se/1.5.0/docs/api/

22 22 The Random Class You have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). A more useful random number generator is provided in the java.util.Random class.

23 23 The Random Class Example If two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed 3. Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(3); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); From random1: 734 660 210 581 128 202 549 564 459 961 From random2: 734 660 210 581 128 202 549 564 459 961

24 24 Using Classes from the Java Library Example 7.1 declared the Circle1 class and created objects from the class. Often you will use the classes in the Java library to develop programs. You learned to obtain the current time using System.currentTimeMillis() in Example 2.5, “ Displaying Current Time. ” You used the division and remainder operators to extract current second, minute, and hour.

25 25 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.

26 26 The Date Class Example For example, the following code java.util.Date date = new java.util.Date(); System.out.println(date.toString()); displays a string like Sun Mar 09 13:50:19 EST 2003.


Download ppt "1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized."

Similar presentations


Ads by Google