You are viewing our Forum Archives. To view or take place in current topics click here.
#11. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
How is 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 (c.getSuit().equals("Clubs") && largest.getSuit().equals("Hearts" || "Spades"))
                  largest = c;
               else if (c.getSuit().equals("Hearts")) && c.getSuit().equals("Spades")
                  largest = c;
            }
         }
      
   return largest;
   }
#12. 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
-Patman wrote How is 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 (c.getSuit().equals("Clubs") && largest.getSuit().equals("Hearts" || "Spades"))
                  largest = c;
               else if (c.getSuit().equals("Hearts")) && c.getSuit().equals("Spades")
                  largest = c;
            }
         }
      
   return largest;
   }


Almost there, change the diamond check to "c" and the last spades check to "largest" and that should do it.

Just to warn you, I am only reading this on my blackberry so if you can test it maybe an idea before handing in lol
#13. 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
Just noticed that this may break, you need to do a check at the very start to see if there is a largest card, if there isn't one then by default the first card is the largest
#14. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
I've made a big breakthrough, I now have this:

 public Card findLargest(){
  Card largest = null;
 
 
  for (Card c:pack){
   ;
   if (c.getNumber() > largest.getNumber()){
    largest = c ;}
   else if (c.getNumber() == c.getNumber()) {
       if (largest.getSuit().equals("Diamonds")) {
        largest = c;
       
       }
       else if (largest.getSuit().equals("Clubs")) {
        if (!c.getSuit().equals("Diamonds")) {
         largest = c;
        }
       
       }
       else if (largest.getSuit().equals("Hearts")) {
        if (c.getSuit().equals("Diamonds")) {
         if (c.getSuit().equals("Clubs")) {
          largest = c;
         }
         
        }
       
       }
   
   }
   
     
 
 
 
 





}
return largest;
 }
}


But I'm getting a null pointer exception. Any ideas?
#15. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
I know that largest is still null here and I'm invoking getNumber()on null which is throwing the NullPointerException. It's just trying to sort it out.
#16. 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
The null pointer exception will be from this line

if (c.getNumber() > largest.getNumber()){

because largest is null.

before the for loop, set largest to be the first card in the pack.

It appears to me as though you have the logic the wrong way. Let me explain.

if (largest.getSuit().equals("Diamonds")) {
largest = c;
}

This statement says, if the largest card is a diamond, then set the current card to be the largest... but what is the suit of the current card?

else if (largest.getSuit().equals("Clubs")) {
if (!c.getSuit().equals("Diamonds")) {
largest = c;
}

This one says,if the largest card is a club, and the current card is not a diamond then set the largest card to the current card.

else if (largest.getSuit().equals("Hearts")) {
if (c.getSuit().equals("Diamonds")) {
if (c.getSuit().equals("Clubs")) {
largest = c;
}

And the last block is the same, if the largest is a heart, then if the current card if a diamond, then if the current card is a club, set the largest to be the current card (Which obviously can never happen!)

Here is the beginning of my code just to start you off ;)


       if (c.getSuit().equals("Diamonds")) {
           largest = c;
       }
       else if (c.getSuit().equals("Club") && (largest.getSuit().equals("Heart") || largest.getSuit().equals("Spade"))
       {
           largest = c;
       }


See if you can work it from there
#17. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Quick question, how do I set largest as the first card in the pack?
#18. 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
-Patman wrote Quick question, how do I set largest as the first card in the pack?


At the start of the loop, do

if (null == largest){
largest = c;
}

This will alway then set largest to c on the very first instance of the loop
#19. Posted:
-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Is this correct so far?

   public Card findLargest(){
       Card largest = null;
          if (null == largest){
          largest = c;
          }
      for (Card c:pack){
         if (c.getSuit().equals("Diamonds")){
            largest = c;
         }
         else if (c.getSuit().equals("Clubs") && (largest.getSuit().equals("Hearts") || largest.getSuit().equals("Spades"))){
            largest = c;
         }
         else if (c.getSuit().equals("Hearts") && (largest.getSuit().equals("Spades"))){
            largest = c;
         }
               }
              return largest;
              }
            
             }
#20. 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
-Patman wrote Is this correct so far?

   public Card findLargest(){
       Card largest = null;
          if (null == largest){
          largest = c;
          }
      for (Card c:pack){
         if (c.getSuit().equals("Diamonds")){
            largest = c;
         }
         else if (c.getSuit().equals("Clubs") && (largest.getSuit().equals("Hearts") || largest.getSuit().equals("Spades"))){
            largest = c;
         }
         else if (c.getSuit().equals("Hearts") && (largest.getSuit().equals("Spades"))){
            largest = c;
         }
               }
              return largest;
              }
            
             }


Move the check for null inside the for loop.

Apart from that i think you have cracked it
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.