Question

In: Computer Science

write on eclipse java Write a program named lab5 that will play a game of Blackjack...

write on eclipse java

  1. Write a program named lab5 that will play a game of Blackjack between the user and the computer.

Create a second class named Card with the following:

  1. Define the following private instance variables: cardValue (int) & cardSuite (String)
  2. Write a constructor with no parameters that will
      1. Set cardValue to a random number between 1 and 13
      2. Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 – hearts, 1 – diamonds, 2 – spades, 3 – clubs)
      3. Call drawCard
    1. Write a void method named drawCard that will draw a simple version of the card like the example shown (11 will have a J in it for Jack, 12 – Q for Queen, 13 – K for King)

Also print cardSuite

*****                  *****

*   2 *                 *   Q *      etc….

*****                  *****

  1. Write a public int method named getValue (no parameters) that will return cardValue

Back in the main class:

  1. Print the title “User Cards”
  2. Declare and instantiate a Card object named user1
  3. Declare and instantiate a Card object named user2
  4. Print the title “Computer Cards”
  5. Declare and instantiate a Card object named computer1
  6. Declare and instantiate a Card object named computer2
  7. Call the findWinner method, sending user1, user2, computer1 & computer2 as parameters.

  1. After the main method (but still inside the class) write a public static void method named findWinner
    1. It should have 4 parameters of type Card – u1, u2, c1, c2
    2. Add the 2 cards for the user and get the sum. If the sum is greater than 21, set it to 0. (Use the getValue method to get the value of each card object.)
    3. Do the same thing for the 2 computer cards.
    4. Compare the user sum to the computer sum and print a statement to show who won or if it was a tie. (The player with the highest score wins.)

Solutions

Expert Solution

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!

Related Solutions

Play a blackjack. Write a Java program that starts from a deck of 52 cards and...
Play a blackjack. Write a Java program that starts from a deck of 52 cards and one player plays again a dealer. Use a simple rule explained here. Ace can be counted as 1 only for simplicity. Jack, Queen, King are counted as 10. At the beginning, player receives two cards. Dealer receives two cards, but shows one and hides one. Program asks player if player wants to receive another card or not. (player can continue to receive new cards...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT