USING JAVA CLASS LIBRARY Some other useful classes to use.
-
Upload
scarlett-sullivan -
Category
Documents
-
view
216 -
download
2
Transcript of USING JAVA CLASS LIBRARY Some other useful classes to use.
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
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
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
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
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
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.
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()