Java Reference
In-Depth Information
10.
You can get the length of a string by invoking its length() method, retrieve a char-
acter at the specified index in the string using the charAt(index) method, and
use the indexOf and lastIndexOf methods to find a character or a substring in
a string.
11.
You can use the concat method to concatenate two strings, or the plus ( + ) operator to
concatenate two or more strings.
12.
You can use the substring method to obtain a substring from the string.
13.
You can use the equals and compareTo methods to compare strings. The equals
method returns true if two strings are equal, and false if they are not equal. The
compareTo method returns 0 , a positive integer, or a negative integer, depending on
whether one string is equal to, greater than, or less than the other string.
14.
The printf method can be used to display a formatted output using format
specifiers.
Q UIZ
Answer the quiz for this chapter online at www.cs.armstrong.edu/liang/intro10e/quiz.html .
P ROGRAMMING E XERCISES
Section 4.2
4.1
( Geometry: area of a pentagon ) Write a program that prompts the user to enter
the length from the center of a pentagon to a vertex and computes the area of the
pentagon, as shown in the following figure.
r
s 2
5
*
The formula for computing the area of a pentagon is Area
=
, where
5
4
*
tan
¢
2 r sin 5 ,
s is the length of a side. The side can be computed using the formula s
=
where r is the length from the center of a pentagon to a vertex. Round up two digits
after the decimal point. Here is a sample run:
Enter the length from the center to a vertex: 5.5
The area of the pentagon is 71.92
 
Search WWH ::




Custom Search