This is a menu-driven Java program that simulates a deck of 52 playing cards. The deck is made up of four suits (clubs, diamonds, hearts, spades) and 13 ranks (Ace, 2-10, Jack, Queen, King). The program is divided into two classes: Card and Deck. Card represents an individual playing card and Deck represents a deck of cards.
To use the program, follow these steps:
- Open a terminal or command prompt.
- Navigate to the directory containing the program files.
- Compile the program by running the command:
javac *.java - Run the program by running the command:
java Main
The program will display a menu of options to choose from:
- Print Deck - Displays the entire deck of cards.
- Print Card - Allows the user to enter a card number (1-52) and displays the corresponding card.
- Same Card - Allows the user to enter a card number (1-52) and displays all other cards in the same suit.
- Compare Card - Allows the user to enter two card numbers (1-52) and displays whether they have the same rank or not.
- Find Card - Allows the user to enter a rank (1-13) and a suit (1-4) and displays the first matching card in the deck.
- Deal Card - Deals five random cards from the deck.
- Shuffle Deck - Shuffles the deck randomly.
- Exit - Exits the program.
The program implements the following methods in the Deck class:
createDeck()- Creates a new deck of 52 cards.printDeck()- Prints the entire deck of cards.printCard(int cardIndex)- Prints the card at the specified index in the deck.sameCard(int cardIndex)- Prints all cards in the same suit as the card at the specified index in the deck.compareCard(int cardIndex1, int cardIndex2)- Prints whether the two cards at the specified indices in the deck have the same rank.findCard(int rank, int suit)- Prints the first card in the deck with the specified rank and suit.dealCard()- Deals five random cards from the deck.shuffleDeck()- Shuffles the deck randomly.
The Card class represents an individual playing card and contains the following properties:
rank- The rank of the card (Ace, 2-10, Jack, Queen, King)suit- The suit of the card (clubs, diamonds, hearts, spades)
The program follows the following coding guidelines:
- Each method is separated into its own function.
- The program uses
ArrayListto store the deck of cards. - The program uses the
Scannerclass to read user input. - The program includes comments to explain the logic of each matters.