Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When you run it, a listof name comes out. How do you sort the names within eachc

ID: 3609313 • Letter: W

Question

When you run it, a listof name comes out. How do you sort the names within eachcategory.

public class PlayersAndTeam
{
    public static void main(String[] args)
    {    //I fixed a few typoshere
        Input3 i3=newInput3();
        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]=new Team(tName, players[0], players[1],players[2],players[3], players[4]);  
        }
       System.out.println(teams[0]);
        /*you can't saySystem.out.println(toString());
         *at least in thiscase you cant. It is looking for the toString method in thePlayersAndTeam class.
         * It isn't hereand what you want to print out is the toString of the Teamobject.
         *System.out.print(teams[0]); automatically calls the toString objectbecause you are printing an object
         * Anytime youprint or put an object in a string, the toString method iscalled.
         */
       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
        public StringtoString()
        {
           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 s = (teamName +": "+o+", "+t+", "+th+", "+fo+", "+fi);
           return s;
        }
    }
   
  
} When you run it, a listof name comes out. How do you sort the names within eachcategory.

public class PlayersAndTeam
{
    public static void main(String[] args)
    {    //I fixed a few typoshere
        Input3 i3=newInput3();
        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]=new Team(tName, players[0], players[1],players[2],players[3], players[4]);  
        }
       System.out.println(teams[0]);
        /*you can't saySystem.out.println(toString());
         *at least in thiscase you cant. It is looking for the toString method in thePlayersAndTeam class.
         * It isn't hereand what you want to print out is the toString of the Teamobject.
         *System.out.print(teams[0]); automatically calls the toString objectbecause you are printing an object
         * Anytime youprint or put an object in a string, the toString method iscalled.
         */
       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
        public StringtoString()
        {
           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 s = (teamName +": "+o+", "+t+", "+th+", "+fo+", "+fi);
           return s;
        }
    }
   
  
}

Explanation / Answer

//Hope this will help you...

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]);
        /*you can't saySystem.out.println(toString());
         *at least in thiscase you cant. It is looking for the toString method in thePlayersAndTeam class.
         * It isn't hereand what you want to print out is the toString of the Teamobject.
         *System.out.print(teams[0]); automatically calls the toString objectbecause you are printing an object
         * Anytime youprint or put an object in a string, the toString method iscalled.
         */
       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
        public StringtoString()
        {
           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= new String[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;
        }
    }
   
  
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote