Java Reference

In-Depth Information

Next, it will swap 6 and 10 and produce the array.

7610321

After the first pass of the sort is complete,

the array will be as follows.

7632110.

The second pass of the sort will work exactly the same way as the first pass (i.e., going

from left to right swapping numbers), but it will stop one number short of the end (i.e., it

does not have to read the number 10 because we know that 10 is the biggest number in the

array). If the input contains
n
numbers, then
n-1
passes of the array will sort it.

Write a method that sorts an array of integers using bubble sort. The method can use an

auxiliary method that swaps two entries in the array. Use a nested
for
loop to implement

the bubble sort. The method should modify the input array and have
void
return type.

Test your method with different inputs.

5.11 Project

Extend the Yahtzee game to include the combinations that are shown in Table 5.2. The

user has seven sets of rolls and the goal is to complete as many of these combinations as

possible.

If the user rolls a combination that fits in more than one category, then the program

should chose in the following order: Yahtzee, Four-of-a-Kind, Large Straight, Full House,

Three-of-a-Kind, Small Straight, Chance. Here is part of an example run of the program

(user input is in italic).

Your dice: 23435

Do you want to reroll (Y/N):

Y

Which dice:
2

Your dice: 24435

Do you want to reroll (Y/N): Y

Which dice:
2

Your dice: 26435

Congratulations, you got a Large Straight that counts for 40 points!

When the program finishes, it should print the total point score of the player. The program

consists of seven rolls, where every roll can have up to two sub-rolls. If the dice that are

rolled do not fit in any combination and chance is already used, then the program should

not assign the dice to any combination and give score 0 for the roll.

TABLE 5 . 2 :

Yahtzee game combinations.

Category

Description

Score

Example

Three-of-a-Kind

Sum of all dice

{

2,3,4,4,4

}

Four-of-a-Kind

Sum of all dice

{

4,5,5,5,5

}

Full House

3-of-a-kind and a pair 25

{

2,2,5,5,5

}

Small Straight

4 sequential dice

30

{

1,3,4,5,6

}

Large Straight

5 sequential dice

40

{

1,2,3,4,5

}

Yahtzee

All the same

50

{

1,1,1,1,1

}

{

}

Chance

Any dice

Sum of all dice

2,3,5,5,2