Utility Classes
Utility Classes
Programming
Grade in Computer Engineering
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 2
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 3
1. Math library
Advanced maths
http://java.sun.com/javase/7/docs/api/java/lang/Math.html
Math class is inside the java.lang package – it is not necessary to import it!
Other Java mathematical libraries: Apache Jakarta Math library
[email protected] 4
1. Math library
AdvancedMaths.java
5
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 6
2. Wrapper classes
Number classes
Primitive datatypes are used most of the time to work with numbers
int x = 1;
double y = 1.2, z;
z = x * y;
Integer x, y;
x = 12;
y = 15;
System.out.println(x+y);
[email protected] 7
2. Wrapper classes
Using wrapper classes
There are three reasons that you might use a Number object rather than a primitive:
As an argument of a method that expects an object (often used when manipulating collections of
numbers).
To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper
and lower bounds of the data type.
To use class methods for converting values to and from other primitive types, for converting to and from
strings, and for converting between number systems (decimal, octal, hexadecimal, binary)
[email protected] 8
2. Wrapper classes
Extended number classes
[email protected] 9
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 10
3. String management
String class
[email protected] 12
3. String management
Parsing strings into numbers
[email protected] 13
3. String management
Parsing numbers into strings
> byte:
String s = String.valueOf(b);
> short:
String s = String.valueOf(s);
[email protected] 14
3. String management
Examples
Strings.java
[email protected] 15
3. String management
Other string methods
Type Method
int compareTo(String anotherString)
Compares two strings lexicographically
boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values
boolean equals(Object anObject)
Compares this string to the specified object
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring
int lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring
boolean matches(String regex)
Tells whether or not this string matches the given regular expression
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement
String [] split(String regex)
Splits this string around matches of the given regular expression
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
http://download.oracle.com/javase/7/docs/api/java/lang/String.html
[email protected] 16
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 17
4. Input and output
Printing on the screen
Line jump can be printed with the new line character ‘\n’:
println("Hi!") is equivalent to print("Hi!\n")
[email protected] 18
4. Input and output
Printing on the screen
Printing.java
[email protected] 19
4. Input and output
Formatting output
System.out.printf
String including conversion characters (placeholders)
%d: integers
%f: floating point numbers
%c: characters
%s: strings
…different number notations, dates, etc.
[email protected] 20
4. Input and output
Formatting output
and more…
[email protected] 21
4. Input and output
Formatting output
FormattedOutput.java
[email protected] 22
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 23
4. Input and output
Reading from the keyboard
Scanner class can be used to read values from the keyboard (see
Lesson 2, slide 33)
Use:
1. Import java.util.* package
import java.util.*;
3. Read values
Integer: int a = sc.nextInt ();
Float: float b = sc.nextFloat();
Double: double c = sc.nextDouble();
String: String s = sc.next(); (No blank spaces)
String s = sc.nextLine(); (With blank spaces)
…
[email protected] 24
4. Input and output
Alternative to Scanner
[email protected] 25
4. Input and output
Alternative to Scanner
InputStreams.java
[email protected] 26
4. Input and output
Alternative to Scanner
InputStreams.java
[email protected] 27
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 28
4. Input and output
Arguments to main
[email protected] 29
4. Input and output
Arguments to main
Arguments
AddTwoArguments.java
[email protected] 30
Arguments to main
AddTwoArguments.java
31
4. Input and output
Arguments to main
Arguments.java
[email protected] 32
4. Input and output
Arguments to main
AddTwoArguments.java
[email protected] 33
4. Input and output
Arguments to main
AddTwoArguments.java
[email protected] 34
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 35
5. Collections
ArrayList
Arrays:
Hold a sequence of basic type values or objects
They have fixed-length, specified in the initialization with new (can
be consulted with array.length)
If we want to add a new element that exceeds its capacity, we
need to define a new larger array, and then copy the first one into
the second one (System.arraycopy)
Dynamic arrays:
Hold objects of a single type (jdk1.5 introduces “generics”, with
extended functionalities)
They have variable length and elements can be dynamically added
or removed from the collection
Java provides several pre-implemented classes to represent and
manage sets/lists in the package java.util: ArrayList, Vector, etc.
36
5. Collections
ArrayList
37
5. Collections
ArrayList
ArrayListExample.java
38
5. Collections
ArrayList
ArrayListExampleObjects.java
39
5. Collections
Enhanced for for collections
ArrayList
ArrayList<Point2D> points = new ArrayList<Point2D>();
…
for(int i=0; i<a.size(); i++) {
Point2D p = points.get(i);
// operate with p
}
[email protected] 40
5. Collections
Enhanced for for collections
[email protected] 42
Outline
1. Math class
2. Wrapper classes
3. String management
4. Input and output
Printing on the screen
Reading from keyboard
Arguments to main
5. Collections
[email protected] 43
Summary
Utility classes
> Strings
Conversion from String > number
parse methods of wrapper classes
Conversion from number > String
valueOf method of String
The compiler automatically "stringifies" numbers
[email protected] 44
Summary
Utility classes
> Printing
System.out.println, System.out.printf
> Reading
Scanner, InputStreamReader + BufferedReader
> Arguments
args String []
> Collections
ArrayList
extended for
[email protected] 45
Additional lectures
Utility classes
Recommended lectures
The JavaTM Tutorials. Oracle, Numbers and Strings [link]
H. M. Deitel, P. J. Deitel. Java: How to Program. Prentice Hall,
2007 (7th Edition), Chapters 29 [link], 30 [link]
K. Sierra, B. Bates. Head First Java. O'Reilly Media, 2005 (2nd
Edition), Chapter 10 [link]
H. M. Deitel, P. J. Deitel. Java: How to Program. Prentice Hall,
2007 (7th Edition), Chapter J [link]
[email protected] 46
Programming – Grado en Ingeniería Informática
Authors
Of this version:
Juan Gómez Romero
47