Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Standard playing cards (Links to an external site.)Links to an external site.come in a deck of 52 different cards, where each card has a suit

Standard playing cards (Links to an external site.)Links to an external site.come in a deck of 52 different cards, where each card has a suit and a value. Possible suits are:

Spades

Hearts

Diamonds

Clubs

And possible values are:

The numbers 2-10

Jack

Queen

King

Ace

In the game Blackjack (Links to an external site.)Links to an external site., players are initially dealt two standard playing cards. The goal of the game is to get a collection of cards that is as close to 21 points as possible (without exceeding 21 points). Cards are assigned point values as possible:

Cards numbered 2-10: the points equals the cards number (so a 4 of Clubs is worth 4 points)

"Face" cards (Jack, Queen, or King): 10 points each

Ace: 11 points or 1 point (whichever makes a better score)

There is more to the game of Blackjack, but that's all the information we will need for this project.

Description

In this assignment, you will finish a Java program that plays a portion of a Blackjack game. You will "deal" two standard playing cards and calculate their point total. If an Ace is dealt, you should initially count it as 11 points. If the user is dealt two Aces, you should adjust to treat one of the Aces as 1 point (instead of 11) so that the score is 12 points instead of 22 points (since 22 points is over the limit).

Here a three sample runs of the program (note that there is no user input):

Sample 1:

Card 1: 5 of Spades Card 2: King of Clubs Total points: 15

Sample 2:

Card 1: Ace of Clubs Card 2: Queen of Diamonds Total points: 21

Sample 3:

Card 1: Ace of Spades Card 2: Ace of Hearts Total points: 12

Random Numbers

We will simulate dealing playing cards by generating random numbers. Java has a random number generator tool that will randomly generate integers. First, we create a random number generator:

Random r = new Random();

Then, we can use our random number generator (which I called r) to randomly generate numbers from 0 up to but not including a given bound. For example, num below is randomly 0,1,2,3,4,5, or 6:

int num = r.nextInt(6);

Since a playing card is just one of four suits and one of 13 values, we can do something like this to generate a card:

int randSuit = r.nextInt(4); int randValue = r.nextValue(13);

Then, we just need to assign a suit to each possible randSuit values above (maybe 0=Spades, 1=Hearts, 2=Dimaonds, 3=Clubs) and a value to each possible randValue values above (maybe 0=Jack, 1=Queen, 2-10=2-10, 11=Queen, 12=Ace).

The random number generation of this project is already done for you in the started project file (linked in "Requirements" below). To generate two playing cards, we generated four numbers (two suits and two values).

Requirements

This program should contain a single class (called Proj5). This project has been started for you -- you will need to download Proj5.javaimage text in transcribed from Canvas. Copy all the text in that file, and when you create a new class in BlueJ, paste the code in the BlueJ file. Do not change any of the completed code from that file.

This project contains several methods, each of which contains documentation on what that method does or is supposed to do. You will need to complete the following methods:

public static String getSuit(int randSuit)

public static String getValue(int randValue)

public static int getPoints(String value)

public static void main(String[] args)

I suggest completing the methods in the order listed above.

Use this code to start out the project and fill it in!

import java.util.*;

public class Proj5 {

public static void main(String[] args) {

Random r = new Random();

//Card 1

//randS1 will randomly be between 0-3 (inclusive)

int randS1 = r.nextInt(4);

//randV1 will randomly be between 0-13 (inclusive)

int randV1 = r.nextInt(13);

//YOU DO THIS

//Call getSuit to get card 1's suit. Pass it randS1, and store the returned value in a String variable

//Call getValue to get card 1's value. Pass it randV1, and store the returned value in a String variable

//Print the value of Card 1 using the format in the project description

//Card 2

//randS2 will randomly be between 0-3 (inclusive)

int randS2 = r.nextInt(4);

//randV2 will randomly be between 0-13 (inclusive)

int randV2 = r.nextInt(13);

//YOU DO THIS

//Call getSuit to get card 2's suit. Pass it randS2, and store the returned value in a String variable

//Call getValue to get card 2's value. Pass it randV2, and store the returned value in a String variable

//Print the value of Card 2 using the format in the project description

//YOU DO THIS

//print a blank line

//Call getPoints to get the points for card 1. Pass it value1, and store the returned value in an int variable

//Call getPoints to get the points for card 2. Pass it value2, and store the returned value in an int variable

//Calculate the total points by adding together the points for each card

//If your point total is more than 21, and if either of your cards were an Ace, subtract 10 from the total points.

//This will count the Ace as a 1 instead of as an 11.

//Print your total points so your output looks like the project description

}

/**

* getSuit returns a card suit associated with the given random number

*

* @param randSuit a random number between 0-3

* @return one of the four playing card suits

*/

public static String getSuit(int randSuit) {

//YOU DO THIS

//If randSuit is 0, return "Spades"

//If it is 1, return "Hearts"

//If it is 2, return "Diamonds"

//If it is 3, return "Clubs"

//if needed, you can leave the following line as a default return value :

return "Invalid";

}

/**

* getValue returns a card value associated with the given random number

*

* @param randValue a random number between 0-12

* @return one of the 13 playing card values, as a string

*/

public static String getValue(int randValue) {

//YOU DO THIS

//If randValue is 0, return "Jack"

//If it is 1, return "Queen"

//If it is 11, return "King"

//If it is 12, return "Ace"

//If it is none of the above, it must be between 2-10

//You want to return randValue as a String, like this:

//return randValue+"";

//you can remove the line below after finishing the method:

return "Invalid";

}

/**

* getPoints gets the points associated with a card value

*

* @param value A playing card value, as a String

* @return The points associated with that card value (always gives 11 for an Ace)

*/

public static int getPoints(String value) {

//YOU DO THIS

//If value is either "Jack", "Queen", or "King", return 10

//If value is "Ace", return 11

//If value is none of the above, value must be 2-10 as a string

//You need to convert value to an integer (use Integer.parseInt)

//and return the result.

//You can remove the line below after finishing the method:

return 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

More Books

Students also viewed these Databases questions

Question

=+31-5 Explain how sensory memory works.

Answered: 1 week ago

Question

5. Identify the logical fallacies, deceptive forms of reasoning

Answered: 1 week ago

Question

6. Choose an appropriate organizational strategy for your speech

Answered: 1 week ago