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

Please adhere to the Standards for Programming Assignments and the Java Code and

ID: 3750240 • Letter: P

Question

Please adhere to the Standards for Programming Assignments and the Java Code and Style Guidelines. Submit the following: List of source code with comments to document Test output listed as comments at the end of your program Cards (10 pts). Design and implement an application called Cards that randomly selects a playing caro. irst use a random number generator (Random class) to create a number in the range 1 to 4. Then convert the number to a suit (heart, diamond, club, or spade) using a switch statement. Next, use the random generator to create a random number in the range 1 to 13. Convert the number to ace, 2, 3, etc using another switch statement. Display the suit and the value of the chosen card. Example output: You have selected the 5 of clubs! You have selected the King of hearts!

Explanation / Answer


import java.util.Random;

public class Cards {
public static void main(String args[])
{
  
Random ran=new Random();
String card="";
String cardName="";
String message;
int cards=ran.nextInt(4)+1;
int point=ran.nextInt(13-1) + 1;//generate random number between two numbers ie 1 and 13
String pointName="";
  
switch(cards)
{
case 4:
card="hearts";
break;
case 3:
card="diamonds";
break;
case 2:
card="clubs";
break;
case 1:
card="spades";
break;   
}
switch(point+1)//give names to the numbers
{
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
pointName=""+point;//(2-10 is the number itself)
break;
case 11:
pointName="Jack";
break;
case 12:
pointName="Queen";
break;
case 13:
pointName="King";
break;
case 14:
pointName="Ace";
break;
  
}
System.out.println("You have selected the "+pointName+" of "+card+" !");

}   
}

Output

You have selected the 8 of spades !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote