//The next sixinputs will be for the second team input[6] = \"MAVERICKS\"; input
ID: 3609293 • Letter: #
Question
//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
//Hope this will helpyou..class PlayersAndTeam
{
public static void main(String[] args)
{ //I fixed a few typoshere
Input3 i3=newInput3();
Team[]teams = newTeam[3];
System.out.println("Hi..... ");
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]=new Team(tName, players[0], players[1],players[2],players[3], players[4]);
}
System.out.println(teams[0]);
print or put an object ina string, the toString method is called.
System.out.println(teams[1]);
System.out.println(teams[2]);
}
/** Input.java */
//class needs to be static to be accessed bystatic method
static class Input3
{
private String[] input = new String[40];
private int StringNum = -1; //can'tbe static because it is called on by a non-static method
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 six inputs 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 six inputs 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 method returns the strings one after the other.
public String getNextString()
{
StringNum++;
return input[StringNum];
}
}
//class needs to be static to be accessed bystatic method
static class Player
{
private String name;
private int score;
public Player(String n,int s)
{ name=n; score=s;}
//!!!!you also need thetoString here
publicString toString()
{
return name;
}
}
//class needs to be static to be accessed bystatic method
static class 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; }
public StringtoString()
{
String [] ss= newString[5];
String s1;
int i,j;
ss[0]=o.toString();
ss[1]=t.toString();
ss[2]=th.toString();
ss[3]=fo.toString();
ss[4]=fi.toString();
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
{
if(ss[i].compareTo(ss[j]) >0)
{
s1 = ss[i];
ss[i]=ss[j];
ss[j]=s1;
}
}
String s = (teamName +": "+ss[0]+", "+ss[1]+", "+ss[2]+","+ss[3]+", "+ss[4]);
return s;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.