Question

Please help we write a java program for this
Write a class for a single Card from a Deck of Cards. Think about what youll need to store in a single card to identify it f

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

CODE

class Card {

private String face, suit;

public Card(String f, String s) {

face = f;

suit = s;

}

public String getFace() {

return face;

}

public void setFace(String face) {

this.face = face;

}

public String getSuit() {

return suit;

}

public void setSuit(String suit) {

this.suit = suit;

}

@Override

public String toString() {

return face + " of " + suit;

}

}

public class Deck {

private Card[] cards;

private static final String[] suits = {"Diamonds", "Hearts", "Clubs", "Spades"};

private static final String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8",

"9", "10", "Jack", "Queen", "King"};

public Deck() {

cards = new Card[52];

int k = 0;

for(int i=0; i<4; i++) {

for(int j=0; j<13; j++) {

cards[k ++] = new Card(faces[j], suits[i]);

}

}

}

public void display() {

for (int i = 0; i<cards.length; i++) {

System.out.println(cards[i]);

}

}

public static void main(String[] args) {

Deck d = new Deck();

d.display();

}

}

OUTPUT

Ace of Diamonds

2 of Diamonds

3 of Diamonds

4 of Diamonds

5 of Diamonds

6 of Diamonds

7 of Diamonds

8 of Diamonds

9 of Diamonds

10 of Diamonds

Jack of Diamonds

Queen of Diamonds

King of Diamonds

Ace of Hearts

2 of Hearts

3 of Hearts

4 of Hearts

5 of Hearts

6 of Hearts

7 of Hearts

8 of Hearts

9 of Hearts

10 of Hearts

Jack of Hearts

Queen of Hearts

King of Hearts

Ace of Clubs

2 of Clubs

3 of Clubs

4 of Clubs

5 of Clubs

6 of Clubs

7 of Clubs

8 of Clubs

9 of Clubs

10 of Clubs

Jack of Clubs

Queen of Clubs

King of Clubs

Ace of Spades

2 of Spades

3 of Spades

4 of Spades

5 of Spades

6 of Spades

7 of Spades

8 of Spades

9 of Spades

10 of Spades

Jack of Spades

Queen of Spades

King of Spades

UML

<<Java Class>> Deck (default package) SF suits: String SFfaces: Stringi Deck() display():void main(String():void -cards 0.. <

Add a comment
Know the answer?
Add Answer to:
Please help we write a java program for this Write a class for a single Card...
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
  • 2. Activity Directions: Create a UML diagram and then write the code for the object classes needed for your UNO game. You will need a minimum of three classes: a. A Card class (models an individual UN...

    2. Activity Directions: Create a UML diagram and then write the code for the object classes needed for your UNO game. You will need a minimum of three classes: a. A Card class (models an individual UNO card) b. A Hand class (models a player's hand) c. A Deck class (models the entire UNO deck) You may add other classes as you see fit. Test your program by writing a console application (a driver program) that creates a deck of...

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

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

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

  • For this project your card class must include an Image of the card. Here is a...

    For this project your card class must include an Image of the card. Here is a zip filecontaining 52 images you can use for your cards. Please note that the name for each card can be created by the concatenation of the first character of the suit, the value of the card, and the ".png" suffix. When you construct a card, you should have it load its image. Alternatives exist regarding how to draw() the card. One way to do...

  • Now, create a Deck class that consists of 52 cards (each card is an instance of...

    Now, create a Deck class that consists of 52 cards (each card is an instance of class Card) by filling in the template below. Represent the suit of cards as a string: "Spades", "Diamonds", "Hearts", "clubs" and the rank of cards as a single character or integer: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", class Deck: def init (self): pass # Your code here. def draw (self): Returns the card at the top of the deck, and...

  • This is a Python Program. Please put the card in a Class and instantiate values just...

    This is a Python Program. Please put the card in a Class and instantiate values just a Python class; Objects And I have an old maid program, and it should create a deck of cards, shuffles it, and gives each player four cards.I need a python class that is instantiated a deck . The Class should have features.

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

  • CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As...

    CARD GAME (LINKING AND SORTING) DATA STRUCTURES TOPIC(S) Topic Primary Linked lists Sorting Searching Iterators As needed Recursion OBJECTIVES Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have a solid foundation before you can build a house). In order to allow creative freedom,...

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

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