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...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
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...
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
Twenty One This game is similar to the famous card game blackjack. We will play a...
Twenty One This game is similar to the famous card game blackjack. We will play a one-player version of the game. The game is played for some number N of rounds (we will use N = 10,000), at the end of which the player wins points. The player accumulates points during the whole game, and the objective is, of course, to end up with the maximum number of points. The objective in each round of the game is to score...
Write a JAVA program in eclipse (write comment in every line) that asks the user to...
Write a JAVA program in eclipse (write comment in every line) that asks the user to enter two numbers and an operator (+, -, *, / or %), obtain them from the user and prints their sum, product, difference, quotient or remainder depending on the operator. You need to use OOP techniques (objects, classes, methods….). This program must be place in a loop that runs 3 times.
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT