This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort Cards by Face?  RSS feed

 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone, I'm having some trouble in my Java class. This is only the 4th week and I'm completely lost. Most of this I got from what we did in class and what I've found on Google. This is supposed to be a beginning Java course, but I feel like we're moving way to fast. I'm actually pretty depressed about how the class is going. Anyway, here's my problem:

Create a Method call “SortCardsByFace()” for Class Player, which need to sort the player’s cards based on their face value (suit values can be ignored in sorting). When you are doing sorting, use toInt() method to compare and sort the Cards. Then use a for-loop to display all cards in sorted increasing order. “Firstname Lastname holds following cards: ♦2 ♦7 ❤8 ❤9 ♣10 ❤J ♠K” You can use any sorting algorithm, BubbleSort, MergeSort, QuickSort etc.
1.9.2 Create a Method call “SortCardsBySuitFace()”, which need to sort the user’s cards based on their Suit value together with face value. The cards should be sorted in each suits separately. Also use a for-loop to display all cards in sorted increasing order. “Firstname Lastname holds following cards: ♦2 ♦7 ♦K ❤8 ❤9 ❤J ♣10 ♠K”

This is my code for the Class Player:

I think I'm on the right track, but I don't know for sure. I'm not getting any errors, save for the line "if (c.toInt(cards.get(k).getFace()) < c.toInt(cards.get(j).getFace()))" The error says: method toInt in class Card cannot be applied given types;
required: no arguments
found: String
reason: actual and formal argument lists differ in length


Here's class Card:


If you need me to post my other classes, I'll get them up. In advance, thanks for any help.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would appear to be calling toInt("some text") when it should be toInt(). That error appears to be occurring in different code from what you posted.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 58 in the Player class:
and line 58 in the Card class, here is how method signature looks like:
As you see, your declared method are not meant to accept any arguments, but you pass some during the method call.

Probably you could fix that by defining method as:
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chase Harris wrote:Hello, everyone, I'm having some trouble in my Java class. This is only the 4th week and I'm completely lost. Most of this I got from what we did in class and what I've found on Google. This is supposed to be a beginning Java course, but I feel like we're moving way to fast. I'm actually pretty depressed about how the class is going. Anyway, here's my problem:

OK, well one of the problems I suspect you're running into is that you're trying to think of everything at once - Cards, Players, Card values etc - and you're getting overwhelmed by all the "mechanics". Don't worry, it's a common problem when you're starting out, but you need to learn to break down problems and eliminate things that aren't relevant.

First lesson: ALL sort methods do the same thing. They take a bunch of unordered "things" - in your case Cards - and they rearrange them in a predefined order - in your case the order defined by Card.toInt().
Now how they do that may well differ from method to method - it would appear the you're using a bubble sort, but I could be wrong (I never was very good at spotting that sort of stuff ) - but what they do is essentially the same.

So: since I assume that, right now, the thing you want to do is to get that sort to work, forget about everything else.

If it was me, I'd write a completely separate TestPlayer class that has only TWO methods in it - main() and sortCardsByFace() (methods should start with a lowercase letter) - ie, eliminate EVERYTHING that is not specifically concerned with testing your sort method.

And my main() method would probably look something like this:and I'd keep running that program with different "hands" (including empty ones) until I'm SURE it works. Every single time.

And once I am, I'd copy the method back to my Player class.

Do you see the idea? Right now (I suspect) you're overwhelmed with all sorts of things that have nothing to do with your sort method.

So get rid of 'em.

HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11153
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The crux of the problem is here:

Others have already pointed out the cause of your compile-time error. When you fix that, you'll find that your sorting logic doesn't work. Why do you feel there's a need to create a new card? When you're sorting things, you only look at the things that already exist, there's no need to include anything like a new Card.

Also, it helps to stay consistent with the terminology used in the problem domain. You might be unconsciously confusing yourself with terms that conflict with deeply ingrained knowledge you already have about cards. When it comes to cards, the thing that you call "face" is usually called the "rank" or "value". The J,Q,K are "face cards" because they usually have faces drawn on the cards. You can and probably will ignore this piece of advice but if you want to learn more about how much stronger your unconscious brain is than you think, look up the Stroop Effect. It's always better to use names that are semantically correct.

To Winston's point, you've lost yourself in the formula and lost track of what you really want to say. The formula that I'm talking about is the expression in that pesky if condition. It doesn't say what you think it's saying. If you move the logic to the Card class and write this instead, you might get a clearer picture of what you need to do:
One principle that's good to follow in OOP is Tell-Don't-Ask. In your code, you asked cards for their values and plugged those values into a formula that you then used to determine what to do next. When you move the logic to the Card class and just call a method like outRanks(Card other) or isGreaterThan(Card other), you are essentially saying, "Hey card[i], please [figure out if you outrank || figure out if your value is greater than the value of] this other card, card[i+1]. When you're done figuring, let me know what you found out." When the card tells you that it outranks the other card, that's when you do your sorting thing, otherwise, you move on to the next card and TELL it to do the thing again.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Also, it helps to stay consistent with the terminology used in the problem domain.

I get the impression that the term 'face' is given in the assignment.

If you move the logic to the Card class and write this instead, you might get a clearer picture of what you need to do:

I'm not convinced of this, since whether cardA > cardB depends on the specific card game.
I think it is the responsibility of that specific game to come up with a comparator, and in that
respect I find OP's code better (if maybe a bit faulty).

Well anyway, if I read OPs'very first paragraph, I think OP has quite a lot to grasp now.
 
Junilu Lacar
Sheriff
Posts: 11153
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet, I think you missed my point or at least misdirected the attention to it. My point is that there's a lot of detail in that implementation that hides the IDEA, the intent of what the calculation represents. Refactoring it so that the intent is revealed better is what I'm trying to get at. Whether that responsibility should be assigned to the Card class or a CardGame class is another discussion in itself. However, if you do want to talk about it now, I see nothing in what the OP posted as his requirements that says you need to be flexible that way. He only needs to sort the cards and as such it seems to me that the responsibility of determining a natural order should be with the Card class.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the help, guys. I felt really overwhelmed, but all the support is definitely helpful. I will be carefully going over everything that's been posted.

I'm not sure if this changes anything, but this was the step before:

In “DeckofCards”, create a Method call “public Card getCurrentCard()”, which will return you currentCard to dealt, and move currentCard to next Card (currentCard++). In your getCurrentCard() method, you should always check if the “currentCard” index is out of boundary. If yes, return null value, and print out “deck has no card left! Deal ends here.”

Create a Method call “Deal(Player[] players, int numberOfCards)”. This method will take array of players, deal each player with “numberOfCards”, and move the currentCard index to the last available Card in deck after each deal. You should use a for-loop to go through each Player, call their AddCard method to add the Cards to them.
In your deal method, you should always check the value from getCurrentCard(), if it is null, that means it reaches end of the DECK, you should directly return to the upper caller layer to break the for loops.

1.7-1.8 worth 20 points. In your main function, create 4 new players. Pass the 4 players to the deck.Deal function, deal 1 card for 1 player each time. Use a for-loop to deal all cards in deck.
Then for each player, call their ShowCards method, which will show what cards they are holding. Then call DeckOfCards.Dsiplay to show how many cards in deck. It should show no cards in deck.


I don't know if this alters what I need to do. I think I pretty much have all this in place though.
 
Junilu Lacar
Sheriff
Posts: 11153
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see your latest requirements changing any of the advice given previously. By convention, method names in Java start with a lower case letter, much like how your getCurrentCard() is named, so the requirement to have a method call "Deal(Player[] players, int numberOfCards" appears to go against this convention. That should be deal(Player[] players, int numberOfCards), deal with a lowercase 'd'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!