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

I have a class for cards which picks a radom card number and suit. I have the be

ID: 3558422 • Letter: I

Question

I have a class for cards which picks a radom card number and suit. I have the belo code which will show me what the 2 cards are. I need to add something tht will determine if one card is hgher than th other one and tell me which one is higher or if they are the same.

I tried putting:

if (card1 > card2){

JOptionPane.showMessageDialog(null, "the higher card is:" + card1);

}

else if (card1==card2){

JOptionPane.showMessageDialog(null, "the higher card is:" + card2);

}

but that gave me errors and wouldn't work.

Can anyone help?

// This class calls a second class called cards to randomly pick 2 different playing cards and show the user the cards picked.

?

import javax.swing.JOptionPane;

public class War

{

public static void main(String[] args)

{

Card card1, //Card objects to be constructed for user's output

card2;

card1 = new Card(); //sets hand1 as the first random card

card2 = new Card(); //sets hand2 as the first random card

if (card1==card2){card1 = new Card();} //checks if both cards are the same and if they are it will choose a different one.

JOptionPane.showMessageDialog(null,"Player 1 Card: " + card1.getValue()+" of "+ card1.getSuit() +" "

+ "Computer's Card: " + card2.getValue()+" of "+ card2.getSuit());

}

}

Explanation / Answer

This is because maybe your comparator for the card class is not defined. you should rather update the if else statements to compare the value of the card instead of card objects themselves. You can update these as :

if (card1.getValue() > card2.getValue()){

JOptionPane.showMessageDialog(null, "the higher card is card 1:" + card1.getValue() +" of "+ card1.getSuit() +" ");

}

else if (card1.getValue()==card2.getValue()){

JOptionPane.showMessageDialog(null, "the higher card is card 2:" + card2.getValue() +" of "+ card2.getSuit() +" ");

}