AP Java: CHAPTER 5 PROGRAMS

advertisement
AP Java: CHAPTER 5 PROGRAMS
1.
Use the Dice class that we wrote in class. Create a driver (main) class called BoxCars that
instantiates and rolls two six-sided dice objects. Have the dice roll 1000 times, counting the number of
box cars (two sixes) that occur. Finally, print this number, as an integer as well as a percent out of the
thousand dice rolls. How does this number match up to the theoretical amounts?
(1000 rolls – 27.8 boxcars, 2.78%)
(15 points)
2.
Design a class called Card that represents a standard playing card. Each card has a suit and a face
value. NOTE: ALL CARD INFORMATION SHOULD BE CREATED IN THE CLASS. Create a
program that deals 20 random cards (cards can repeat themselves!) (25 points)
3.
Design a class called Rectangle that contains TWO private data members (instance data) named
length and width. Let the class contain the following member functions (4 methods):
a constructor, getPerimeter, getArea, getDiagonalLength.
The driver program send data for length and width to the constructor, then calling the methods, and
printing the perimeter of the rectangle, the area, and the diagonal length. (25 points)
4.
Use the Dice class that we wrote in class. Create a driver class called Yahtzee that instantiates and
rolls five six-sided dice objects. Have the dice roll ONCE, then have the computer decide what kind of
“hand” the dice represent. The possible hands to decide on are: 3 of a kind, 4 of a kind, 5 of a kind
(Yahtzee), a Full House (3 of one kind and 2 of another), a small straight (4 dice in consecutive order), a
large straight (5 dice in consecutive order), none of the above (chance). (25 points)
5.
Using the Dice class, design and play a game called Pig. In this game, the user competes against the
computer. On each turn, the player rolls a pair of dice and adds up his or her points. The game is won by
the human or computer based on who reaches 100 points first. If a player rolls a 1, he or she loses all
points for that round and the dice go to the other player. If a player rolls two 1s in one turn, the player
loses all points earned so far in the game and loses control of the dice. The human player may
voluntarily turn over the dice after each roll. So on each possession of the dice a player must decide to
either roll again and risk losing points or give up the dice, possibly letting the other player win. The
Computer player MUST continue to roll until they either roll a 1, two 1s, or exceed a round sum of 20.
(30 points)
Download