Question

*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 high-card points

d. writeHandOutput – input a bridge-hand and prints it out on the monitor

e. writeDeckOuput – inputs a deck of bridge cards and prints it out on the monitor main method should:

a. Ask the user what kind of deck he/she wants to create (pinochle or bridge). For this project, have the user input ‘bridge’.

b. Create the user-asked-for deck

c. Print out the unshuffled deck

d. Shuffle the deck

e. Create 4 Bridge hands of 13 cards each, call them North, South, East and West. + Deal every fourth card to each of the hands

f. Calculate the high-card-points in each hand

g. Print out each hand and the number of high-card-points associated with each hand

h. Sort the hands from highest to lowest number of points – use Insertion Sort we did before

i. Print out each hands points from highest to lowest.

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

import java.util.*;

class Card{

    String[] rank;

    String[] suit;

    int getRank(String s){  //To return highest rank for high-card used for sorting

        switch(s){

            case "A": return 1;

            case "K": return 2;

            case "Q": return 3;

            case "J": return 4;

            case "10": return 5;

            case "9": return 6;

            case "8": return 7;

            case "7": return 8;

            case "6": return 9;

            case "5": return 10;

            case "4": return 11;

            case "3": return 12;

            case "2": return 13;

        }

        return 0;

    }

    void createDeck(String type){

        if(type.equals("bridge")){

            rank = new String[] {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

            suit = new String[] {"Diamond", "Club", "Heart", "Spade"};

        }

        else{

            rank = new String[] {"A", "A", "10", "10", "K", "K", "Q", "Q", "J", "J", "9", "9"};

            suit = new String[] {"Diamond", "Club", "Heart", "Spade"};

        }

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

            for(int j=0;j<suit.length;j++){

                System.out.println(rank[i] + " " + suit[j]);

            }

        }

    }

    void swap(String[] deck, int i, int j){

        String temp = deck[i];

        deck[i] = deck[j];

        deck[j] = temp;

    }

    String[] shuffleDeck(String[] deck){

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

            swap(deck, i, (2*i+5)%52);

        }

        return deck;

    }

    int countBridgePoints(String[] bridgeHand){

        int res = 0;

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

            if(bridgeHand[i].equals("A") || bridgeHand[i].equals("K") || bridgeHand[i].equals("Q") || bridgeHand[i].equals("J")){

                res++;  //if card has either of them, increment count by 1 because its high card deck

            }

        }

        return res;

    }

    void writeHandOutput(String[] bridgeHand){

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

            System.out.print(bridgeHand[i] + " ");

        }

        System.out.println();

    }

    void writeDeckOuput(String[] deck){

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

            System.out.print(deck[i] + " ");

        }

        System.out.println();

    }

}

class CardTest{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter type of deck (bridge or pinochle): ");

        String type = sc.next();    //Input type of card

        Card card = new Card();

        card.createDeck(type);  //Create deck of that type

        System.out.println("Enter the un-shuffled deck of size 52:");

        String deck[] = new String[52];

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

            deck[i] = sc.next();    //Input un-shuffled array of deck from user

        }

        card.writeDeckOuput(deck);  //Print un-shuffled deck

        deck = card.shuffleDeck(deck);  //Shuffle the deck

        String[] north = new String[13];    //

        String[] south = new String[13];

        String[] east = new String[13];

        String[] west = new String[13];

        int in = 0, t = -1, p = -1;

        while(in < 13){

            t += 4;

            north[in] = deck[t%52];

            t += 4;

            south[in] = deck[t%52];

            t += 4;

            east[in] = deck[t%52];

            t += 4;

            west[in] = deck[t%52];

            if(t > 51){

                t = ++p;

            }

            in++;

        }

        int northHand = card.countBridgePoints(north);

        int southHand = card.countBridgePoints(south);

        int eastHand = card.countBridgePoints(east);

        int westHand = card.countBridgePoints(west);

        System.out.println("North Hand:");

        card.writeHandOutput(north);

        System.out.println(northHand);

        System.out.println("South Hand:");

        card.writeHandOutput(south);

        System.out.println(southHand);

        System.out.println("East Hand:");

        card.writeHandOutput(east);

        System.out.println(eastHand);

        System.out.println("West Hand:");

        card.writeHandOutput(west);

        System.out.println(westHand);

        int[] points = {northHand, southHand, eastHand, westHand};

        for (int i = 1; i < points.length; i++)

        {  

            int key = points[i];  

            int j = i - 1;  

            while (j >= 0 && points[j] < key)

            {  

                points[j + 1] = points[j];  

                j = j - 1;  

            }  

            points[j + 1] = key;  

        }

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

            System.out.print(points[i] + " ");

        }

    }

}

output:

Screenshot program:

Add a comment
Know the answer?
Add Answer to:
*in Java 1. Create a card class with two attributes, suit and rank. 2.In the 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
  • 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...

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

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...

  • JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one...

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

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

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

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • Write a Java program that deals a five-card poker hand and then determines which of the...

    Write a Java program that deals a five-card poker hand and then determines which of the following hands are contained in it: high card, pairs, two pair, three of a kind, flush. Test only for those hands. Use the numbers from 0 to 51 to represent the cards of the poker deck. 1. To deal the cards, your main method should call a secondary method that selects a random card. The secondary method should accept 5 integers that represent the...

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

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