In: Computer Science
write on eclipse java
Create a second class named Card with the following:
Also print cardSuite
***** *****
* 2 * * Q * etc….
***** *****
Back in the main class:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Card.java
public class Card {
        // attributes
        private int cardValue;
        private String suitValue;
        // constructor
        public Card() {
                // assigning an int between 1 and 13 to cardValue
                cardValue = (int) (Math.random() * 13) + 1;
                // generating an int between 0 and 3
                int s = (int) (Math.random() * 4);
                // based on this value, assigning a suit to suitValue
                if (s == 0)
                        suitValue = "hearts";
                else if (s == 1)
                        suitValue = "diamonds";
                else if (s == 2)
                        suitValue = "spades";
                else
                        suitValue = "clubs";
        }
        // method to draw the card
        public void drawCard() {
                // storing cardValue in a string
                String cardStr = "" + cardValue;
                // if card value is 11, 12 or 13, using J, Q, K respectively, instead of
                // rank number
                if (cardValue == 11) {
                        cardStr = "J";
                } else if (cardValue == 12) {
                        cardStr = "Q";
                } else if (cardValue == 13) {
                        cardStr = "K";
                }
                // displaying card
                System.out.println("*****");
                System.out.printf("*%2s *\n", cardStr);
                System.out.println("*****");
                // displaying suit
                System.out.println(suitValue + "\n");
        }
        // method returning value
        public int getValue() {
                return cardValue;
        }
}
//Lab5.java
public class Lab5 {
        //method to find winner given two user cards and two computer cards
        public static void findWinner(Card u1, Card u2, Card c1, Card c2) {
                //finding score of user
                int userScore = u1.getValue() + u2.getValue();
                //if sum is greater than 21, resetting sum to 0
                if (userScore > 21) {
                        userScore = 0;
                }
                //repeating same for computer
                int compScore = c1.getValue() + c2.getValue();
                if (compScore > 21) {
                        compScore = 0;
                }
                //displaying scores
                System.out.println("User score: " + userScore + ", Computer score: "
                                + compScore);
                //finding and displaying winner
                if (userScore > compScore) {
                        System.out.println("User wins!");
                } else if (userScore < compScore) {
                        System.out.println("Computer wins!");
                } else {
                        System.out.println("Its a tie!");
                }
        }
        public static void main(String[] args) {
                System.out.println("User Cards");
                //creating two Cards for user and printing it
                Card user1 = new Card();
                Card user2 = new Card();
                user1.drawCard();
                user2.drawCard();
                System.out.println("Computer Cards");
                //creating two Cards for computer and printing it
                Card computer1 = new Card();
                Card computer2 = new Card();
                computer1.drawCard();
                computer2.drawCard();
                //finding winner
                findWinner(user1, user2, computer1, computer2);
        }
}
/*OUTPUT*/
User Cards
*****
* J *
*****
clubs
*****
* 9 *
*****
clubs
Computer Cards
*****
* 5 *
*****
clubs
*****
* 5 *
*****
hearts
User score: 20, Computer score: 10
User wins!