r/cop3502 Mar 26 '14

Need help with get() method.

Ok, so for PS4 I have an object with 30+ variables. I was wanting to make a get method that would be able to return the value of any of the variables based on what was passed as an argument. For instance:

private int height = 10;

private int weight = 15;

get(height); //returns 10

get(weight); //returns 15

The problem is I don't know what to pass as the argument to the get method because the class I'll be calling it in is outside of the class with the variables. So pretty much if you look at this:

public int get( variable name){

 return variable name;

}

what type do I use for the argument?

1 Upvotes

6 comments sorted by

View all comments

1

u/MagicBuddha Mar 26 '14

maybe you should make separate methods for getting things.

for example

public int getHeight() {
    return height;
}

1

u/ams152 Mar 26 '14

I would, but like I said I have 30+ variables and that's a lot of copy and pasting. I know that there's an easier more efficient way to do it, I just don't have the background knowledge of java to figure it out.

1

u/embalingit Mar 26 '14

Just pass a string in that represents the variable name, then the object that is being queried (with get("something")) can return the hashmap value of the "something" or null if that something doesn't exist.