Java Library - PowerPoint PPT Presentation

kolton
java library n.
Skip this Video
Loading SlideShow in 5 Seconds..
Java Library PowerPoint Presentation
Download Presentation
Java Library

play fullscreen
1 / 26
Download Presentation
Java Library
274 Views
Download Presentation

Java Library

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  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 in a class library). • A package is a collection of interrelated classes in the Java class library. • Example of package: • java.langcontains classes such as, Object, String, and System. • java.awtprovides classes such as, Button, TextField, and Graphics.

  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. 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. Allows a specific class to be known by its abbreviated name. import package.class Allows all the classes in the specified package to be known by their short names. import package.*

  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. 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. The Scanner Class (Cont)

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

  9. The Math Class • Class constants: • PI • E • (A class constantis 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. 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. Trigonometric Methods • sin(double a) • cos(double a) • tan(double a) • acos(double a) • asin(double a) • atan(double a) 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 Radians toRadians(90)

  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. 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. 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. 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. Rounding Methods Examples • Math.ceil(2.1) • Math.ceil(2.0) • Math.ceil(-2.0) • Math.ceil(-2.1) • Math.floor(2.1) • Math.floor(2.0) • Math.floor(-2.0) • Math.floor(-2.1) • Math.rint(2.1) • Math.rint(2.0) • Math.rint(-2.0) • Math.rint(-2.1) • Math.rint(2.5) • Math.rint(-2.5) • Math.round(2.6f) • Math.round(2.0) • Math.round(-2.0f) • Math.round(-2.6)

  17. Rounding Methods Examples • Math.ceil(2.1) returns 3.0 • Math.ceil(2.0) returns 2.0 • Math.ceil(-2.0) returns –2.0 • Math.ceil(-2.1) returns -2.0 • Math.floor(2.1) returns 2.0 • Math.floor(2.0) returns 2.0 • Math.floor(-2.0) returns –2.0 • Math.floor(-2.1) returns -3.0 • Math.rint(2.1) returns 2.0 • Math.rint(2.0) returns 2.0 • Math.rint(-2.0) returns –2.0 • Math.rint(-2.1) returns -2.0 • Math.rint(2.5) returns 2.0 • Math.rint(-2.5) returns -2.0 • Math.round(2.6f) returns 3 • Math.round(2.0) returns 2 • Math.round(-2.0f) returns -2 • Math.round(-2.6) returns -3

  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. 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. 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. 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. 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. 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. 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. 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. 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 likeSun Mar 09 13:50:19 EST 2003.