As MS has already hinted, it is non‑object‑oriented programming to do that when you can override the toString method instead.If you haven't got a good explanation for the keyword static, it is a mistake.
I didn't write the base code. My professor sent it to the class to modify. Not sure why he is doing anything honestly.Campbell Ritchie wrote:Don't use the == operator to compare reference types; it might appear to work at present because you are using String literals, but it can be relied upon to give undesired results some time or other.
The lowest cards are called deuce and trey; since Ace is the highest card, it should be declared last. That is how the base code is written and my professor will fail me on this assignment if I stray too much.
MS: why would you need methods to check the validity? Please explain more.
Dave Tolls wrote:For those basic errors, those enum values are not part of Card, they are part of Card.Suit and Card.Rank respectively.
chad jensen wrote:I didn't write the base code. My professor sent it to the class to modify. Not sure why he is doing anything honestly.
I presume that was as an example of code needing lots of modification to make it work.chad jensen wrote:. . . . My professor sent it to the class to modify. Not sure why he is doing anything honestly.
Junilu Lacar wrote:That base code is not object-oriented code and it will take much more beyond just converting those constants to enums to make it better. Are you a beginner Java programmer or are you actually up-and-up on the concepts of object-oriented programming? Have a look at this example that I posted a week ago: https://coderanch.com/t/678724/java/Merge-Halves-Sorted-Array#3182773 and see if you can follow what is being done with the enums. See if you can understand how that code is more object-oriented than the code you've been given, which isn't OO at all.
chad jensen wrote:Sorry to vent, but yes, I consider myself a beginner.
Junilu Lacar wrote:It would be fine if the student had enough context to actually be able to understand what to do and how to do it properly. It's more problematic when the student is in way over his/her head. The latter seems to be the situation with OP.
Junilu Lacar wrote:Please don't use color tags -- just put inline comments in the code that you post.
Junilu Lacar wrote:Chad, this is what I was saying about you not having a good context. Context is what gives you the big picture view and understanding of things. Based on the comments you added, you seem to have a pretty good understanding of the nuts and bolts of the program. I think a good analogy would be that you were sitting down somewhere and saying "This is a chair, this is a window, this is a radio, this is a belt, this is a door, these are the hinges that keep the door in place, this is so I can lock the door, this is so I can open the window, this is so I can control the radio volume, ... " but you never realize nor recognize that you're sitting in a car.
I think the most astute comment you have there is "That is main method code. not sure why they would have it in a class."
Here's the answer: It doesn't have to be in the Card class; main() just has to be in some class, and since Card is the central concept to this program, whoever wrote that example code chose it to hold the main() method. It very well could have been in a different class, like a CardGame or CardDemo or CardExample class. If that were the case, you would run the program by doing one of the following:
Campbell Ritchie wrote:Also, read what it says about enumerated types in the Java™ Tutorials and the Java® Language Specification (=JLS). That is one of the few parts of the JLS which beginners can understand.
Why have you got static methods? Except for the main method, I have a rule of thumb:As MS has already hinted, it is non‑object‑oriented programming to do that when you can override the toString method instead.If you haven't got a good explanation for the keyword static, it is a mistake.
I have corrected that. I think many of those comments should be deleted, however. There are some comments which are uselessThat sort of thing is simply a waste of space because it doesn's tell you anything that isn't blatantly obvious to any passers‑by. What follows is even worse, because it doesn't tell you the one thing you actually want to know, viz why on Earth you are starting at 3:-Junilu Lacar wrote:Please don't use color tags -- just put inline comments in the code that you post.
I didn't give you those hints for nothing. Have you read those links?chad jensen wrote:
Campbell Ritchie wrote:Also, read what it says about enumerated types in the Java™ Tutorials and the Java® Language Specification (=JLS). That is one of the few parts of the JLS which beginners can understand.
That fulfils the letter of the law, but not the spirit. Yes, you can gain access to static members of a class via objectName.memberName(), but it should also say that is bad style. It should have told you that the recommended form is always ClassName.memberName() because static members belong to the class rather than to a particular object.. . . This is a quote directly from my book. . . . We can access a class’s public static members through a reference to any object of the class, or by
qualifying the member name with the class name and a dot ( . ), as in Math.random() . . . .
If only Oracle would change the wording of that dreadful error message, which confuses beginners by the million. If only they would stop saying non‑static and write instance instead, which is the correct term.. . . When I try to use certain variables I get the "non-static method cannot be referenced from a static context" message. I know if I create an object out of the variable I am calling I won't need a static method but I am trying to figure out how to do that.
Campbell Ritchie wrote:I think many of those comments should be deleted, however. There are some comments which are useless
chad jensen wrote:This is a quote directly from my book.
Class Scope
... To access a private static member when no objects of the class exist, provide a public static method and call it by qualifying its name with the class name and a dot.
That is why I have static methods. When I try to use certain variables I get the "non-static method cannot be referenced from a static context" message. I know if I create an object out of the variable I am calling I won't need a static method but I am trying to figure out how to do that.
Campbell Ritchie wrote:
I didn't give you those hints for nothing. Have you read those links?chad jensen wrote:
Campbell Ritchie wrote:Also, read what it says about enumerated types in the Java™ Tutorials and the Java® Language Specification (=JLS). That is one of the few parts of the JLS which beginners can understand.
That fulfils the letter of the law, but not the spirit. Yes, you can gain access to static members of a class via objectName.memberName(), but it should also say that is bad style. It should have told you that the recommended form is always ClassName.memberName() because static members belong to the class rather than to a particular object.. . . This is a quote directly from my book. . . . We can access a class’s public static members through a reference to any object of the class, or by
qualifying the member name with the class name and a dot ( . ), as in Math.random() . . . .
Always quote the source, in this case name of book, author and if possible page number.If only Oracle would change the wording of that dreadful error message, which confuses beginners by the million. If only they would stop saying non‑static and write instance instead, which is the correct term.. . . When I try to use certain variables I get the "non-static method cannot be referenced from a static context" message. I know if I create an object out of the variable I am calling I won't need a static method but I am trying to figure out how to do that.
If you use an enum, which I thought you had been told to (you will get dreadful marks if you shou&wj;ld use an enum and you use numbers), you may not need to create instances. I don't know how many instances of what you need, because I haven't been through the code carefully enough. Sorry. The elements in an enum already are full‑blown objects. We have an FAQ about the main method, which should show you how to create instances. I am very surprised you haven't been taught how to create instances. As long as you weren't asleep during that lecture
![]()
No, that is mistaken. They are recorded in the object, or until their values are changed until the object is no longer required. You might be thinking of constructor arguments there.chad jensen wrote:. . . I do understand that instance variables are just variables that are used to create an object then they are thrown away. . . .
Campbell Ritchie wrote:
No, that is mistaken. They are recorded in the object, or until their values are changed until the object is no longer required. You might be thinking of constructor arguments there.chad jensen wrote:. . . I do understand that instance variables are just variables that are used to create an object then they are thrown away. . . .