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
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.
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.
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.
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.
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.