Java Reference

In-Depth Information

8.8

Self review exercises

SR1.
State whether each of the following is true or false. If false, explain why.

(a) One element of an array can be an
int
and another can be a
double
.

(b) An array-element subscript should normally be of type
int
.

(c) An array can be created with zero elements.

(d) The index of an array element and its subscript are the same thing.

(e) After declaring
b
using
int
[] b;
, one can store a value in
b[0]
.

SR2.
Fill in the blanks:

(a) Elements of an array have the same _________.

(b) To refer to the sixth element of array
b
, use the notation ___________.

(c) In an array element reference like
b[k]
,
b[k]
is called a __________,

and
k
is called the _______.

(d) The number of elements in an array
d
is ____________________.

SR3.
Do the following tasks regarding an array
sizes
.

(a) Declare a final variable
sizeOfSizes
that is initialized to
15
.

(b) Declare variable
sizes
so that it can contain an array with base type
int
.

(c) Create an
int
array with
n
elements and store the array name in
sizes
.

(d) Write Java code to set all the elements of array
sizes
to
0
.

(e) Change the fifth element of array
sizes
to
4.

(f) Write Java code that sets the sixth element of
sizes
to
6
, but only if the

fifth element is even.

Exercises for Chapter 8

Many of these exercises ask for a method to be written. When writing the

method, be sure you write a suitable specification for it before writing the

method body! And test your answers on the computer.

For more exercises, turn to lesson 8 of the
ProgramLive
CD and click on

button
Work
.

E1.
Create an array to contain the number of hours worked by someone each day

in a five-day workweek. Use the following data: Monday, 6 hours; Tuesday, 5;

Wednesday, 7; Thursday, 8; Friday, 7. Write statements to compute the total

hours worked, the average number of hours per day, and the total earnings,

assuming $7.50 per hour.

E2.
A bowling alley has 100 balls with the following weight distribution:

06 pounds: 10 balls

12 pounds: 20 balls

08 pounds: 10 balls

14 pounds: 20 balls

09 pounds: 15 balls

16 pounds: 10 balls

10 pounds: 15 balls

Create an array of 100 elements to contain the weights of the balls. Do not write

Search WWH ::

Custom Search