zcraven said:
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3,
OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6,
OutfieldPlayer p7,
OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)
{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}
if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
This is pointless. You already know that g1 is a Goalkeeper and that p1
- p11 are OutfieldPlayers because those are the declared types of the
method arguments.
Well, it's not *completely* pointless: it will catch null arguments and
throw IllegalArgumentException in that case, but that doesn't appear to
be the intent.
As Joona already wrote, this simply _demands_ an array. If ever you
find yourself writing a method with more than about 3 arguments then you
should consider whether there is a better way. If you ever find
yourself writing a method with more than about 6 arguments then you
should assume that there is a better way and look hard for it. The
better way might be to split the method into two or more smaller
methods, or, as in this case, might be to use a higher-level argument
such as an array or other collective Object. There are other possibilities.
If you wrote:
public void setFirst11(Goalkeeper g, OutfieldPlayer[] ofPlayers) {
[...]
Then you still wouldn't need any argument type checking.