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