In the class teams andplayers, I am suppose to call the class Input 3 to get the
ID: 3609292 • Letter: I
Question
In the class teams andplayers, I am suppose to call the class Input 3 to get the teamnames and players, then I am suppose to get the players in theclass teams from the class players. How can I do that.
public classPlayersAndTeam
{
public static void main(String[] args)
{
}
/** Input.java */
public class Input3
{
private String[] input = new String[40];
private static int StringNum = -1;
public static final int NUMBER_OF_TEAMS = 3;
public static final int NUMBER_OF_PLAYERS = 5;
public Input3()
{
//The first six inputs will be for the first team
input[0] = "LAKERS";
input[1] = "Kobe Bryant";
input[2] = "Derek Fisher";
input[3] = "Shaquille O'Neal";
input[4] = "Karl Malone";
input[5] = "Brian Cook";
//The next sixinputs will be for the second team
input[6] = "MAVERICKS";
input[7] = "Antoine Walker";
input[8] = "Dirk Nowitzki";
input[9] = "Tony Delk";
input[10] = "Shawn Bradley";
input[11] = "Travis Best";
//The next sixinputs will be for the third team
input[12] = "KNICKS";
input[13] = "Mike Sweetney";
input[14] = "Allan Houston";
input[15] = "Howard Eisley";
input[16] = "Kurt Thomas";
input[17] = "Shanon Anderson";
}
//This methodreturns the strings one after the other.
public StringgetNextString()
{
StringNum++;
return input[StringNum];
}
}
class Player
{
private String name;
private int score;
public Player(String n,int s)
{ name=n; score=s;}
}
publicclass Team
{
private Player o;
private Player t;
private Player th;
private Player fo;
private Player fi;
private StringteamName;
public Team (StringTname, Player one, Player two, Player three, Player four, Playerfive)
{ teamName=Tname; o=one;t=two; th=three; fo=four; fi=five; }
}
}
Explanation / Answer
/*The reason you were getting staticerrors is because only static methods and variables and classes canaccess and be accessed by other static methods variables andclasses. */public class PlayersAndTeam { public static void main(String[]args) { //I fixed a few typoshere Input3 i3=new Input3(); Team[]teams = newTeam[3]; for(int i = 0; i< 3;i++) { String tName =i3.getNextString(); Player[]players= new Player[5]; for (int a = 0;a<5; a++) players[a]=new Player(i3.getNextString(),0); teams[i]=newTeam(tName, players[0], players[1],players[2], players[3],players[3]); } } /** Input.java */ //class needs to be static to be accessedby static method static class Input3 { private String[] input = new String[40]; private int StringNum = -1; //can't be static because it is called on by a non-staticmethod public static final int NUMBER_OF_TEAMS = 3; public static final int NUMBER_OF_PLAYERS = 5; public Input3() { //The first six inputs will be for the firstteam input[0] = "LAKERS"; input[1] = "Kobe Bryant"; input[2] = "Derek Fisher"; input[3] = "Shaquille O'Neal"; input[4] = "Karl Malone"; input[5] = "Brian Cook"; //The next six inputs will be for the secondteam input[6] = "MAVERICKS"; input[7] = "Antoine Walker"; input[8] = "Dirk Nowitzki"; input[9] = "Tony Delk"; input[10] = "Shawn Bradley"; input[11] = "Travis Best"; //The next six inputs will be for the thirdteam input[12] = "KNICKS"; input[13] = "Mike Sweetney"; input[14] = "Allan Houston"; input[15] = "Howard Eisley"; input[16] = "Kurt Thomas"; input[17] = "Shanon Anderson"; } //This method returns the strings one after theother. public String getNextString() { StringNum++; return input[StringNum]; } } //class needs tobe static to be accessed by static method static class Player { private String name; private int score; public Player(String n, ints) { name=n; score=s;} } //class needs to be static to beaccessed by static method static class Team { private Player o; private Player t; private Player th; private Player fo; private Player fi; private StringteamName; public Team (String Tname,Player one, Player two, Player three, Player four, Playerfive) { teamName=Tname; o=one;t=two; th=three; fo=four; fi=five; } } }