Java Reference
In-Depth Information
E26.
Write a function that has three
int
parameters
a
,
b
, and
c
and returns the
value of the statement “
a
,
b
,
c
are the lengths of the sides of a triangle”. Three
such values form the lengths of the sides of a triangle if and only if
a≤b+c
,
b
≤c+a
, and
c≤a+b
.
E27.
Write a procedure that has three
int
parameters
a
,
b
, and
c
and returns one
of these strings:
"equilateral"
(meaning the sides have the same length),
"isosceles"
(only two sides are equal),
"triangle"
(no sides equal, but they
form a triangle; see the previous exercise), or
"not a triangle"
.
E28.
Write a function that has as its parameters the lengths
a
,
b
, and
c
of a tri-
angle and returns the area of the triangle. Let
s
be
1/2
the perimeter of the tri-
angle. Then the area of the triangle is the square root of the expression
s*(s-
a) * (s - b) * (s - c)
.
E29.
Write a procedure that has as its parameters the (two) lengths of the sides
of a rectangle and prints: the area, the perimeter, and the length of the diagonal.
E30.
Write a function with an
int
parameter
n
, with the precondition that
0≤n<1000
, and returns the three digits of the number (with leading zeros if nec-
essary) with a blank between adjacent digits. For example, for argument
43
, the
string
"0 4 3"
is returned.
E31.
Write a function that is given the coordinates
(x
1
, y
1
)
,
(x
2
, y
2
)
of two
points and returns the distance between them: the square root of
(x
1
-x
2
)
2
+
(y
1
-y
2
)
2
.
E32.
Write a function that is given a letter in
A..Z
and returns the corresponding
digit on the telephone. Here is the translation:
2:ABC
,
3:DEF
,
4:GHI
,
5:KHL,
6:MNO
,
7:PRS
,
8:TUV
, and
9:WXY
. For
Z
, return
-2
, and for
Q
, return
1
.
E33.
Turn to lesson page 2-1 of the CD
ProgramLive
and click the Project icon.
Do Project Dates, which asks you to write 5-6 functions that manipulate dates in
various ways.
E34.
Write an if-statement that swaps (exchanges) the values of
int
variables
b
and
c
, if necessary, so that the larger of the two is in
c
.
E35.
Is it possible to write the body of procedure
swap
, shown below, so that it
is consistent with its specification? For example, if you write a call
swap(d,
e);
, is the larger of
d
and
e
guaranteed to be in
e
after the call? If you do not
fully understand, do not guess. Instead, write the procedure body and then exe-
cute the call to it by hand, using the steps given in Sec. 2.7.2, and see what hap-
pens. Explain your answer.
Search WWH ::
Custom Search