You are viewing our Forum Archives. To view or take place in current topics click here.
Find the largest card in the pack? Java help.
Posted:

Find the largest card in the pack? Java help.Posted:

-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Hi! So basically what I need to do is to find the largest card in a randomly generated pack of 10 cards. So say for example the program generated the following 10 cards that are then stored in an ArrayList of Card obejcts:

Four of Spades
Eight of Spades
Six of Hearts
King of Clubs
Six of Clubs
Seven of Diamonds
Ace of Clubs
King of Clubs
Seven of Diamonds
Ace of Spades


So currently I'm trying to write a method that will find the largest card in the pack. Each card has a numerical value of 1-13 with an Ace being 1 and a King being 13. So I want the method to loop through the ArrayList and find the cards with the highest numerical value. If 2 or more cards have the same numerical value the highest is determined by the suit of the card. With it being in the order of Spades > Hearts > Clubs > Diamonds. I have the first part of the method where it compares the numerical value but I'm stuck on comparing the suits of the cards. So far I have this:

   public Card findLargest() {
      Card largest;
      Card c;
      for (int i = 0; i < pack.size(); i++){
         if (c.getNumber() > largest.getNumber()){
         largest = c
         else if (c.getNumber() == largest.getNumber()){
            if (largest.getSuit().equals("Diamonds"))
                 largest = !c
            
         }
   }

   }
   }


Any help is greatly appreciated. Thanks in advance!
#2. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Totally shameless self bump.
#3. Posted:
Imp
  • Retired Staff
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
You may need to consider setting a numerical value for each of you suit names, then check the value of the suit on the largest card against the value of the suit in the current card, whichever the larger is the new largest car.
#4. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Imp wrote You may need to consider setting a numerical value for each of you suit names, then check the value of the suit on the largest card against the value of the suit in the current card, whichever the larger is the new largest car.


I was told by my tutor to not do it like that. Although it would be much easier and neater to do it like that.
#5. Posted:
TTGXMODsX
  • TTG Natural
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 996
Reputation Power: 64
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 996
Reputation Power: 64
-Patman wrote
Imp wrote You may need to consider setting a numerical value for each of you suit names, then check the value of the suit on the largest card against the value of the suit in the current card, whichever the larger is the new largest car.


I was told by my tutor to not do it like that. Although it would be much easier and neater to do it like that.


I think your teacher wants the cards to be in an array so you will need to set the suit value in the array
#6. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
I have the structure of my method, it's just trying to get it to work. It should look like this:

   public Card findLargest() {
      Card largest;
      Card c;
      for (int i = 0; i < pack.size(); i++){
         if (c.getNumber() > largest.getNumber()){
         largest = c
         else if (c.getNumber() == largest.getNumber()){
            if (largest.getSuit().equals("Diamonds"))
                 largest = c
                 else if ()
                   
                 else if ()
            
         }
   }

   }
   }
#7. Posted:
Imp
  • Shoutbox Hero
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Spades > Hearts > Clubs > Diamonds

You need to build a nested IF statement started from as you have Diamonds then follow the logic down

If this card is a diamond then its the largest else

If this card is a club and the largest card is a heart or a spade then this card is the largest else

If this card is a heart and the largest card is a spade then this card is the largest

All other conditions means that the largest card is the same as thus card

If you convert that you should get the expected results.

Sorry to be tough and not give you exact code, but it is the best way to learn
#8. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Imp wrote Spades > Hearts > Clubs > Diamonds

You need to build a nested IF statement started from as you have Diamonds then follow the logic down

If this card is a diamond then its the largest else

If this card is a club and the largest card is a heart or a spade then this card is the largest else

If this card is a heart and the largest card is a spade then this card is the largest

All other conditions means that the largest card is the same as thus card

If you convert that you should get the expected results.

Sorry to be tough and not give you exact code, but it is the best way to learn


No that's fine, I'd much rather it that way.
#9. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201013Year Member
Posts: 823
Reputation Power: 34
Is it along these lines or have I done this completely wrong?

public Card findLargest(){
   Card largest;
   Card c;
   for (int i = 0; i < pack.size(); i++){
      if (c.getNumber() > largest.getNumber()){
         largest = c;
         else if (c.getNumber() == largest.getNumber()){
            if (largest.getSuit().equals("Diamonds"))
               largest = c;
            else if (largest.getSuit().equals("Clubs") && c.getSuit().equals("Hearts" || "Spades"))
               largest = c;
            else if (largest.getSuit().equals("Hearts")) && c.getSuit().equals("Spades")
               largest = c;
         }
      }
   }
}
#10. Posted:
Imp
  • Blind Luck
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
-Patman wrote Is it along these lines or have I done this completely wrong?

public Card findLargest(){
   Card largest;
   Card c;
   for (int i = 0; i < pack.size(); i++){
      if (c.getNumber() > largest.getNumber()){
         largest = c;
         else if (c.getNumber() == largest.getNumber()){
            if (largest.getSuit().equals("Diamonds"))
               largest = c;
            else if (largest.getSuit().equals("Clubs") && c.getSuit().equals("Hearts" || "Spades"))
               largest = c;
            else if (largest.getSuit().equals("Hearts")) && c.getSuit().equals("Spades")
               largest = c;
         }
      }
   }
}


Right just looking at the code, the idea is right, but what you have done is checked the largest card suit, swap all the "largest" with "c" and visa versa. But only in the If statements checking suits, including your "diamond" check

you need to be checking the current card suit and seeing if it is better than the current largest suit ;)
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.