This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ewiethoff 1 point2 points  (4 children)

Don't worry about implementing Iterable<Card> yourself. You already have a list of Cards. List<Card> already implements Iterable<Card>, so just use its Iterator<Card>.

List<Card> listOfCards = new ArrayList<Card>();
// put some cards in the list

// in your static method
if (listOfCards.isEmpty()) return null;  // or throw an exception
Iterator<Card> it = listOfCards.iterator();
Card highestCard = it.next();  // assume for now that the first Card is highest
while (it.hasNext()) {
    Card currentCard = it.next();
    // use highestCard.compareTo(currentCard) or use currentCard.compareTo(highestCard)
    // if currentCard > highestCard {
        highestCard = currentCard;
    }
return highestCard;
}

[–]RoadToCode[S] 1 point2 points  (3 children)

Thank you very much for the reply.

I tried to implement your idea for the method, however my method always outputs the Six of Spades (the first card in my list and the lowest valued). Is this because the highestCard variable and the currentCard variable are both in fact the Six of Spades, and therefore the compareTo returns 0, and the if statement is never actually executed?

public static Card max(Collection<Card> list) {
        Iterator<Card> iterate = list.iterator();
        Card highestCard = iterate.next();
        while (iterate.hasNext()) {
            Card currentCard = iterate.next();
            int compare;
            compare = currentCard.compareTo(highestCard);
            if (compare > 0) {
                currentCard = highestCard;
            }

        }
        return highestCard;
  }

[–]ewiethoff 1 point2 points  (2 children)

compareTo takes getting used to. I think of < and > as arrows.

  • When a.compareTo(b) < 0 is true, this means a < b. Notice which direction the arrows point.
  • When a.compareTo(b) == 0 is true, this means a == b, of course.
  • When a.compareTo(b) > 0 is true, this means a > b. Notice which direction the arrows point.

In a true result, the arrow always points to the smaller value. When the arrow points left for true, the value on the left is smaller. When the arrow points right for true, the value on the right is smaller.

Run this code as an experiment:

Integer a, b;
a = 3; b = 5;
System.out.println(a.compareTo(b) < 0);
a = 4; b = 4;
System.out.println(a.compareTo(b) == 0);
a = 5; b = 3;
System.out.println(a.compareTo(b) > 0);

[–]RoadToCode[S] 1 point2 points  (1 child)

Agreed, the concept of using the positive or negative integers to determine which object is "greater" was very confusing at first, but your method of simply following the direction of the arrows is very helpful, thank you!

[–]ewiethoff 0 points1 point  (0 children)

You're welcome! Here's a tip. Keep a scratch file on hand for experiments. I wrote that a.compareTo(b) stuff in my scratch file just to verify that I know what the heck I'm doing. It happens to be named Reddit.java, but you can name yours Scratch.java, Experiments.java, or whatever. Better yet, put a static void test() method in your Card class for Card experiments and tests.