Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a static method blackjack that accepts as its parameter a Scanner for an i

ID: 3817413 • Letter: W

Question

write a static method blackjack that accepts as its parameter a Scanner for an input file containing a hand of playing cards, and returns the point value of the hand in the card game Blackjack.

A card has a rank and a suit. There are 13 ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. There are 4 suits: Clubs, Diamonds, Hearts, and Spades. A Blackjack hand's point value is the sum of its cards' point values. A card's point value comes from its rank; the suit is irrelevant. In this problem, cards are worth the following points:

The input file contains a single hand of cards, each represented by a pair of "<rank><suit>" tokens.

For example:

5 Diamonds

Q Spades

2 Spades 3 Hearts

Given the above input, your method should return 20, since the cards' point values are 5 +10 +2 +3 = 20

The input can be in mixed casing, have odd spacing between tokens, and can be split across lines.

For example:

2 Hearts

j SPADES a Diamonds

2 ClUbS

A

hearts

Given the above input, your method should return 36, since the cards' point values are 2 +10 +11 +2 +11 = 36

You may assume that the scanner contains at least 1 card (two tokens) of input, and that no line will contain any tokens other than valid card data. The real game of Blackjack has many other rules that you should ignore for this problem, such as the notion of going "bust" once you exceed a score of 21.

Rank Point Value 2-10 The card's rank (for example, a 7 is worth 7 points) Jack(J), Queen(Q), King(K) 10 points each Ace(A) 11 points (for this problem; simplified compared compared to real Blackjack)

Explanation / Answer

Answer-

public static int blackjack(Scanner input) {
int total = 0;
while (input.hasNext()) {
if (input.hasNextInt()) {
total += input.nextInt();
} else {
String token = input.next().toLowerCase();
if (token.equals("j") || token.equals("q") || token.equals("k")) {
total += 10; // jack/queen/king
} else {
total += 11; // ace
}
}
input.next(); // suit
}
return total;
}
public static int blackjack(Scanner input) {
int total = 0;
while (input.hasNext()) {
if (input.hasNextInt()) {
total += input.nextInt();
} else {
String token = input.next().toLowerCase();
if (token.equals("a")) {
total += 11;
} else if (token.equals("j") || token.equals("q") || token.equals("k")) {
total += 10; // jack/queen/king
}
}
}
return total;
}
public static int blackjack(Scanner input) {
int total = 0;
while (input.hasNext()) {
if (input.hasNextInt()) {
total += input.nextInt();
} else {
String token = input.next().toLowerCase();
if (token.equals("a")) {
total += 11;
} else {
total += 10; // jack/queen/king
}
}
input.next(); // suit
}
return total;
}