Java Reference
In-Depth Information
application that reads values for
side1
and
side2
and performs the calculation with the
hypotenuse
method. Use
Math
methods
pow
and
sqrt
to determine the length of the hypotenuse for each of the
triangles in Fig. 6.15. [
Note:
Class
Math
also provides method
hypot
to perform this calculation.]
Triangle
Side 1
Side 2
1
3.0
4.0
2
5.0
12.0
3
8.0
15.0
Fig. 6.15
|
Values for the sides of triangles in Exercise 6.15.
6.16
(Multiples)
Write a method
isMultiple
that determines, for a pair of integers, whether the
second integer is a multiple of the first. The method should take two integer arguments and return
true
if the second is a multiple of the first and
false
otherwise. [
Hint:
Use the remainder operator.]
Incorporate this method into an application that inputs a series of pairs of integers (one pair at a
time) and determines whether the second value in each pair is a multiple of the first.
6.17
(Even or Odd)
Write a method
isEven
that uses the remainder operator (
%
) to determine
whether an integer is even. The method should take an integer argument and return
true
if the in-
teger is even and
false
otherwise. Incorporate this method into an application that inputs a se-
quence of integers (one at a time) and determines whether each is even or odd.
6.18
(Displaying a Square of Asterisks)
Write a method
squareOfAsterisks
that displays a solid
square (the same number of rows and columns) of asterisks whose side is specified in integer param-
eter
side
. For example, if
side
is
4
, the method should display
****
****
****
****
Incorporate this method into an application that reads an integer value for
side
from the user and
outputs the asterisks with the
squareOfAsterisks
method.
6.19
(Displaying a Square of Any Character)
Modify the method created in Exercise 6.18 to re-
ceive a second parameter of type
char
called
fillCharacter
. Form the square using the
char
pro-
vided as an argument. Thus, if
side
is
5
and
fillCharacter
is
#
, the method should display
#####
#####
#####
#####
#####
Use the following statement (in which
input
is a
Scanner
object) to read a character from the user
at the keyboard:
char
fill = input.next().charAt(
0
);
6.20
(Circle Area)
Write an application that prompts the user for the radius of a circle and uses
a method called
circleArea
to calculate the area of the circle.
6.21
(Separating Digits)
Write methods that accomplish each of the following tasks:
a)
Calculate the integer part of the quotient when integer
a
is divided by integer
b
.
b)
Calculate the integer remainder when integer
a
is divided by integer
b
.