Programming 2: Assignment 1
Due: Monday 20 August 2018
Please submit your code to Blackboard under the correct assignment folder by 11:59pm on the due
date. Place all Java source code (Classes with extension .java) into a folder with your name and student
id eg: Jack_Bloggs_1293849 and zip it up (Jack_Bloggs_1293849.zip).
Question 1) : Slot Machine (15 marks)
Using the following UML, create a class called SlotMachine
that simulates a casino slot machine in which three
numbers between 0 and 9 are randomly selected and
printed side by side. This is done using the “pullLever”
method which is overloaded, a default method which uses
one token, and a parameter method which can specify
how many tokens to gamble for that single pull. There are
five situations which occur when the lever is pulled.
1) {0,0,0} Super Jackpot, all zeros. Token input is multiplied by a factor of 500 and added to the
machine token credit. Should output suitable “Super Jackpot Winner” message showing
winnings.
2) {X,X,X} Three number win. Where X is a value 1-9, token input is multiplied by a factor of 50
and added to the machine token credit. Should output suitable “Jackpot Winner” message
showing winnings.
3) {X,X,Z} Free spin. Where X and Z are values 0-9 and Z≠X, token input is added back into the
machine token credit. Should output suitable “Free Spin” message.
4) {X,Y,Z} Bad luck, Where X,Y,Z are values 0-9 and Z≠Y≠X, token input subtracted from machine
token credit. Should output suitable “Bad Luck, Try again” message.
5) Insufficient Token Balance, where there is not enough tokens in machine to pullLever using
the token input amount.
A user can also top up their machine token credit, and cashout tokens. Add a suitable main method
which tests the class. It should use Scanner to repeatedly allow user to input tokens, and pull lever
using the desired amount of tokens they wish gamble with. The user can exit the machine and “cash
out” once they have done playing. Use suitable output to show user what is happening in the class
and repeatedly show the current machine token balance.
Add another variable called houseCredit, which is used to keep track of the total amount of tokens
inserted across all instances of SlotMachine, add another method called getHouseCredit, which can be
used to obtain this information. Show the changes in main in a suitable way, a user should be able to
jump across different slot machines, until they choose to leave the casino.
Question 2) : Card (5 marks)
Using the following UML, create a class called Card, which represents single object in a pack of playing
cards. It holds a value 1-13 inclusive, which represents {Ace, 2, 3, … , Jack, Queen, King} and a suit 1-4
inclusive (Spades, Clubs, Diamonds, Hearts} which is given when a Card is constructed (ensure that
SlotMachine
- generator : Random
- tokenCredit : int
+SlotMachine()
+topupTokens(tokens:int)
+cashoutTokens() : int
+pullLever() : void
+pullLever(tokenInput:int) : void
+getTokenBalance():int
+main(args:String[]) void
these values can’t be set with invalid numbers). It has suitable “getter” methods which retrieve this
information and a toString method (feel free to also use private helper methods), which converts the
value and suit to a suitable output String (eg: if value=1, suit=2 then toString will return [AC] if value=4,
suit=4 toString returns [4H], if value=12, suit=1 then toString returns [QS])
Question 3) : Deck (10 Marks)
Using the above UML, create a class called deck which encapsulates an array of Card objects. It has
two constructors, one which takes a boolean to indicate whether the Deck should be initialized with
the full 52 cards, or initialised with an empty Deck. The default constructor initializes with a full deck.
It has a variable called deckSize which keeps how many cards are currently in the deck. It has methods
drawCard which returns the top Card on the Deck or placeCard which places a Card back on top of the
Deck. The class also has a shuffle method which randomly shuffles the cards using a suitable algorithm.
Add a suitable toString which simply returns the entire Deck of Card objects as a String.
Card
- suit : int
- value : int
+Card(value : int, suit : int)
+getValue() : int
+getSuit() : int
+toString() : String
Deck
- deck : Card[]
- deckSize : int
+ MAX_SIZE = 52: int
+Deck()
+Deck(fullDeck:boolean)
-initialiseFullDeck() : void
+drawCard() : Card
+placeCard(card : Card) : void
+shuffle() : void
+getDeckSize() : int
+hasCardsRemaining() : boolean
+toString() : void
<<interface>>
Comparable<Card>
+ compareTo(other : Card) : int
2
Question 4) : DrinkingGame and Card modification (5 marks)
Make changes to the Card class so that it implements the Comparable interface. Two Card objects can
be compared using its underlying value (assuming Ace is lowest, King is highest). If the values are the
same then instead compare suit (assuming Spades lowest and Hearts highest). Create a class called
DrinkingGame intended to be played with two people, which simply has a main method. It should use
a full deck of shuffled cards, continuously drawing two Cards, comparing them and outputting which
card was higher until the Deck is empty. A person with the lowest Card must drink the shot by
prompting a message to the console. Use two counters which keep track of how many shots each
player consumed and output them once the Deck is empty.
Question 5) : Snap (15 marks)
Using the above UML, create a class called Snap which is a card game intended for two players. To
play Snap, each player starts off with half a Deck of Cards each. The players take turns placing a Card
into a centre pile. If two card values match, then any player can call “Snap!”. If the two cards indeed
match value (ignoring suit) then the player who called snap first picks up the cards from the centre
pile shuffling them back into their Deck. If there is an incorrect call for “Snap!” then the opposing
player instead gets to pick up the cards. A player wins if the opposing player runs out of cards and can
no longer draw a Card from their Deck.
Create a main method which uses Snap methods to play a game of Snap between two players. It
should create a single Snap instance by invoking its constructor. The constructor needs to set the initial
state of the game by creating two Deck instances for each player (given by fields player1Deck and
player2Deck, each initially containing 26 Card objects). The constructor also initializes an ArrayList
instance of Card objects which represent the centre pile in which players will place Card objects from
their associated Deck instances. Use the setupPlayerDecks helper method to help achieve this.
The main method should constantly ask for input from Scanner using four keys, two for each player
whom can call draw and snap.
- A player can draw a card (if it is their turn) by using the drawCard method. This draws a Card
from the particular players deck (either player1Deck or player2Deck depending on method
parameter) and places the Card on the center pile. The method returns a reference to the
Card placed on the pile which should be outputted to the console.
- A player can call Snap (any time) by calling the snap method (which returns whether snap was
successful or not). If the snap was successful (using helper method checkSnap) then the pile
(ArrayList) of Card objects are placed back into the players Deck (using helper method
pickupPile), then shuffled. If the snap was unsuccessful then the opposing player gets to
pickup the pile.
The game continues until any player cannot draw a Card anymore as they have run out of Cards in
their deck (determined by hasGameFinished method). The winner is given by the isWinner method
which checks the player parameter to see if the opposing player has no Card objects left in their deck
(possible that both players ran out of cards).
Use suitable output showing the progress and state of the current game of Snap being played.
TOTAL: 50 marks
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。