Download section 7.2 handout

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object Oriented Programming II
7.2. Standard Classes
There are certain predefined classes that provide useful services. Among these are the
String class, the Math class, the NumberFormat class and the DecimalFormat class.
The String class
Every character string is an object in Java, defined by the String class. Every literal string,
delimited by double quotation marks, represents a String object.
String concatenation (+) is used to append one string to the end of another. It can also be
used to append a number to a string.
The plus operator (+) is also used for arithmetic addition. The function that the + operator
performs depends on the type of the information on which it operates. If both operands are
strings, or if one is a string and one is a number, it performs string concatenation. If both
operands are numeric, it adds them. The + operator is evaluated left to right. Parentheses can
be used to force the operation order.
The String class has several methods that are useful for manipulating strings. These methods
return a value, such as an integer or a new String object.
Since strings are objects, we cannot use the relational operators to compare strings. The
equals method can be called on a string to determine if two strings contain exactly the
same characters in the same order.
The String class also contains a method called compareTo to determine if one string
comes before another alphabetically (as determined by the Unicode character set).
Class Libraries
A class library is a collection of classes that we can use when developing programs. A Java
standard class library comes with any Java Development Environment. These classes are not
part of the Java language per se, but we rely on them heavily.
The System class and the String class are part of the Java standard class library. Other class
libraries can be obtained through third party vendors, or you can create them yourself.
1
Object Oriented Programming II
The classes of the Java standard class library are organized into packages. The packages in
the standard class library include:
Package
is used for
java.lang
General support
java.applet
Creating applets for the web
java.awt
Graphics and graphical user interfaces
javax.swing
Additional graphics capabilities and components
java.net
Network communication
java.util
Utilities
In order to use a class from a package, you first need to import it using the import statement.
For example, the statement
import java.util.Random;
gives you access to a random generator.
To import all classes in a particular package, you can use the * wildcard character. For
example,
import java.util.*;
All classes of the java.lang package are automatically imported into all programs.
Oracle’s API specification for Java provides a description of all classes in java.lang,
which includes the services provided by these classes.
The System and String classes belong to java.lang, and so they do not need to be
imported into classes that use them.
The Random class is part of the java.util package. It provides methods that generate
pseudo-random numbers. We often have to scale and shift a number into an appropriate range
for a particular purpose.
The Math Class
The Math class contains many static methods that provide various mathematical functions,
including, absolute value, trigonometry functions and square root. These methods can be
invoked through the class name, instead of through an object of the class.
2
Object Oriented Programming II
For example,
double discriminant = Math.pow(b, 2) - (4 * a * c);
You should look these methods up in the Oracle API.
The NumberFormat Class
The NumberFormat class has static methods that return a formatter object.
For example,
getCurrencyInstance() //returns a number formatted as currency
getPercentInstance()
// returns a number formatted as a percentage
Each formatter object has a method called format that returns a string with the specified
information in the appropriate format.
The video demonstrates how the NumberFormat class is used.
The DecimalFormat class
The DecimalFormat class can be used to format a floating point value in generic ways.
For example, you can specify that the number be printed to three decimal places. The
constructor of the DecimalFormat class takes a string that represents a pattern for the
formatted number.
The video demonstrates how the DecimalFormat class is used.
3