8:56 PM ..ooo Sprin LTE tps:// ccc-wy.desire2learn.com To do this week 4 Create
ID: 3793939 • Letter: 8
Question
8:56 PM ..ooo Sprin LTE tps:// ccc-wy.desire2learn.com To do this week 4 Create a program that wilrandomly pick two cards (ignore the suit. and tell the user what thetotalisasif they are playing blackjack. Here are the rules: Aces are either worth 1 or 11 10's and all face cards are 10 All other cards are their value for example, a 6 is worth 6) Declare two int variables, one for each of the two cards. Then determine the total value of the two cards as followi: First, consider just the first card scored as above. HitisanAce.count that as 11. Next, consider the second card also as above but ifit is an Ace, and the total would be over 21 countitas 1. Output the two cards and the total to the user, To determine a random card create arandom number betweenland13.1 isan Ace, 11 aJack 12 queen and 13a king All other numbers are their face value. When outputting the cards and calculating the total, you wilneedtwo multi-way blocks, each with several options (one each for the face cards, one for the ace, and for the rest). Here are some sample outputs: You got a Ace and a 2 for a total of 13 You got a King and a 7 for a total of 17 You got a 5 and a 7 for a total of 12 total of 2 You Rot a Ace and a King for aExplanation / Answer
Code :
1. BlackJack.java
package CardGame;
public class BlackJack {
public int getCard1() {
return card1;
}
public void setCard1(int card1) {
this.card1 = card1;
}
public int getCard2() {
return card2;
}
public void setCard2(int card2) {
this.card2 = card2;
}
private int card1;
private int card2;
public String play() {
int num1, num2, ace1 = 0, ace2 = 0;
String output = "";
num1 = (int)(Math.random()*13)+1;
num2 = (int)(Math.random()*13)+1;
if(num1 == 1) {
output += "You got a Ace and ";
ace1 = 11;
} else if(num1 > 10) {
if(num1 == 11) {
output += "You got a Jack ";
} else if(num1 == 12) {
output += "You got a Queen ";
} else if(num1 == 13) {
output += "You got a King ";
}
num1 = 10;
} else {
output += "You got a "+num1 + " ";
}
if(num2 == 1){
output = "You got 2 Ace ";
ace2 = 11;
} else if(num2 > 10) {
if(num2 == 11) {
output += "and a Jack ";
} else if(num2 == 12) {
output += "and a Queen ";
} else if(num2 == 13) {
output += "and a King ";
}
num2 = 10;
} else {
output += "and a " + num2 + " ";
}
// Made the 2nd Ace to 1 when total greater then 21
if(num1 + num2 > 21 && (ace1 == 11 || ace2 == 11)) {
if(ace2 == 11) {
num2 = 1;
}
}
output += "for a total of " + (num1+num2);
System.out.println(num1);
System.out.println(num2);
System.out.println(num1 + num2);
return output;
}
}
2. test.java
package CardGame;
public class test {
public static void main(String[] args) {
BlackJack game = new BlackJack();
String output = game.play();
System.out.println(output);
}
}
comment : Run the test.java you will get the desired output as you asked for. Just check the play method of BlackJack.java you will find the entire logic written there. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.