Java Reference
In-Depth Information
2.25 (Odd or Even) Write an application that reads an integer and determines and prints wheth-
er it's odd or even. [ Hint: Use the remainder operator. An even number is a multiple of 2. Any mul-
tiple of 2 leaves a remainder of 0 when divided by 2.]
2.26 (Multiples) Write an application that reads two integers, determines whether the first is a
multiple of the second and prints the result. [ Hint: Use the remainder operator.]
2.27 (Checkerboard Pattern of Asterisks) Write an application that displays a checkerboard pat-
tern, as follows:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
2.28 (Diameter, Circumference and Area of a Circle) Here's a peek ahead. In this chapter, you
learned about integers and the type int . Java can also represent floating-point numbers that contain
decimal points, such as 3.14159. Write an application that inputs from the user the radius of a circle
as an integer and prints the circle's diameter, circumference and area using the floating-point value
3.14159 for
π
. Use the techniques shown in Fig. 2.7. [ Note: You may also use the predefined con-
stant Math.PI for the value of
π
. This constant is more precise than the value 3.14159. Class Math
is defined in package java.lang . Classes in that package are imported automatically, so you do not
need to import class Math to use it.] Use the following formulas ( r is the radius):
diameter = 2 r
circumference = 2
π
r
π
r 2
Do not store the results of each calculation in a variable. Rather, specify each calculation as the
value that will be output in a System.out.printf statement. The values produced by the circum-
ference and area calculations are floating-point numbers. Such values can be output with the for-
mat specifier %f in a System.out.printf statement. You'll learn more about floating-point
numbers in Chapter 3.
2.29 (Integer Value of a Character) Here's another peek ahead. In this chapter, you learned about
integers and the type int . Java can also represent uppercase letters, lowercase letters and a consider-
able variety of special symbols. Every character has a corresponding integer representation. The set
of characters a computer uses together with the corresponding integer representations for those
characters is called that computer's character set. You can indicate a character value in a program
simply by enclosing that character in single quotes, as in 'A' .
You can determine a character's integer equivalent by preceding that character with (int) , as in
( int ) 'A'
An operator of this form is called a cast operator. (You'll learn about cast operators in Chapter 4.)
The following statement outputs a character and its integer equivalent:
System.out.printf( "The character %c has the value %d%n" , 'A' , (( int ) 'A' ));
When the preceding statement executes, it displays the character A and the value 65 (from the Uni-
code ® character set) as part of the string. The format specifier %c is a placeholder for a character (in
this case, the character 'A' ).
Using statements similar to the one shown earlier in this exercise, write an application that dis-
plays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols.
Display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.
area =
 
Search WWH ::




Custom Search