• 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

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Position in what?
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again--position in what? Does each dog know its position? How are the doggies stored?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read TellTheDetails
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...

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.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you could not give enough information, it is hard for us to help.
Did you try to get help from your professor?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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))
 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic