0% found this document useful (0 votes)
2 views13 pages

Java Random Part 2 Class Notes

The document discusses various utility classes in Java, including the Scanner, Math, and Integer classes. It explains their methods, constants, and provides examples of how to use them in code. Additionally, it introduces the concept of wrapper classes for primitive types and mentions class hierarchies in Java.

Uploaded by

carawa6255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Java Random Part 2 Class Notes

The document discusses various utility classes in Java, including the Scanner, Math, and Integer classes. It explains their methods, constants, and provides examples of how to use them in code. Additionally, it introduces the concept of wrapper classes for primitive types and mentions class hierarchies in Java.

Uploaded by

carawa6255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Scanner class

Example 2

public class ScanString


{
public static void main(String[] args)
{
String sample = "one two \tthree";
Scanner s = new Scanner(sample);
System.out.println("<<<<"+sample+">>>>");
while (s.hasNext()){
String token = s.next();
System.out.println(token);
}
System.out.println("<<<< end of tokens >>>>");
}
}

22
Math class

Math class is a utility class

•You cannot create an instance of Math


•All references to constants and methods will use the prefix Math.

•Contains constants, π and e


Names of these are PI and E
Java convention is to name constants using capital letters.

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

1. Find the largest of 3 int values


As there is no object note the use of the prefix Math.

25
Math class

Example 1
public class FindMax

The larger of 3 integers is determined. The max method is used twice.

Scanner methods used:


Math.max() returns the larger of two values passed in as arguments

Math is a utility class with static methods.


Consider the statement:
Math.max(j,k)

Two arguments, j and k, passed in to max

The method max

The Math class

26
Math class

Example 1

public class FindMax


{
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.println(
"Please enter 3 int values");
int i = kb.nextInt(); Note how Math.max(…) is used twice
int j = kb.nextInt();
int k = kb.nextInt();
int mx = Math.max(i, Math.max(j,k));
System.out.println("largest is "+mx);
}
}

27
Integer class

Integer class is a utility class

•Many methods are static


you do not need an object of type Integer.
The prefix Integer. is used for these.

•Contains constants, MAX_VALUE and MIN_VALUE


Again … Java convention is to name constants using capital letters.

28
Integer class

Methods

max(…) Returns the larger of two int arguments


min(…) Returns smaller …
parseInt(…) Parses the string argument expecting that
argument to be a valid decimal integer.
Correction to notes: E.g. parseInt(" 23 ")
no spaces
Should be: E.g. parseInt("23")

to ensure there are no leading or trailing spaces


in a string one can use the trim() method
String xx = ...
xx = xx.trim();

29
Integer class

Example

Read lines of text from System.in


Each line is parsed according to the expected format:
<name of an item><comma><quantity as integer>

Examples of such lines:


monitor,45
laptop,55

30
Integer class

Example
public class TotalQuantity

Integer methods used:


parseInt(…) returns the integer represented by a character string

Integer is a utility class with static methods.


Consider the statement:
int qty = Integer.parseInt(qtyAsString);

Int variable String containing an integer

The method parseInt

The Integer class

31
Integer class

If qtyAsString does not have a


Example 1 valid integer value the program
will terminate with an error
public class TotalQuantity
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int totalQty = 0;
for (int i = 0; i < 4; i++){
System.out.print("Enter next line: ");
String line = kb.nextLine();
int commaAt = line.indexOf(",");
String qtyAsString = ine.substring(commaAt+1);
int qty = Integer.parseInt(qtyAsString);
totalQty += qty;
}
System.out.println("total = "+totalQty);
}
} 32
Wrapper classes

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

A topic in ACS-1904 is class hierarchies. For example:

All classes are subclasses of Object

34

You might also like