java program- i need help with this progam. its problem 14.3 from the intro to j
ID: 3686641 • Letter: J
Question
java program- i need help with this progam. its problem 14.3 from the intro to java programming 10th edition book:
Display three cards) Write a program that displays three cards randomly selected from a deck of 52, as shown in Figure 14.43c. The card image files are named 1.png, 2.png, ..., 52.png and stored in the image/card directory. All three cards are distinct and selected randomly. Hint: You can select random cards by storing the numbers 1–52 to an array list, perform a random shuffle introduced in Section 11.12, and use the first three numbers in the array list as the file names for the image.
extra intrustions:
1. Use an ArrayList (add to insert into the array list and get to obtain a value from the array list) together with the shuffle method from util.Collections.
2. add a button which refreshes the display.
Explanation / Answer
The information provided is incomplete. You have not provided Figure 14.43c and Section 11.12. But still I have tried to do my best to implement the functionality. If you provide me with other details, I will make sure to provide you the complete solution.
package chegg;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Cards {
public static void main(String[] args) {
List<Integer> cardDeck = new ArrayList<>();
for (int i = 1; i <= 52; i++) {
cardDeck.add(i);
}
Random random = new Random();
int randomIntegerInRange;
// Shuffle the deck
for (int i = 0; i < 52; i++) {
// Get random integer
randomIntegerInRange = random.nextInt(52);
// Swap it with current position
int temp = cardDeck.get(i);
cardDeck.set(i, randomIntegerInRange);
cardDeck.set(randomIntegerInRange, temp);
}
System.out.println(getCardFace(cardDeck.get(0)));
System.out.println(getCardFace(cardDeck.get(1)));
System.out.println(getCardFace(cardDeck.get(2)));
}
private static String getCardFace(int value) {
// Diamonds, Spades, Clubs, Hearts
if(value == 0)
return "Ace of Diamonds";
if (value < 10)
return (value+1) + " of Diamonds";
if (value == 10)
return "Jack of Diamonds";
if (value == 11)
return "Queen of Diamonds";
if (value == 12)
return "King of Diamonds";
if(value == 13)
return "Ace of Clubs";
if (value < 23)
return (value-12) + " of Clubs";
if (value == 23)
return "Jack of Clubs";
if (value == 24)
return "Queen of Clubs";
if (value == 25)
return "King of Clubs";
if(value == 26)
return "Ace of Spades";
if (value < 36)
return (value-25) + " of Spades";
if (value == 36)
return "Jack of Spades";
if (value == 37)
return "Queen of Spades";
if (value == 38)
return "King of Spades";
if(value == 39)
return "Ace of Hearts";
if (value < 49)
return (value-38) + " of Hearts";
if (value == 49)
return "Jack of Hearts";
if (value == 50)
return "Queen of Hearts";
if (value == 51)
return "King of Hearts";
return null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.