Question

Java Question 4

Hello. I am a new student of java class and I struggle below question 4.

Could anyone please help me?

Thank you very much.

Card Deck suit: int value: int deck: Cardl deckSize: int + MAXSize = 52: int Deck() Deck(fullDeck:boolean) -initialiseFullDeck()E void rdrawCard0: Card place Card(card : Card): void 0, 52 +Card(value: int, suit: int) +getvalue): int getSuit): int +toString) : String - +shuffle0: void +getDeckSize) int +hasCardsRemaining0: boolean toString0: String sinterfaces> ComparablesCard> + compare To(other Card) int Snap playerTurn: int DrinkingGame + PLAYER-1-1: int PLAYER 2: int playeri Deck: Deck player2Deck: Deck pile : Arrayluist<Card t moin ors String): void setupplayerDecksQ : void pickupPİle(player: İnt): void checkSnap): boolean +snap(player :int) boolean rdrawCard(player int) Card thasGameFinishedO boolean HisWinner(player: int) boolean getPlaverTurn(0: int +getplayertiardsRemaininglplayerint): int +main(ar Question 3): Deck (10 Marks) Hvoid

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

I have neatly documented the code. Happy learning !!

import java.util.ArrayList;
import java.util.Random;

class Card implements Comparable<Card> {

    int suite;
    int value;

    /*
     *  There would be 4 suits in cards
     *  Let's denote spades as 1
     *  Hearts as 2
     *  Diamonds as 3
     *  Clubs as 4
     *  The above mentioned could be anything. For simplicity let's assume them as priorities.
     */

    /*
     *  Let's only take the values between 2 and 10 inclusive at both ends
     */

    /*
     * So the value for suite can be anything between 1 and 4 inclusive at both ends
     * The value for @value can be anything between 2 and 10 inclusive at both ends
     */

    Card(int suite, int value) {
        this.suite = suite;
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public int getSuite() {
        return suite;
    }

    /*
     *  Comparable is just an interface to customize the checking between two objects
     *  For example if you want to compare two numbers how would you do that ?
     *  You would use your intuition right :D
     *  Now let's say you have to compare two mobiles
     *  You would check various factors like screen size, battery life etc
     *  If both of them have same screen size you would check battery life etc
     *  That is exactly how the Comparable interface works
     *  Just see the below function
     */

    @Override
    public int compareTo(Card o) {
        // If both cards have the same value go and compare their suite !!
        if (this.value == o.value) return this.suite - o.suite;
            // If their values itself are different then just return the smallest one !!
        else return this.value - o.value;
    }
}

public class DrinkingGame {
    public static void main(String[] __) {
        /*
         * Lets use an ArrayList for storing all cards
         * Lets have 52 cards
         */

        ArrayList<Card> deck = new ArrayList<>();

        for (int i = 0; i < 52; i++) {
            int randomSuite = getRandom(1, 4);
            int randomValue = getRandom(2, 10);
            Card card = new Card(randomSuite, randomValue);
            deck.add(card);
        }

        /*
         * Playing the game
         * Remove 2 cards each time until the deck is empty
         */

        int player1 = 0, player2 = 0;

        /*
         * Lets always assign the first drawn card to first player and the second drawn card to player 2
         */

        System.out.println("!!!!! Game has started !!!!!!\n");
        while (!deck.isEmpty()) {


            // Drawing two random cards in the deck
            int randomCard1 = getRandom(0, deck.size());
            Card p1 = deck.get(randomCard1);
            deck.remove(randomCard1);


            int randomCard2 = getRandom(0, deck.size());
            Card p2 = deck.get(randomCard2);
            deck.remove(randomCard2);


            if (p1.compareTo(p2) > 0) {
                // This is just checking if p1 > p2
                player1 += 1;
                System.out.println("Player 1 scored a point");
            } else {
                player2 += 1;
                System.out.println("Player 2 scored a point");
            }
        }
        System.out.println("\n\n");
        System.out.println("!!!!!! Final scores: !!!!!!");
        System.out.println("Player - 1: " + player1);
        System.out.println("Player - 2: " + player2);

        // Comparing the winning score using the terenary operator
        System.out.println("Result: " + ((player1 > player2) ? "Player 1" : "Player 2") + " has won");
    }

    public static int getRandom(int start, int end) {

        /*
         * This function generates a random number between start and end inclusive at both ends
         */
        Random random = new Random();
        int randomNumber = random.nextInt(end - start) + start;
        return randomNumber;
    }
}
Add a comment
Know the answer?
Add Answer to:
Java Question 4 Hello. I am a new student of java class and I struggle below...
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
  • Deck of Cards Full rating for answer Thanks! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// De

    Deck of Cards Full rating for answer Thanks! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Design an object card Design an object Deck Design a driver program Use these class diagrams to create your objects. ------------------------------------- Card ------------------------------------- -int _value -String _suit -String _Color -String _Face ------------------------------------- +Card(int value, String suit) +String toString() +String getColor() +String getFace() +int getValue() +String getSuit() ------------------------------------ ------------------------------------ Deck ------------------------------------ -List _cards ------------------------------------ +Deck() -void loadCards() +Card drawCard() +int getDeckSize() +void shuffle() ------------------------------------ Write a driver program that produces the output...

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

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

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

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

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

  • S12 Five Crowns – Part 2 This week you will continue your work on the Five...

    S12 Five Crowns – Part 2 This week you will continue your work on the Five Crowns program. 1. You will create a class Deck that consists of the following: a. The vector of 116 cards from last week’s program. b. a method to deal a card from the deck 2. Create overloaded operators: a. bool operator == that overloads the == operator to check if two cards are equal. b. bool operator < that overload the < operator and...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • 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