Homework Help Question & Answers

CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

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.

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 player's score is the number of correct guesses
0 0
Report
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

Know the answer?
Add Answer to:
CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...
Your Answer: Your Name: What's your source?
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
  • 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...

  • 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...

  • 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...

  • Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriat...

    Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...

  • need help with java

    Write classes named Card and Deck. The card class will represent a single playing card and the Deck class will create a card using a Random object (we'll leaveshuffling a list of cards for another day).Card should have the following instance variables and methods:Card- suit: int- value: int+ Card()+ Card(suit: int, value: int)+ getValue(): int+ getSuit(): int+ toString(): StringThe suit can be represented as 0 for spades, 1 for clubs, 2 for diamonds and 3 for hearts.The value can be...

  • A Java Problem. E5.19 Write a program that takes user input describing a playing card in...

    A Java Problem. E5.19 Write a program that takes user input describing a playing card in the following shorthand notation: А Ace 2... 10 Card values Jack Queen King Diamonds Hearts Spades Clubs Your program should print the full description of the card. For example, Enter the card notation: QS Queen of Spades Implement a class Card whose constructor takes the card notation string and whose getDescription method returns a description of the card. If the notation string is not...

  • While using JAVA , solve this. There are three basic classes we'll need this week: Card:...

    While using JAVA , solve this. There are three basic classes we'll need this week: Card: A class like the one presented in the modules, but with a few changes. Hand: A class that represents the cards held by a single player. Deck: A class that represents the source of the cards for dealing and, as the game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck" or when future hands...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

Free Homework 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.