user3281749
user3281749

Reputation: 11

What is the easy way to accept about one hundred inputs for a java method?

I am using java to make a game. I want to make a method that makes it easy to create a world with just a couple lines. I want to have a method like this...

public void makeLevel(see following*)
{
if (... == 1
{
drawGrass(0, 16);
}
......
}

I want to call it like this

makeLevel(1,2,1,1,2,2,1,3,2,1,2,2,1,2,1,2,3,2,2,2,2,1,2,1,2,2,1,2,1,2,1,2,3,2,1,2,3);

I will also have a method called drawGrass and drawWater that will use Graphics2D to draw these pictures.

I don't think that you need my code but if you do just tell me and I will put it here.

I was thinking that i could somehow use an array or somthing like that.

Upvotes: 1

Views: 117

Answers (6)

jmrah
jmrah

Reputation: 6221

May I suggest an improvement? I think the biggest 'problem' here is firstly, there are too many parameters, and secondly, there are too many 'magic' numbers. Think about the person using this method to create a level. What does the number 3 mean? what about 16? It is not apparent right off the bat.

So, the first improvement I would make is turning those numbers into meaningful types. Perhaps put them in an enum, like:

public enum Terrain
{
    Grass,
    Sand,
    Water,
    Bush
}

If you are familiar with objects and classes, I would take it a step further and would make a parent Terrain class and a bunch of different sub-types, like Grass, Sand, Water, etc. Each sub type would contain it's own image and would know how to draw itself.

The second improvement deals with the large number of parameters. If you are not familiar with classes and objects, then a 2D array of Terrain enum types would probably be best. If you are comfortable with objects, then I would create a World object which would have methods such as Add(Terrain item).

I hope that helps.

Upvotes: 1

AlfredoCasado
AlfredoCasado

Reputation: 818

A better option it you want to make a friendly API its to use a fluent interface (http://en.wikipedia.org/wiki/Fluent_interface), instead of passing one hundred parameters (that is obviously not a good idea), you can do something like:

import static mylibrary.createALevel;

Level mylevel = createALevel().withWater(...)
                              .withGrass(...)
                              .withXXX(...)
                              .create()

Easy to write and easy to understand, and with other important benefits (for example if some of your configurations are optional)

Upvotes: 4

Mark Giaconia
Mark Giaconia

Reputation: 3953

if you need to categorize the values, try a Map

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234817

Please don't use 100 inputs. Use a data structure (perhaps an array, or a Map or custom class if you want to name your arguments) to hold your data.

Alternatively, you might want to use variadic arguments. For instance:

public void doSomething(int... args) {
    // treat args as an int[]
}

which can be called with:

doSomething(1, 2, 3);
doSomething(1,2,3,4,5,6,7,8);
// etc.

but 100 arguments in calling code is just cruel.

Upvotes: 6

Roxxorfreak
Roxxorfreak

Reputation: 380

Put the values into a vector or an array list and pass this object!

Upvotes: 4

Justin Niessner
Justin Niessner

Reputation: 245429

Accept an Array rather than individual inputs.

Upvotes: 5

Related Questions