simple IF question if (newplayer == goalkeeper) {throw error}

Z

zcraven

Joona I Palaste said:
zcraven <[email protected]> scribbled the following







Ah yes. If you have defined your method parameters like that, then by
the time your method is even called, you are already guaranteeing to
the compiler that g1 is of type Goalkeeper and p2 through p11 are of
type OutfieldPlayer, so the whole if statement in the method becomes
needless.
I had thought that all parameters, both g1 and p2 through p11, were of
type Player. If they are then the code I showed (for this method) should
compile.
Could you also show the code for the method that is calling this method?

public static Club createCLUBManUtd()
{
Club c1 = new Club("Man Utd");
Goalkeeper g1 = new Goalkeeper(c1, "Peter", "Tom", "Schmeichal",
195, 1973, 8, 3);
Goalkeeper g2 = new Goalkeeper(c1, "Roy", "Phil", "Carroll", 189,
1980, 9, 7);
OutfieldPlayer p1 = new OutfieldPlayer(c1, "Micheal", "Abbey",
"Silvestre", 179, 1974, 12, 2, "defender");
OutfieldPlayer p2 = new OutfieldPlayer(c1, "Rio", "John",
"Ferdinand", 185, 1967, 04, 25, "defender");
OutfieldPlayer p3 = new OutfieldPlayer(c1, "Phil", "Bob", "Neville",
169, 1975, 11, 2, "defender");
OutfieldPlayer p4 = new OutfieldPlayer(c1, "Gary", "Steve",
"Neville", 166, 1974, 2, 2, "defender");
OutfieldPlayer p5 = new OutfieldPlayer(c1, "Claude", "Scot",
"Heinz", 159, 1969, 2, 4, "defender");
OutfieldPlayer p6 = new OutfieldPlayer(c1, "Christaino", "Alex",
"Ronaldo", 178, 1982, 10, 12, "midfielder");
OutfieldPlayer p7 = new OutfieldPlayer(c1, "Ryan", "Jamie", "Giggs",
195, 1973, 1, 2, "midfielder");
OutfieldPlayer p8 = new OutfieldPlayer(c1, "Paul", "John",
"Scholes", 179, 1973, 4, 7, "midfielder");
OutfieldPlayer p9 = new OutfieldPlayer(c1, "Wayne", "OAP", "Rooney",
175, 1975, 3, 7, "striker");
OutfieldPlayer p10 = new OutfieldPlayer(c1, "Loius", "Sean", "Saha",
177, 1976, 9, 12, "striker");
OutfieldPlayer p11 = new OutfieldPlayer(c1, "Ruud", "Stan", "Van
Nistelrooy", 180, 1977, 12, 9, "striker");
OutfieldPlayer p12 = new OutfieldPlayer(c1, "Alan", "Steve",
"Smith", 179, 1980, 10, 10, "striker");
c1.addPlayer(g1);
c1.addPlayer(g2);
c1.addPlayer(p1);
c1.addPlayer(p2);
c1.addPlayer(p3);
c1.addPlayer(p4);
c1.addPlayer(p5);
c1.addPlayer(p6);
c1.addPlayer(p7);
c1.addPlayer(p8);
c1.addPlayer(p9);
c1.addPlayer(p10);
c1.addPlayer(p11);
c1.addPlayer(p12);
c1.setFirst11(g1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
c1.updateClubStats(0,2);
c1.updateClubStats(0,4); // creates equal points and goal
difference to arsenal, but a higher goals scored.
c1.updateClubStats(0,0);

return c1;
}

This is a 'test data' method that creates a club. this is the only method
so far that calls the club.setFirst11 method.

Probably I will change all the values in the setFirst11 method header to
'Player', and then check them for goalkeeper/outfield as you suggested.
 
Z

zcraven

i know, but this method is just for picking the team before the match
starts - the program is not that complicated to update the team throughout a
match etc.

zac
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
public static Club createCLUBManUtd()
{
Club c1 = new Club("Man Utd");
Goalkeeper g1 = new Goalkeeper(c1, "Peter", "Tom", "Schmeichal",
195, 1973, 8, 3);
Goalkeeper g2 = new Goalkeeper(c1, "Roy", "Phil", "Carroll", 189,
1980, 9, 7);
OutfieldPlayer p1 = new OutfieldPlayer(c1, "Micheal", "Abbey",
"Silvestre", 179, 1974, 12, 2, "defender");
OutfieldPlayer p2 = new OutfieldPlayer(c1, "Rio", "John",
"Ferdinand", 185, 1967, 04, 25, "defender");
OutfieldPlayer p3 = new OutfieldPlayer(c1, "Phil", "Bob", "Neville",
169, 1975, 11, 2, "defender");
OutfieldPlayer p4 = new OutfieldPlayer(c1, "Gary", "Steve",
"Neville", 166, 1974, 2, 2, "defender");
OutfieldPlayer p5 = new OutfieldPlayer(c1, "Claude", "Scot",
"Heinz", 159, 1969, 2, 4, "defender");
OutfieldPlayer p6 = new OutfieldPlayer(c1, "Christaino", "Alex",
"Ronaldo", 178, 1982, 10, 12, "midfielder");
OutfieldPlayer p7 = new OutfieldPlayer(c1, "Ryan", "Jamie", "Giggs",
195, 1973, 1, 2, "midfielder");
OutfieldPlayer p8 = new OutfieldPlayer(c1, "Paul", "John",
"Scholes", 179, 1973, 4, 7, "midfielder");
OutfieldPlayer p9 = new OutfieldPlayer(c1, "Wayne", "OAP", "Rooney",
175, 1975, 3, 7, "striker");
OutfieldPlayer p10 = new OutfieldPlayer(c1, "Loius", "Sean", "Saha",
177, 1976, 9, 12, "striker");
OutfieldPlayer p11 = new OutfieldPlayer(c1, "Ruud", "Stan", "Van
Nistelrooy", 180, 1977, 12, 9, "striker");
OutfieldPlayer p12 = new OutfieldPlayer(c1, "Alan", "Steve",
"Smith", 179, 1980, 10, 10, "striker");

Declare these variables as type Player, for example:
Player g1 = new Goalkeeper(c1, "Peter", "Tom", "Schmeichal",
195, 1973, 8, 3);
Player p1 = new OutfieldPlayer(c1, "Micheal", "Abbey",
"Silvestre", 179, 1974, 12, 2, "defender");

Note that only the variable can be of type Player. The actual objects
still need to be Goalkeepers and OutfieldPlayers.
c1.addPlayer(g1);
c1.addPlayer(g2);
c1.addPlayer(p1);
c1.addPlayer(p2);
c1.addPlayer(p3);
c1.addPlayer(p4);
c1.addPlayer(p5);
c1.addPlayer(p6);
c1.addPlayer(p7);
c1.addPlayer(p8);
c1.addPlayer(p9);
c1.addPlayer(p10);
c1.addPlayer(p11);
c1.addPlayer(p12);
c1.setFirst11(g1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);

If the variables are of type Player, you can define setFirst11() as
setFirst11(Player g1, Player p2, Player p3, Player p4, Player p5,
Player p6, Player p7, Player p8, Player p9, Player p10, Player p11)
which will make the code I suggested work.
c1.updateClubStats(0,2);
c1.updateClubStats(0,4); // creates equal points and goal
difference to arsenal, but a higher goals scored.
c1.updateClubStats(0,0);
return c1;
}
This is a 'test data' method that creates a club. this is the only method
so far that calls the club.setFirst11 method.
Probably I will change all the values in the setFirst11 method header to
'Player', and then check them for goalkeeper/outfield as you suggested.

Yes, that's exactly right.
That solves this problem, but as has been said before, your code also
has a design problem - you're using individual variables where you could
just as well be using an array. An array would make writing and
debugging your code much faster and easier.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"As a boy, I often dreamed of being a baseball, but now we must go forward, not
backward, upward, not forward, and always whirling, whirling towards freedom!"
- Kang
 
Z

zcraven

Joona I Palaste said:
zcraven <[email protected]> scribbled the following
method?

Declare these variables as type Player, for example:
Player g1 = new Goalkeeper(c1, "Peter", "Tom", "Schmeichal",
195, 1973, 8, 3);
Player p1 = new OutfieldPlayer(c1, "Micheal", "Abbey",
"Silvestre", 179, 1974, 12, 2, "defender");

Note that only the variable can be of type Player. The actual objects
still need to be Goalkeepers and OutfieldPlayers.


If the variables are of type Player, you can define setFirst11() as
setFirst11(Player g1, Player p2, Player p3, Player p4, Player p5,
Player p6, Player p7, Player p8, Player p9, Player p10, Player p11)
which will make the code I suggested work.





Yes, that's exactly right.
That solves this problem, but as has been said before, your code also
has a design problem - you're using individual variables where you could
just as well be using an array. An array would make writing and
debugging your code much faster and easier.

i know but i dont really understand how to get the players into that array.
it seems like i will still have to read in a load of players and put them
into an array, which is exactly what the setFirst11 method wants to do
anyway - so I would have the same bad code problem, PLUS an extra method
which doesnt do much.

zac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top