Java Reference
In-Depth Information
Enter a row (0, 1, or 2) for player X:
. . .
——————-——————
| X | | |
——————-——————
| O | X | O |
——————-——————
| | | X |
——————-——————
X player won
*8.10
( Largest row and column ) Write a program that randomly fills in 0s and 1s into
a 4-by-4 matrix, prints the matrix, and finds the first row and column with the
most 1s. Here is a sample run of the program:
0011
0011
1101
1010
The largest row index: 2
The largest column index: 2
**8.11
( Game: nine heads and tails ) Nine coins are placed in a 3-by-3 matrix with some
face up and some face down. You can represent the state of the coins using a
3-by-3 matrix with values 0 (heads) and 1 (tails). Here are some examples:
0 0 0 1 0 1 1 1 0 1 0 1 1 0 0
0 1 0 0 0 1 1 0 0 1 1 0 1 1 1
0 0 0 1 0 0 0 0 1 1 0 0 1 1 0
Each state can also be represented using a binary number. For example, the pre-
ceding matrices correspond to the numbers
000010000 101001100 110100001 101110100 100111110
There are a total of 512 possibilities, so you can use decimal numbers 0, 1, 2, 3,
. . . , and 511 to represent all states of the matrix. Write a program that prompts
the user to enter a number between 0 and 511 and displays the corresponding
matrix with the characters H and T . Here is a sample run:
Enter a number between 0 and 511: 7
H H H
H H H
T T T
The user entered 7 , which corresponds to 000000111 . Since 0 stands for H and
1 for T , the output is correct.
**8.12
( Financial application: compute tax ) Rewrite Listing 3.5, ComputeTax.java,
using arrays. For each filing status, there are six tax rates. Each rate is applied
to a certain amount of taxable income. For example, from the taxable income of
$400,000 for a single filer, $8,350 is taxed at 10%, (33,950
-
8,350) at 15%,
 
Search WWH ::




Custom Search