• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Merge Two Halves Into One Sorted Array

 
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?




Deck



Card


TestDrive


Error

 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thank you!
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that I am thinking though, I don't need to swap, I need to override actual array.

In the above code, I am using tempArray to write down the correct sorted object Card array. All I need to do at the end is copy over all tempArray values over to Card object.

How would I do that without setSuit and setRank though?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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, but ... what would PIE do? Especially concerning this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic