1 java library lecture 9 by dr. norazah yusof. 2 java library java has pre-defined classes that...

26
1 Java Library Lecture 9 by Dr. Norazah Yusof

Upload: delilah-wilcox

Post on 21-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Java Library

Lecture 9by

Dr. Norazah Yusof

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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.

Page 5: 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

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

Page 6: 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

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.

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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

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

Page 10: 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

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)

Page 11: 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

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

Page 12: 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

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)

Page 13: 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

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

Page 14: 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

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)

Page 15: 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

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

Page 16: 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

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)

Page 17: 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

17

Rounding Methods Examples

1. Math.ceil(2.1) returns 3.0 2. Math.ceil(2.0) returns 2.03. Math.ceil(-2.0) returns –2.04. Math.ceil(-2.1) returns -2.05. Math.floor(2.1) returns 2.06. Math.floor(2.0) returns 2.07. Math.floor(-2.0) returns –2.08. Math.floor(-2.1) returns -3.09. Math.rint(2.1) returns 2.010. Math.rint(2.0) returns 2.011. Math.rint(-2.0) returns –2.012. Math.rint(-2.1) returns -2.013. Math.rint(2.5) returns 2.014. Math.rint(-2.5) returns -2.015. 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

Page 18: 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

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)

Page 19: 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

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

Page 20: 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

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:

(int)(Math.random() * 10) Returns a random integer

between 0 and 9.

50 + (int)(Math.random() * 50) Returns a random integer between 50 and 99.

In general,

a + Math.random() * b Returns a random number between

a and a + b, excluding a + b.

Page 21: 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

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/

Page 22: 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

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.

java.util.Random

+Random()

+Random(seed: long)

+nextInt(): int

+nextInt(n: int): int

+nextLong(): long

+nextDouble(): double

+nextFloat(): float

+nextBoolean(): boolean

Constructs a Random object with the current time as its seed.

Constructs a Random object with a specified seed.

Returns a random int value.

Returns a random int value between 0 and n (exclusive).

Returns a random long value.

Returns a random double value between 0.0 and 1.0 (exclusive).

Returns a random float value between 0.0F and 1.0F (exclusive).

Returns a random boolean value.

Page 23: 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

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

Page 24: 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

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.

Page 25: 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

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.

java.util.Date

+Date()

+Date(elapseTime: long)

+toString(): String

+getTime(): long

+setTime(elapseTime: long): void

Constructs a Date object for the current time.

Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.

Returns a string representing the date and time.

Returns the number of milliseconds since January 1, 1970, GMT.

Sets a new elapse time in the object.

The + sign indicates public modifer

Page 26: 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

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.