• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Highest card question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I'm working on a question where they want me to find the highest card out of four cards! Here is the question:

In a trick-taking card game, four players each play one card in turn. The trick is taken by the highest card played in the suit of the first card. (Ace counts as the highest card, and for simplicity, there are no jokers, trump suits or bowers that some card games have.) Write a method String winningCard(String play) that returns the card that won the trick, when the play to the trick is given as an 8-character string such as “5hKc2dJh”. For this example hand, this method would return “Jh”.

So I wrote a separate method that does the calculation to determine which card is higher.  After I am done that I can figure out which card is the highest.  








 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have a Card class.

Please read this: https://coderanch.com/wiki/659830/Wiki/Strings-Bad
 
Mimi Udasco
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wups, nevermind.  It is a class not a method! Doesn't fix anything though..
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something very non‑object‑oriented about your code. Java® is supposed to be an object‑oriented language an so many people appear here who appear to be bein gtaught procedural programming. Anyway, read the Java® Language Specification (=JLS). The JLS is usually incomprehensible, but not that section.
 
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be a little hard to help since I don't know what tools are in your toolbox--meaning, what parts of Java they taught you already.

enums would be useful for cards but since they're using a String for that, I'm inferring that they haven't mentioned enums.

Campbell: One doesn't start programming learning classes and OOP principals first; one begins with the basics: variables, conditional statements, and flow-control.  "Parts of speech" precedes "composition".


Mimi: I'm not sure what your question is; does your program work?

To address the repetition, did they cover "for loops"?  You could loop through that string rather than dealing with each pair individually.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm much less worried than you are. When someone makes his first steps, drops on the floor are to be expected; in the end, most of us learn to walk pretty well. And when I was in my very first school year, I counted on my fingers, while nowadays I might triple-integrate some function with a change of base. I bet the professor will come up with some lessons to show the limitations of the current String-based approach, that will be a valuable experience for OP. Just give it some time.

Edit: just missed that replacement Can you move this reply there as well? Thanks!
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julian West wrote:. . . Campbell: One doesn't start programming learning classes and OOP principals first; one begins with the basics: variables, conditional statements, and flow-control.  "Parts of speech" precedes "composition". . . .

That is what lots of people think, but it is not how I was taught Java®. It is really easy to get into a procedural style because procedural programming looks like school algebra. And many people find it very difficult to get out of a procedural style.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that Junilu has split off the discussion about teaching here. Please continue any discussion about teaching in that link.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Julian West wrote:. . . Campbell: One doesn't start programming learning classes and OOP principals first; one begins with the basics: variables, conditional statements, and flow-control.  "Parts of speech" precedes "composition". . . .

That is what lots of people think, but it is not how I was taught Java®. It is really easy to get into a procedural style because procedural programming looks like school algebra. And many people find it very difficult to get out of a procedural style.



Right, wrong, or indifferent, this doesn't help the person asking for it.

Saying, "that's wrong" and offering nothing else helps nobody.
 
Sheriff
Posts: 17644
300
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

Julian West wrote:
Right, wrong, or indifferent, this doesn't help the person asking for it.

Saying, "that's wrong" and offering nothing else helps nobody.


That's debatable.

Digressing to a different topic, however, will undoubtedly not help answer OP's question so if you'd like to discuss the merits or lack thereof of Campbell's point, please reply in the thread that was split off.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . That's debatable. . . .

I also think it wasn't what I said. Also I didn't notice that Junilu had split off the post about teaching until after I posted here.
 
Junilu Lacar
Sheriff
Posts: 17644
300
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
@Mimi : I'm not sure what your question is regarding the code that you posted.

Here's one thing about your code though: It's a very brute force way of doing things. Never use brute force when you can use a formula.

It may not be immediately obvious to you but there is a way to use the String API and eliminate a significant amount of that code. Take a look at this example:

Notice that N lines of code in the first version has been reduced to a single line of code in the second version.

You might be wondering how line 25 above works. The value "ABCDEFGHIJK" is a String literal. String literals in Java are also full-fledged objects. As such, you can call all of the String API methods on a String literal.

The following idiom takes advantage of this:

On line 2, the check for str != null is necessary to avoid a NullPointerException when str is null.  

On line 7, you avoid the check for null because a String literal is never null. So, you can safely call equals() on it without any danger of throwing a NullPointerException. This version is more straightforward and elegant.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With line 7 how do you know that the method accepts a null value for argument?  Not all methods will. So that technique requires some studying of the API doc or testing to see if it works.
The code on line 2 should always work.
 
Junilu Lacar
Sheriff
Posts: 17644
300
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

Norm Radder wrote:With line 7 how do you know that the method accepts a null value for argument?  Not all methods will. So that technique requires some studying of the API doc or testing to see if it works.
The code on line 2 should always work.


This particular idiom is used primarily with equals(), which should always return false when the argument is null. You can extend it to other types if you use constants that you know will not be null. I use it mainly when checking equality to a String though.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@mimi I'm not sure if you have fully understood the question as the cards do not need to be weighted according to suits ie Spades do not rank higher than clubs. The card that wins is the highest value card in the suit of the first card that was played. For example: if the first card is a 2 of clubs and the next three cards are the Ace of spades, Ace of hearts and Ace of diamonds the 2 of clubs is the winner, if however one of the other cards was a club with a higher value ie 4 of clubs then that card would be the winner.

Your code should not be adding a value for the suit but it should be disregarding any card that is not the same suit as the first card (ie giving it a zero score).
 
Junilu Lacar
Sheriff
Posts: 17644
300
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

Norm Radder wrote:With line 7 how do you know that the method accepts a null value for argument?  Not all methods will. So that technique requires some studying of the API doc or testing to see if it works.
The code on line 2 should always work.


There's an underpinning here that I must point out, however. There is a certain, for want of a better word, "attitude" implied that you'd rather do the clunky but safe thing instead of exercising due diligence to research, test, and perhaps refactor or rework the behavior/design. This can lead to a proliferation of code that always checks for null before calling a method on a reference variable.

This kind of code is indicative of the null flag antipattern:

When you have a lot of this kind of code in your app, you really should consider changing the design of myObj.getList() so that it will always return a valid list, even if it's just an empty one.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

do the clunky but safe thing

You got me there.  I try to always be safe.  Trying to optimize every statement to be the best way can make coding more time consuming and perhaps induce errors because the attempted solution isn't always safe.
 
Junilu Lacar
Sheriff
Posts: 17644
300
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

Norm Radder wrote:I try to always be safe. Trying to optimize every statement to be the best way can make coding more time consuming and perhaps induce errors because the attempted solution isn't always safe.


This kind of thing speaks to the overall approach to development. I view testing as something that must be done. If you're doing even minimal testing, these kind of problems should surface fairly early on and you can deal with them accordingly and do the right thing. Letting the null flag antipattern fester in your code to satisfy that urge to stay "safe" is often to the detriment of general code quality. In my experience, poor overall code quality brings with it many more costly problems than "being safe" in that one aspect is worth.
reply
    Bookmark Topic Watch Topic
  • New Topic