Write the steps or thing you would do before to writing this
Write the steps or thing you would do before to writing this program
Write a program that will produce output similar to the output below:
User: hearts 8
User Totals: 8
Computer: spades 2
Computer Totals: 2
User: hearts 4
User Totals: 12
Computer: spades 8
Computer Totals: 10
User: hearts 1
User Totals: 13
Computer: spades 10
Computer Totals: 20
User: hearts 3
User Totals: 16
Computer: spades 4
Computer Totals: 24
User won because computer went over 21
Other messages that could be displayed are:
\"User won this round.\"
\"Computer won this round.\"
\"Computer won because user went over 21\"
\"User won because computer went over 21\"
“Both lost since both user and computer totals went over 21\"
To accomplish this, you will need to create 2 classes, a domain Card class, and a driver class.
The Card class will have the following 2 instance variables:
private String suit; //Clubs, Diamonds, Hearts, Spade
private int cardValue; // 1 - 11;
The Card class will also contain the following:
Constructor, receiving card value and card name as input parameters.
Setters & Getters for each of the 2 instance variables (cardValue & suit)
toString – to display content of card, including card suit and card value.
The Driver class will have the following global variables:
public static int userTotals = 0;
public static int computerTotals = 0;
public static Random myRan = new Random();
The Driver class will also have the following methods:
Main method, which will call 2 methods inside of a loop:
//A loop in main will include these 3 statements:
drawUserCard();
drawComputerCard();
System.out.println(\"\ \");
After the loop in main finishes, there will be a set of if-else-if statements to determine whether the user won, the computer won, or both were tied, or both lost. Here’s the beginning of the if-statements needed:
if (userTotals > computerTotals && userTotals <= 21)
{
System.out.println(\"User won this round.\");
}
else if …
As a result of all the if-statements, each of the following error messages should be displayed:
“User won because computer went over 21”
\"User won this round.\"
\"Computer won this round.\"
\"Computer won because user went over 21\"
\"User won because computer went over 21\"
“Both lost since both user and computer totals went over 21\"
Solution
Program:
import java.util.Random;
import java.util.Scanner;
public class BlackJack {
public static int userTotals = 0;
public static int computerTotals = 0;
public static Random myRan = new Random();
public static void main(String[] args)
{
while (userTotals<21 || computerTotals<21)
{
drawUserCard();
drawComputerCard();
System.out.println(\"\ \");
}
if (userTotals > computerTotals && userTotals <= 21)
{
System.ou
t.println(\"User won this round.\");
}
else if (computerTotals > userTotals && computerTotals <= 21)
{
System.out.println(\"Computer won this round.\");
}
else if (userTotals > computerTotals && userTotals > 21)
{
System.out.println(\"Computer won because user went over 21\");
}
else if(computerTotals > userTotals && computerTotals > 21)
{
System.out.println(\"User won because computer went over 21\");
}
else if(computerTotals > 21 && userTotals > 21)
{
System.out.println(\"Both lost since both user and computer totals went over 21\");
}
}
public static void drawUserCard()
{
int userCard = myRan.nextInt(11) + 1;
int cardSuite = myRan.nextInt(4) + 1;
String suiteName;
if (cardSuite == 1)
suiteName = \"clubs\";
else if (cardSuite == 2)
suiteName = \"hearts\";
else if (cardSuite == 3)
suiteName = \"diamonds\";
else
suiteName = \"spades\";
Card myCard = new Card(suiteName, userCard);
System.out.println(\"User Card: \" + myCard);
userTotals += userCard;
System.out.println(\"User Total: \" + userTotals);
}
public static void drawComputerCard()
{
int computerCard = myRan.nextInt(11) + 1;
int cardSuite = myRan.nextInt(4) + 1;
String suiteName;
if (cardSuite == 1)
suiteName = \"clubs\";
else if (cardSuite == 2)
suiteName = \"hearts\";
else if (cardSuite == 3)
suiteName = \"diamonds\";
else
suiteName = \"spades\";
Card myCard = new Card(suiteName, computerCard);
System.out.println(\"Computer Card: \" + myCard);
computerTotals += computerCard;
System.out.println(\"Computer Total: \" + computerTotals);
}
}
public class Card
{
private String suit;
private int cardValue; public Card (String aSuit, int aCardValue)
{
suit=aSuit;
cardValue=aCardValue;
}
public String toString()
{
return \"Suit \" + suit + \"\ \" + \"Card Value \" + cardValue;
}
}
Sample Output:
User Card: Suit diamonds
Card Value 9
User Total: 9
Computer Card: Suit clubs
Card Value 3
Computer Total: 3
User Card: Suit diamonds
Card Value 4
User Total: 13
Computer Card: Suit diamonds
Card Value 5
Computer Total: 8
User Card: Suit spades
Card Value 1
User Total: 14
Computer Card: Suit clubs
Card Value 11
Computer Total: 19
User Card: Suit spades
Card Value 5
User Total: 19
Computer Card: Suit spades
Card Value 10
Computer Total: 29
User Card: Suit diamonds
Card Value 3
User Total: 22
Computer Card: Suit diamonds
Card Value 4
Computer Total: 33
User won because computer went over 21





