Question

In java How to get started; Create a class called Card. The card has three fields;...

In java

How to get started;

Create a class called Card. The card has three fields; a value , a suit and a face.

The card class has a constructor that takes three values for the three fields.

Create a no-args constructor.
The Card class has three get methods to return the values of each of the fields.

The Card class has a toString( ) method.

The Card class has a compareTo( ) method that uses the value of the card to determine the ordering.

Create an equals( ) method that returns true if the parameter Card has the same suit and the same face as the invoking Card.

Create a class called Deck. A deck has two fields- an array of Cards and an int which holds the subscript of the Card at the top of the deck.
The Deck class will have only one constructor, which will create the array. Try to think of a way to code this without writing 52 statements. (not required)

The Deck class also has a method getTopCard( ) which returns a copy of the Card that is at the top of the array. Notice this means the Card class needs a copy constructor.


The Deck class has a method called shuffle( ) which shuffles the array of Cards. To shuffle, generate random numbers and use these to exchange array elements. Exchange 1000 times in this method.

Display a test of your Card class and Deck class in a main( ) that

Prints the entire Deck before shuffling, prints the Deck after a shuffle and

Draws and prints the top two Cards.

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

Programming Language:java

File Name:Deck.java

As u mentioned The Deck class will have only one constructor, which will create the array. Try to think of a way to code this without writing 52 statements. (not required) in Question I didn't code for this section.

Sourcecode:

import java.util.*

public class Card{
int value=new int();
String suit=new String();
int face= new String();
public Card(int v,String s,String f)
{
this.value=v;
this.face=f;
this.suit=s;
}
public Card(){
this.value=0;
this.face="";
this.suit="";
}
public int getvalue(){
return value;
}
public int getsuit(){
return suit;
}
public int getface(){
return face;
}
public String toString(int value,String suit){
if (suit == JOKER) {
if (value == 1)
return "Joker";
else
return "Joker #" + value;
}
else{
String s;
String va;
switch ( suit ) {
case SPADES: s="Spades";
case HEARTS: s="Hearts";
case DIAMONDS: s="Diamonds";
case CLUBS: s= "Clubs";
default: s= "Joker";
}
switch (value) {
if ( suit== "JOKER")
return "" + value;
else {
switch ( value ) {
case 1: va="Ace";
case 2: va= "2";
case 3: va= "3";
case 4: va= "4";
case 5: va= "5";
case 6: va= "6";
case 7: va= "7";
case 8: va= "8";
case 9: va= "9";
case 10: va= "10";
case 11: va= "Jack";
case 12: va= "Queen";
default: va= "King";
}
}
retrun s +" of "+va;

}
public String compareTo(Card va){
//determines the order of the cards with the help of card value

}
public boolean equals(Card c){
if(this.suit==c.suit && this.face==c.face)
return true;
else
return false;

}
}
public class Deck{
Card arr[]=new Card();
int subscript=new int();
public Card Deck(){
//in question said that this method is not required
}
public Card getTopCard(){
return Card.arr[arr.length-1];//returns the top card of the array card
}
public void shuffle()
{
for ( int i = arr.length-1; i > 0; i-- ) {
int ran = (int)(Math.random()*(i+1));
Card tem = arr[i];
arr[i] = arr[ran];
arr[rand] = tem;
}
}
public static void main(String[] args) {
for(int i=0;i<arr.length;i++){
System.out.println(Deck.arr[i]);
}
Deck d=new Deck();
d.shuffle();
for(int i=0;i<arr.length;i++){
System.out.println(d.arr[i]);
}
System.out.println("First Top Card "+d.arr[arr.length-1]);
System.out.println("Second Top Card"+d.arr[arr.length-2]);
};

}

In Addition You have to add Card data to do further computation.

Add a comment
Know the answer?
Add Answer to:
In java How to get started; Create a class called Card. The card has three fields;...
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 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...

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

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

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

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

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

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

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

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