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

Activity Details (Game: displaying three cards) Display a frame that contains th

ID: 3629210 • Letter: A

Question

Activity Details
(Game: displaying three cards) Display a frame that contains three labels. Each label displays a playing card. The card image files are named 1.png, 2.png, …, 54.png, and stored in the image/card directory. All three cards are distinc import and selected randomly.

here is what I have

javax.swing.*;
import java.awt.*;
import java.util.*;

public class THREECARD extends JFrame
{
public THREECARD()
{
int counter = 0;
int now = 0;
int hitter = 1;
Random r = new Random();

JLabel[] labels = new JLabel[54];

setLayout(new GridLayout(3,1));

for (int cards = 0; cards < 3; cards++)
{
labels[cards] = new JLabel();
add(labels[cards]);
}

while (counter < 3)
{
do
{
now = r.nextInt(3);
}
while (labels[now].getIcon() != null);

if (hitter == 1)
{
labels[now].setIcon(pick);
hitter = 0;
}

else
{
labels[now].setIcon(pick);
hitter = 1;
}

counter++;
}
}

public static void main(String[] args)
{
THREECARD frame = new THREECARD();

frame.setTitle("Display Three Cards");
frame.setSize(500, 350);
//frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Explanation / Answer

import java.awt.GridLayout;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class THREECARD extends JFrame
{

//good style dictates the avoidance of magic numbers, so I've created some variables:
int nCardsDisplayed = 3;
int totalCards = 54 ;

//display variables
int[] selected; //the numbers that are selected already
JLabel[] labels; //the labels

Random rand; //random object

public THREECARD()
{
selected = new int[nCardsDisplayed]; //integer array initializes to all 0s
labels = new JLabel[nCardsDisplayed]; //you only need three labels (save space)
rand = new Random(); //initialize random

setLayout(new GridLayout(nCardsDisplayed,1));

for (int cards = 0; cards {
int card = getNextCard();
selected[cards] = card;
labels[cards] = new JLabel(new ImageIcon("C:/Users/POWERSOF3/Documents/NetBeansProjects/ThreeCards/build/classes/threecards/Images/"+card+".png"));
labels[cards].setText(""+card); //I don't have your image files, so I added this line to test. Remove as needed.
add(labels[cards]);
}

/* FRAME HOUSEKEEPING */
setTitle("Display Three Cards");
setSize(500, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

/**
* Gets a random number then checks to make sure the corresponding card is not selected yet
* @return card number
*/
private int getNextCard()
{
int cardNo;
while(isAlreadyDisplayed(cardNo=rand.nextInt(54)+1)); //will loop until a valid card is found
return cardNo;
}

/**
* checks the selected array for the integer in question
* @param cardNo
* @return true if the card already exists, false otherwise
*/
private boolean isAlreadyDisplayed(int cardNo)
{
for(int i=0; i {
if(selected[i]==cardNo) return true;
}
return false;
}

public static void main(String[] args)
{
THREECARD frame = new THREECARD();
frame.setVisible(true);
}
}

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