Java Reference

In-Depth Information

E4.
The owners at the Water Hill stables (see exercise E3) would like a bit more

information than just whether a horse has been fed. Create class
Horse
and make

the array an array of class
Horse
. Be creative in the fields and methods you

include in the class.

E5.
Write a function that calculates the
transpose
of a rectangular array
b
. The

transpose of
b
is
b
with its rows and columns interchanged. In other words, sup-

pose
b
is an
m
-by-
n
array. Then the transpose
c
of
b
is an
n
-by-
m
array in which

each element
c[i, j]
has the value
b[j, i]
.

E6.
Write a procedure to print the first
n
rows of Pascal's triangle in a nice for-

mat, with each row centered. Do it two ways. First, have each element take as

many characters as it needs, but no more. Second, have each element take the

same number of characters: the number needed for the maximum integer to be

printed.

E7.
Write a function that, given an
n
-by-
m
array
b
, returns a one-dimensional

array of size
n
that contains the sums of the individual rows of
b
.

E8.
Write a function that tells whether an array is a magic square. An array is a

magic square if: (1) it is an
n
-by-
n
array, for some
n
, (2) it contains the integers

1
,
2
,
3
, ...,
n
2
, (3) the rows, columns, and two diagonals have the same sum. Here

is a magic square:

{{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}

E9.
Type “magic square” into a search engine on the internet and find out about

magic squares. Write a function that, given an odd integer
n
, computes an
n
-by-

n
magic square.

E10.
Think about some area where it might make sense to use a ragged array.

Dream up a problem that would make use of a ragged array and write a Java pro-

gram for it.

Search WWH ::

Custom Search