So cardArray is an object. I get each elements of this objects through getRank, getSuit methods in Card object. There is no setSuit and setRank. When I merge, as far as I know, I need to override values and swap them therefore I need setSuit and setRank methods. Below code fails with the error at the bottom and I am thinking because I am not able to use setSuit, setRank methods. Any ideas?
You should be swapping Card objects, not the values of the ranks and suits.
Edit: Yup, it seems you haven't quite grasped the concept of object-orientation. You are reaching far too deep into the details and you're attempting to use a 2D array to represent combinations of ranks and suits. That is not what you're supposed to do. Go back over your notes and review the concept of "Encapsulation". A Card object encapsulates the rank and suit values. Once you create a Card with a specific rank and suit, there is absolutely no need to change it. A real world analogy would be if you had a bunch of real playing cards, you are essentially trying to take an eraser and rub out the markings on each card so you can write over it with a pencil. If you saw someone in the real world trying to do that, you'd probably think they have a screw loose or something, so why would you do essentially the same thing in your program?
This is an homework given to me, I didn't really plan this program.
But it does make sense that I need to replace cardArray objects.
I don't know why would anyone write a code to go through merge sort method of each individual cardArray objects. It's totally unuseful and not helpful to understand the concept of merge sort whatsoever.
I couldn't tell you, honestly. That's like asking me how to drive on the wrong side of the road or clean a gun while it was loaded. It would be irresponsible for me to tell you because you just might go out and do it and get yourself killed or something.
For me, the only reasonable and sensible answer is this: DON'T do it that way. Go back and look for object-oriented examples of Card/Rank/Suit. Rank and Suit are normally implemented as enums. A Card is immutable, once you create it with a given Rank and Suit, it can't be changed. Did you not understand my analogy with real-world cards?
Here, have a look at the object-oriented way to write a Card class below. The CardDemo is just a quick-and-dirty way to show how to use the Card class, create instances, put them in a Collection, display, shuffle, sort them. I will leave it up to you to implement a proper Deck class that would manage a bunch of Card objects, allow those Card objects to be shuffled, ordered, dealt in hands, etc.
Thank you @Junilu Lacar, your cardDemo is much better than the one teacher gave me.
I just hope I won't end up getting a 0 by not figuring out a way to handle what he asked based on the way he's making me do it.
This is why it's better to learn coding yourself rather go to school. You learn from one another group of people and knowledge versus teacher make you code certain way you never really learn how to do certain things better.
Recaip Sanli wrote:Thank you @Junilu Lacar, your cardDemo is much better than the one teacher gave me.
It's the least I could do for you. Are you taking an online course or are you actually going to a brick-and-mortar school? If that was my instructor, I'd be on the phone with the school administration / department chair and giving them an earful.
Here's a challenge for you: How would you change the code I gave so that you could program a classic Solitaire game? In Classic Solitaire, open cards must alternate between red suits and black suits and they must be in rank order, with cards with lower ranks being placed on top of cards with the next higher rank, e.g. a black 10 can only be placed on top of a red Jack which can only be placed on top of a black Queen. How would you validate that a move was legal?
Hint: It might help if you added these methods:
One part of this challenge is to decide where to put these methods (they probably wouldn't all go in the same place).
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad: