Home > database >  I need this boolean to give me the lowest card value and print the first lowest card. When i use thi
I need this boolean to give me the lowest card value and print the first lowest card. When i use thi

Time:11-04

public String lowCard() {
    if (card1.getValue() < card2.getValue() && 
            card1.getValue() <card3.getValue() &&
            card1.getValue() < card4.getValue()) {
        return new string (card1.toString());
    }
    
    else (card2.getValue() < card1.getValue() &&
            card2.getValue() < card3.getValue() &&
            card2.getValue() < card4.getValue()) {
        return (card2.toString());
    }
    
    else (card3.getValue() < card1.getValue() &&
            card3.getValue() < card2.getValue() &&
            card3.getValue() < card4.getValue()) {
        return (card3.toString());
    }
    else
        return (card4.toString());
}

It should print.. 3 of clubs 2 of diamonds 5 of clubs 2 of clubs The low card is 2 of diamonds

but it prints... 3 of clubs 2 of diamonds 5 of clubs 2 of clubs The low card is 2 of clubs

CodePudding user response:

So there's a few things happening here that we can make better.

  1. This code as written does not compile. We can't have 2 elses after an if. else should be the last in the decision tree with no conditions. When we do if/else if/else statements the syntax looks like this:

    if(condition){
       //do something 
    } else if (condition) {
       //do something
    } else {
      //do something
    }
    

Notice the placement of your current braces do not match this syntax.

  1. With that in mind, you need else ifs and you mentioned in a comment that these are integers. So we should change how we return it if we want a string value. We can do String.valueOf(num) or Integer.toString(num) It'll look like this:

    public String lowCard() {
     if ((card1 < card2) && 
         (card1 < card3) &&
         (card1 < card4)) 
     {
         return Integer.toString(card1);
     }
    
     else if ((card2 < card1) && 
              (card2 < card3) &&
              (card2 < card4))  
     {
         return  Integer.toString(card2);
     }
    
     else if ((card3 < card1) && 
              (card3 < card2) &&
              (card3 < card4))  
     {
         return  Integer.toString(card3);
     }
     else
         return  Integer.toString(card4);
     }
    

Now remember that these cards HAVE to be global variables because you haven't passed them into your function. As for where it says "of clubs" or "of diamonds" you got me. That's not listed in the question anywhere. If you complete your question, I can complete this answer.

CodePudding user response:

public class PokerHand {
// instance variables
private Card card1;
private Card card2;
private Card card3;
private Card card4;

/**
* parameterized constructor --
* puts 4 cards into the poker hand
* @param c1 a reference to a Card object
* @param c2 a reference to a Card object
* @param c3 a reference to a Card object
* @param c4 a reference to a Card object
*/
public PokerHand (Card c1, Card c2, Card c3, Card c4) {
card1 = c1;
card2 = c2;
card3 = c3;
card4 = c4;

}

/**
* fourOfAKind method --
* determines if the four cards have the same value
* @return true if four of a kind exists, false otherwise
*/

public boolean fourOfAKind() {
if( card1.equalValue(card2) && card1.equalValue(card3) && card1.equalValue(card4) )
return true;
else
return false;
}

/**
* flush method --
* determines if the four cards are of the same suit
* @return true if a flush exists, false otherwise
*/
public boolean flush() {
if (card1.equalSuit(card2) &&
card1.equalSuit(card3) &&
card1.equalSuit(card4))
return true;
else
return false;
}


/**
* threeOfAKind method --
* determines the different ways that three of a kind may exist with 4 
* cards
* assumes that four of a kind has already been tested
* @return true if three of a kind exists, false otherwise
*/


public boolean threeOfAKind() {
if (card1.equalValue(card2) ||
card1.equalValue(card3) ||
card1.equalValue(card4)) {
return true;
}
if (card2.equalValue(card1) ||
card2.equalValue(card3) || (card2.equalValue(card4))) {
return true;
}
if (card3.equalValue(card1) ||
card3.equalValue(card2) || (card3.equalValue(card4))) {
return true;
}

else {
return false;
}

}


/**
* pair method --
* determines the different ways that a pair that may exist with cards
* assumes that four of a kind and three of a kind have already been 
*tested
* @return true if a pair exists, false otherwise
*/

public boolean pair() {
if (card1.equalValue(card2) ||
card1.equalValue(card3) ||
card1.equalValue(card4))
return true;
else if (card2.equalValue(card1) ||
card2.equalValue(card3) ||
card2.equalValue(card4))
return true;
else if (card3.equalValue(card2) ||
card3.equalValue(card1) ||
card3.equalValue(card4))
return true;
else if
(card4.equalValue(card2) ||
card4.equalValue(card3) ||
card4.equalValue(card1))
return true;
else
return false;

}





/**
* lowCard method --
* finds the card with the lowest value -- if two cards have the
* same value, it should return the first card it finds with that value
* @return a reference to the card with the lowest value
*/

public Card lowCard() {
if (card1.getValue() < card2.getValue() &&
card1.getValue() <card3.getValue() &&
card1.getValue() < card4.getValue()) {
return (card1);
}
else if (card2.getValue() < card1.getValue() &&
card2.getValue() < card3.getValue() &&
card2.getValue() < card4.getValue()) {
return (card2);
}
else if (card3.getValue() < card1.getValue() &&
card3.getValue() < card2.getValue() &&
card3.getValue() < card4.getValue()) {
return (card3);
}
else
return (card4);
}






/**
* replaceCard method --
* replaces the card number with the specified card
* @param whichCard the number of the card to be replaced
* @param otherCard the card to be placed into the poker hand
*/
public void replaceCard( int whichCard, Card otherCard ) {
if (whichCard == 1)
card1 = otherCard;
else if (whichCard == 2)
card2 = otherCard;
else if (whichCard == 3)
card3 = otherCard;
else if (whichCard == 4)
card4 = otherCard;
}


/**
* toString method
* @return a reference to a String which contains the value and suit of each
* of the 4 cards
*/
public String toString() {
return new String( card1.toString()   "\n"   card2.toString()   "\n"  
card3.toString()   "\n"   card4.toString() );
}
}



The method is used as such... 

PokerHand clubHand = new PokerHand (twoClubs, threeClubs, fourClubs, 
fiveClubs);

System.out.println ("Testing lowCard card method: "   "\n" );
System.out.println (clubHand.toString());

System.out.println("The low card is "   clubHand.lowCard()   "\n");
clubHand.replaceCard(1, threeClubs);
clubHand.replaceCard(2, twoClubs);
System.out.println(clubHand.toString());
System.out.println("The low card is "   clubHand.lowCard()   "\n");

clubHand.replaceCard(2, fourClubs);
clubHand.replaceCard(3, twoClubs);
System.out.println(clubHand.toString());
System.out.println("The low card is "   clubHand.lowCard()   "\n");

clubHand.replaceCard(3, fiveClubs);
clubHand.replaceCard(4, twoClubs);
System.out.println (clubHand.toString());
System.out.println( "The low card is "   clubHand.lowCard()   "\n");

clubHand.replaceCard(2, twoDiamonds);
System.out.println(clubHand.toString());
System.out.println("The low card is "   clubHand.lowCard());

//Im focusing on the lowCard method.


It should print..
3 of clubs
2 of diamonds
5 of clubs
2 of clubs
The low card is 2 of diamonds

but it prints...
3 of clubs
2 of diamonds
5 of clubs
2 of clubs
The low card is 2 of clubs
  • Related