Java Reference
In-Depth Information
LISTING 3.3
continued
OUTPUT
Enter the coefficient of x squared: 3
Enter the coefficient of x: 8
Enter the constant: 4
Root #1: -0.6666666666666666
Root #2: -2.0
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 3.20 What is a class method (also called a static method)?
SR 3.21 What is the value of each of the following expressions?
a. Math.abs(10) + Math.abs(-10)
b. Math.pow(2, 4)
c. Math.pow(4, 2)
d. Math.pow(3, 5)
e. Math.pow(5, 3)
f. Math.sqrt(16)
SR 3.22 Write a statement that prints the sine of an angle measuring 1.23 radians.
SR 3.23 Write a declaration for a double variable called result and initialize it
to 5 raised to the power 2.5.
SR 3.24 Using the online Java API documentation, list three methods of the
Math class that are not included in Figure 3.5.
3.6 Formatting Output
The NumberFormat class and the DecimalFormat class are used to format informa-
tion so that it looks appropriate when printed or displayed. They are both part of
the Java standard class library and are defined in the java.text package.
The NumberFormat Class
The NumberFormat class provides generic formatting capabilities for numbers.
You don't instantiate a NumberFormat object by using the new operator. Instead,
you request an object from one of the static methods that you invoke through the
class name itself. Figure 3.6 lists some of the methods of the NumberFormat class.
 
Search WWH ::




Custom Search