辅导java Assignment、java Blackboard assignment辅导、辅导java程序

- 首页 >> Java编程


Assignment 1

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)

SlotMachine

- generator : Random

Using the following UML, create a class called SlotMachine

- tokenCredit : int

that simulates a casino slot machine in which three +SlotMachine()

numbers between 0 and 9 are randomly selected and +topupTokens(tokens:int) : void

printed side by side. This is done using the “pullLever” +cashoutTokens() : int

method which is overloaded, a default method which uses +pullLever() : void

one token, and a parameter method which can specify +pullLever(tokenInput:int) : void

how many tokens to gamble for that single pull. There are +getTokenBalance():int

five situations which occur when the lever is pulled. +main(args:String[]) void

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

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])

Card

- suit : int

- value : int

0,..,52

+Card(value : int, suit : int)

+getValue() : int

+getSuit() : int

+toString() : String

<<interface>>

Comparable<Card>

+ compareTo(other : Card) : int

1

DrinkingGame

+ main(args : String) : void

Question 3) : Deck (10 Marks)

Deck

- deck : Card[]

- deckSize : int

+ MAX_SIZE = 52: int

+Deck()

+Deck(fullDeck:boolean) -initialiseFullDeck() : void

+drawCard() : Card

+placeCard(card : Card) : void

1 +shuffle() : void

+getDeckSize() : int

+hasCardsRemaining() : boolean

+toString() : String

2

1

Snap

- playerTurn : int

+ PLAYER_1 = 1: int

+ PLAYER_2 = 2: int

- player1Deck : Deck

- player2Deck : Deck

- pile : ArrayList<Card>

+Snap() - setupPlayerDecks() : void

- pickupPile(player:int) : void

- checkSnap() : boolean

+snap(player : int) : boolean

+drawCard(player : int) : Card

+hasGameFinished() : boolean

+isWinner(player : int) : boolean

+getPlayerTurn() : int

+getPlayerCardsRemaining(player:int) :int

+main(args: String[]) : void)

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.

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


站长地图