Question

1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full decks of 52 cards 2. An instance of FullDeck has only one attribute that is an array of 52 Cards Write a constructor that initializes the array with 52 (unshuf fled) Card objects, assigning a different value to each card. Write a class shuffle that shuffles a decki and a method dealCard that returns a card from the top of the deck. Update the deck Iaccordingly after each deal Part II 3. Write an application HighLowGame that lets the user play the following game: a deck is shuffled and a first card is dealt. The user must guess whether the next card is higher or lower. If the user guesses correctly, the game continues until the user guesses incorrectly. The game is then over and the players score is the number of correct guesses


CS102 : JAVA Object-Oriented Programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.lang.Math;

public class Card {

   static final String[] SUITS = { "Spades", "Hearts", "Diamonds", "Clubs" };
   private int rank;
   private String suit;

   public Card(int rank, String suit) {
       this.rank = rank;
       this.suit = suit;
   }

   public int getRank() {
       return rank;
   }

   public String getSuit() {
       return suit;
   }

   public void setRank(int rank) {
       this.rank = rank;
   }

   public void setSuit(String suit) {
       this.suit = suit;
   }

   public boolean isValid() {
       if ((this.getRank() < 1) || (this.getRank() > 13))
           return false;
       else {
           for (String suit : SUITS) {
               if (this.getSuit().equalsIgnoreCase(suit))
                   return true;
           }
       }

       return false;
   }

   @Override
   public String toString() {
       String str = ((this.rank == 1) ? "Ace"
               : ((this.rank == 11) ? "Jack"
                       : ((this.rank == 12) ? "Queen" : ((this.rank == 13) ? "King" : this.rank))))
               + " of " + this.suit;
       return str;
   }
}

//Deck.java class


public class Deck {
  
   private static final int MAX = 52;
   private Card[] deck;
   public Deck() {
       this.deck = new Card[MAX];
       int count = 0;

       for (String suit : Card.SUITS) {
           for (int i = 0; i < 13; i++) {
               this.deck[count++] = new Card((i + 1), suit);
           }
       }
   }

   @Override
   public String toString() {
       StringBuffer sb = new StringBuffer();
       for (Card card : deck) {
           sb.append(card + "\n");
       }
       return sb.toString();
   }

   public static void main(String args[]) {

       Deck deck = new Deck();
       System.out.println(deck);
   }

}

Sample Output:

a Javadoc Declaration Console 3 <terminated Deck [Java Application] C Program FilesJavaljdk1.8.0_101\bin javaw.exe (Feb 17, 2Jack of Hearts Queen of Heart:s King of Hearts Ace of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds 5 of Diamonds 6 of D

Add a comment
Know the answer?
Add Answer to:
CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one...

    JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one for the suit. 2- Create an array of 52 strings that contain a deck of playing cards. The first 13 items are the cards, whose suit is clubs, the next 13 diamonds, the next 13 hearts and finally the spades. In this step, the array and the order of the cards should be printed. 3- Use the shuffling algorithm you have learnt in the...

  • *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card...

    *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • Using C++: 1. Array and Struct in Card applications: In card game design, card suit and...

    Using C++: 1. Array and Struct in Card applications: In card game design, card suit and face values can be defined with enumeration values. Declare suit values - – hearts, clubs, diamonds, spades using enum, declare face values of 2 – 10, Jack, Queen, King, Ace using enum Declare a structure called Card, and it includes two data members, whose data types are the above two defined enumerations suit and face typedef an array called Deck, and it includes all...

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT