Forums Register Login

Help with a get method that returns positions of a variable.

+Pie Number of slices to send: Send
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));

I appreciate any help you can give me.

Thanks!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Position in what?
+Pie Number of slices to send: Send
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)?
+Pie Number of slices to send: Send
Again--position in what? Does each dog know its position? How are the doggies stored?
+Pie Number of slices to send: Send
Please read TellTheDetails
+Pie Number of slices to send: Send
No the dogs don't know their positions at all. That is what I'm not understanding how to do. The book, nor the lectures, talked about this.
Thanks!
+Pie Number of slices to send: Send
...

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.
+Pie Number of slices to send: Send
If you could not give enough information, it is hard for us to help.
Did you try to get help from your professor?
+Pie Number of slices to send: Send
Deena --

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:

+Pie Number of slices to send: Send

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;
}]

Thanks so much!!
+Pie Number of slices to send: Send
 

Deena Lee wrote:
Here is the code I tried:


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.
+Pie Number of slices to send: Send
 

Raymond Tong wrote:

Deena Lee wrote:
Here is the code I tried:


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))
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
 

Raymond Tong wrote:

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.

Edited for proper use of "its".
+Pie Number of slices to send: Send
 

Deena Lee wrote:
[public int getFido(int index)

{
if (fido == 1)
return oneFido;
else if (fido == 2)
return twoFido;
else
return thirdFido;
}]



The method doesnt seem to be using the 'index' thats passed to the method. Look at EFH's post (the wrong way part of it).
+Pie Number of slices to send: Send
Ahhhh .... DUH! ... what a blonde moment. Live and learn ... and I learned a lot from this one.

Thank you, thank you ... to everyone who has helped me!

+Pie Number of slices to send: Send
 

Deena Lee wrote:Ahhhh .... DUH! ... what a . . .

Are you a blonde? Don't worry: everybody does that sort of thing. And not only when they are beginners. And welcome to the Ranch
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1203 times.
Similar Threads
getter not returning subclass instance variable values set by setter
HELP! Connection to MS Access DB Trouble
JTable Problem
Forward Referencing
add text into JTextPane through JButton, but not replace other text
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:02:11.