Chapter 5 Java Class Libraries
Chapter 5 Java Class Libraries
Typical process:
1. Create a Random object
2. Use the object to get random values. Use one of:
nextInt() returns a random integer
nextInt(max) returns an integer random value in [0, … max )
nextDouble() returns a random value in [0.0, 1.0 )
nextBoolean() returns a random value from {true, false}
1
Random Class
Examples
2
Random class – Rock/paper/scissors
3
Random class – toss a coin
4
Random class – toss a coin using booleans
5
Random class
6
Random Number Generators
https://en.wikipedia.org/wiki/Linear_congruential_generator
7
Character class
8
Character class
Examples
Notice in the code how the Character class methods are specified
when there is no object … this may seem odd, but …
9
Character class
Example 1
public class CharacterTypes
13
Character class
Example 3
public class ValidateStudentNumber
No instance of Character is used which means the methods are called using
statements of the form
If ( ! Character.isDigit(c) ) valid = false ;
14
Character class
Example 3
public class ValidateStudentNumber
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Enter a number: ");
String number = kb.next();
// characters are examined one-by-one
boolean valid = true;
for (int i = 0; i < number.length(); i++){
char c = number.charAt(i);
if(! Character.isDigit(c) ) valid = false;
}
if (valid) System.out.println("Valid");
else System.out.println("Invalid");
}
}
15
Scanner class
The Scanner class is useful when you need to parse some stream that
is said to contain tokens.
We can easily create a Scanner object that is associated with one of:
– System.in
– a string Streams containing tokens
– a file
Examples
Scanner s = new Scanner(System.in);
Scanner s = new Scanner(s); //s is of type String
Scanner s = new Scanner(f); //f is of type File
16
Scanner class
Scanner methods
17
Scanner class
Examples
18
Scanner class
Example 1
public class DisplayReadme
The file Readme.txt is read, line-by-line, until there are no lines left.
A Scanner object is needed to provide a reference to the file and the current
location in the file. Consider the statement:
Scanner f = new Scanner( new File("Readme.txt"));
19
Scanner class
Example 1
20
Scanner class
Example 2
public class ScanString
A Scanner object is needed that provides a reference to the file and the current
location in the file. Consider the statement:
String token = s.next() ;
21
Scanner class
Example 2
22
Math class
23
Math class
Methods
pow(…) Raise the first argument to the power
specified in second argument
e.g. Math.pow(x,3)
abs(…) Returns absolute value of its argument
max(…) Returns the larger of two int or
double arguments
min(…) Returns smaller …
…
many more
24
Math class
Example
25
Math class
Example 1
public class FindMax
26
Math class
Example 1
27
Integer class
28
Integer class
Methods
29
Integer class
Example
30
Integer class
Example
public class TotalQuantity
31
Integer class
With similarity to the Integer class, there are classes for other types
… these types of classes are called wrapper classes.
These are called wrapper classes because you instantiate an object and
wrap a primitive value inside
Double
Boolean
Byte
Character
Float
Long
Short
33
Aside: Hierarchy of classes
34