USING JAVA CLASS LIBRARY Some other useful classes to use.

8
USING JAVA CLASS LIBRARY Some other useful classes to use

Transcript of USING JAVA CLASS LIBRARY Some other useful classes to use.

Page 1: USING JAVA CLASS LIBRARY Some other useful classes to use.

USING JAVA CLASS LIBRARY

Some other useful classes to use

Page 2: USING JAVA CLASS LIBRARY Some other useful classes to use.

Import declaration

Each class in the library must be imported

You can use the * as a wild card with import statements

import statements go before the class heading

Page 3: USING JAVA CLASS LIBRARY Some other useful classes to use.

Scanner Class

Constructors Scanner(InputStream source) Scanner(File source) Scanner(String source)

String next() Returns the next input token as a character

string String nextLine()

returns all input remaining on the current line as a character string

Page 4: USING JAVA CLASS LIBRARY Some other useful classes to use.

Scanner class

boolean nextBoolean() byte nextByte() double nextDouble() float nextFloat() int nextInt() long nextLong() short nextShort()

returns the next input token as the indicated type. Throws InputMismatchException if the next token is inconsistent with the type

Page 5: USING JAVA CLASS LIBRARY Some other useful classes to use.

Random class

Random() constructor: creates a new pseudorandom

number generator double nextDouble()

returns a random number between 0.0 inclusive and 1.0 exclusive

int nextInt(int num) Returns a random number in the range of 1

to num-1

Page 6: USING JAVA CLASS LIBRARY Some other useful classes to use.

Math class

Don’t import anything static int abs(int num) static double abs(double num)

returns the absolute value of num static double pow(double num, double

power) returns the value num raised to the

specified power static double sqrt(double num)

returns the square root of num, which must be positive

Page 7: USING JAVA CLASS LIBRARY Some other useful classes to use.

DecimalFormat class

DecimalFormat(String pattern) Constructor: creates a new DecimalFormat

object with the specified pattern void applyPattern(String pattern)

applies the specified pattern to this DecimalFormat object

String format(double number) returns a string containing the specified

number formatted according to the current pattern.

Page 8: USING JAVA CLASS LIBRARY Some other useful classes to use.

String class

Can use the = operator and “ “ to construct string

other useful methods char charAt(int index) boolean equals(String str) boolean equalsIgnoreCase(String str) int indexOf(String str) int length() String toLowerCase() String toUpperCase()