I have an entire program wrote, but I'm having problems with the getMethod. I need a get method which will receive the position of the variable (1,2, or 3) and will then return the variable at that position. Here's an example of the call to the method: System.out.println(dog.getFido(2));
Well let's start with what kind of problem are you having? Compiler error? Runtime error? The code doesn't produce what you expected it to do? It would also help if, along with specifying your problem, you post your code. Before posting your code, please read the UseCodeTags page.
At the moment I'm not getting compiler errors, but it's not returning the correct values. I don't want to post all my code, because the last time i did that another student in the class picked my code up and tried to have the prof. help her with my problem. Needless to say he accused me of cheating!
So here is my getMethod, as of now,
[public int getFido(int index)
{
return Fido;
}]
The problem I'm having is that there are 3 different "fido's", OneFido, TwoFido and ThreeFido, each already set with a different name. How do I get all of them set to the getMethod so that I can print them by calling dog.Fido(1), dog.Fido(2), or dog.Fido(3)?
In order to know how to return their position, we need to know their position in what. Are they in an array? A list? Is the only indication of their position the name of their references (in which case, can't be done)? You have to understand that we don't know your assignment, we don't have your instructor in front of us to ask, we don't know the rest of the code--we simply don't have enough information we can use to help.
There are are two alternatives, I think -- the right way, and the wrong way.
The right way is to use an array or a java.util.ArrayList. Then "position" translates directly into "position in the array or ArrayList". Have you learned about these things in class?
The wrong way is to use a bunch of "if" statements:
I know about arrays, but haven't gotten to that chapter yet. Plus, the instructor told me I can't use them. The instructor isn't much help. His lectures are posted straight from the textbook.
So anyway, I tried the If else statements ... I really liked the idea . However, for all the print statements it returned the name of fido(3) (The print statements were provided in the instructions)
Here is the code I tried:
[public int getFido(int index)
{
if (fido == 1)
return oneFido;
else if (fido == 2)
return twoFido;
else
return thirdFido;
}]
If your index is 4, what would you get in this method ?
By the way, your indentation seems have mix up of spaces and tabs which you should avoid.
Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))
Deena Lee wrote:
Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))
What I am saying is, if you do
What do you expect? Or this statement would not happen anyway ?
Deena Lee wrote:
Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))
What I am saying is, if you do
What do you expect? Or this statement would not happen anyway ?
Ah I see ... Well I wasn't going to concern myself with a fido(4), because the print statements are hard coded and there isn't one for fido(4). But, I guess it doesn't matter if I can't figure out how to set each fido(#) to it's its respective variable that contains it's its name.
Although if I was getting user input I'd have to make sure that fido(4) returned an error statement or a null statement.