CS102 : JAVA Object-Oriented Programming.
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:
CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...
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 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 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 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...
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 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: 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 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 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 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...