• Post Reply Bookmark Topic Watch Topic
  • New Topic

ParseCard Method call to main  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am given this method called parseCard and it takes a String and returns the corresponding card.
This is the method:


My question is how do I call this method to main?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using all those if statements you could use a single for loop.



And to call that method from main method, you have to create an object of the class where you have written that method as the method is non-static method and you are calling it from the static context as main method is static. And then call that method from that object reference and as your method is returning Cards object you have to assign the result the Cards.
such as
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:
And to call that method from main method, you have to create an object of the class where you have written that method as the method is non-static method and you are calling it from the static context as main method is static. And then call that method from that object reference and as your method is returning Cards object you have to assign the result the Cards.
such as


Could you rephrase this? I am a bit confused on it. I somewhat understand it but I need to reread it worded differently.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method "parseCard" that you want to call from "main" method is non-static. And as "main" method is static, it won't allow to call a non-static method from there. Thus, you need to create an object of the class where you have written that parseCard method. and from that object, you'll have to call the parseCard method. And as the parseCard method returns the object of Cards class. you need to take the result in Cards object such as
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't I just change the "parseCard" method to static?
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:Couldn't I just change the "parseCard" method to static?

Yes you can change it to static. Then you can directly call the parseCard method from main method without creating an object of the class where the parseCard method is declared as the context of both the methods is static.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The decision to make the method static should not be based on whether or not it is called from the static main method or any static context for that matter.  you should see if the method requires information that an instance object would have. In this case, it doesn't because all the information the method needs to do its job is in the String argument that is passed to it. Therefore, it is appropriate to make this a static method. It's the same reason methods like Integer.parseInt() and Integer.valueOf() are static methods.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question I would ask is why is the class named "Cards" (plural) when an object of this type represents only ONE card?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this design won't allow you to go the other way, from Card to String. How are you supposed to tell if an object created using new Card(2, 10) represents a 10, Jack, Queen, or King of Hearts?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:I am given this method called parseCard and it takes a String and returns the corresponding card.
This is the method:

My question is how do I call this method to main?

I assume you meant "call from main()" -- my question would be, was this code provided to you like this? If so, then you'd need an instance of the class that this method is found in. Otherwise, you'd make it a static method as already discussed.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The question I would ask is why is the class named "Cards" (plural) when an object of this type represents only ONE card?

Because there are other methods in the program that do things with multiple cards.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
I assume you meant "call from main()" -- my question would be, was this code provided to you like this? If so, then you'd need an instance of the class that this method is found in. Otherwise, you'd make it a static method as already discussed.


If I make that method static, and call it from main like this:
I get a compiler error:


How would I make the variables static?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . How would I make the variables static?
You don't. I think that compiler error message is misleading; it shou‍ld say you have to call instance methods from an instance context. What you need to do is move the call out of a static context.
As far as I am concerned, if you don't know why you are in a static context, then using static is a serious error.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
Junilu Lacar wrote:The question I would ask is why is the class named "Cards" (plural) when an object of this type represents only ONE card?

Because there are other methods in the program that do things with multiple cards.

That's not a good reason at all.

That's like saying that just because it has methods that deal with multiple people, you would name a class "People" even though an instance only represents ONE person. Would you really name a class like that People instead of the grammatically correct name, Person?

Does this code look right to you?

Are you a "people" or a "person"?

So, does this code look right to you?

If you say "Yes" then do you mean to say that "His cards was the Ace of Diamonds." is a grammatically correct sentence? Code that is not grammatically correct confuses people reading it. It should be changed.

Do you see what I'm getting at?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may think that I'm nitpicking about the name and the associated grammar but it's not nitpicking. Clarity and expressiveness are very important when it comes to writing good, clean code that helps the reader understand its purpose. Code that is grammatically incorrect misleads the reader and is likely to create misunderstanding and/or confusion. Misunderstanding what code does is the leading cause of bugs, loss of hair, sleep, appetite, patience, and eventually sanity. Don't be evil. Choose good names for your program entities.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
If I make that method static, and call it from main like this: I get a compiler error:...

Why would you call it that way when you've declared parseCard() to take ONE argument of type String? Do you understand how parseCard() is supposed to be called when you declare it like that?

Does this call:

match this declaration?

Sorry to say this but sometimes it seems like you're just guessing at this stuff. You can't write a correct program by guessing. You need to understand how all the pieces of your program fit together. Those two pieces don't fit each other. It's like trying to put a 2-prong plug into a 1-hole socket; it can't be done. Does that make sense?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!