'Football Team: Objects and Classes

Before I post my question, I just need guidance on how to improve my java program, because I want to learn.

So, for my assignment I need to:

Create 3 classes, app, football player and football team.The application (app) will use the two other classes (football player and football team).

app will

  • create 11 football players (you can place them in an array or ArrayList)
  • create a football team using the players above use the football team instance (object) to display the information requested in the lab

a football player class

  • has at least 5 attributes from your choice a method that returns the complete info about the player

a football team class has

  • a name and a mascot
  • 11 football players
  • any other attributes (optional)
  • a method that displays all the information about a team including:
    • name
    • mascot, and information on each player in the team
    • a method that display information about a specific player in the team using an input parameter such as the player position or player number for instance. For instance, from team A, displays information about the quarterback, or display information about player number 5.

I'm confused with my team class. I don't know how to add 11 players to the class and provide an input parameter for the player instance. I'm unsure about my app class because I don't know how to use the football team instance (object) to display the information requested in the lab.

Here is what I have so far

public class app {
public static void main(String[] args)
{
        player pl1 = new player
        ("Christian","Campbell","Cornerback","Alabama","Central");
            System.out.println(pl1.getInfo());

        player pl2 = new player
        ("Marcus","Allen","Safety","Maryland","Dr. Henry A. Wise, Jr");
            System.out.println(pl2.getInfo());
        player pl3 = new player 
        ("Tommy","Stevens","Quarterback","Indiana","Decatur Central");
            System.out.println(pl3.getInfo());
        player pl4 = new player
        ("Nyeem","Wartman-White", "Linebacker","Pennsylvania","Valley View");
             System.out.println(pl4.getInfo());
        player pl5 = new player
        ("George", "Foreman", "Defensive back","Georgia","Parkview");
            System.out.println(pl5.getInfo());
        player pl6 = new player
        ("Andre","Robinson","Right Tackle","Pennsylvania","Bishop McDevitt");
            System.out.println(pl6.getInfo());
        player pl7 = new player
        ("Malik","Golden","Safety","Connecticut","Chesire Academy");
            System.out.println(pl7.getInfo());
        player pl8 = new player
        ("Koa","Farmer","Safety","California","Notre Dame");
            System.out.println(pl8.getInfo());
        player pl9 = new player
        ("Jake","Zembiec","Quarterback","New York","Aquinas Institute");
            System.out.println(pl9.getInfo());
        player pl10 = new player
        ("Brandon","Polk","Wide Receiver","Virgina","Briar Woods");
            System.out.println(pl10.getInfo());
        player pl11 = new player
        ("Trace","McSorley","Quarterback","Virgina","Briar Woods");
            System.out.println(pl11.getInfo());


  }

}

public class player {



//---------Declaring attributes----
    String firstName;
    String lastName;
    String position;
    String State;
    String Highschool;


    player (String inf_firstName, String inf_lastName, String inf_position, String inf_State, String inf_Highschool)
{
            firstName = inf_firstName;
            lastName = inf_lastName;
            position = inf_position;
            State = inf_State;
            Highschool = inf_Highschool;
    }
String getInfo()
{
    return "Name: "+firstName+ " "+lastName+", "+"position: " +position+ ", State: " +State+ ", High School: " +Highschool; 
  }

}

public class team {

   String team = "Penn State";
   String mascot = "Nittany Lions";

    team(String inf_team, String inf_mascot)
      {
        team = inf_team;
        mascot = inf_mascot;                             
      }
team t1 = new team("Penn State", "Nittany Lions");

     String getInfo()
     {
       return "Team Name: "+team+ "Team Mascot: "+mascot;
     }
}


Solution 1:[1]

In your team class, below String team and String mascot, you need a player[] players array. And in your constructor, pass in an array that contains all your players.

Solution 2:[2]

2 options -

in your main method you create an array and add all of your players to it, and then pass it to your Team constructor, which sets an internal array to the value of the passed in array

or your Team object has a method called something like addPlayer(player) which adds the player to your Team's internal player array. Then you invoke the method after each player creation.

also you should uppercase your class names.. like Team, Player, etc.

Solution 3:[3]

I didn't write the getter or setter methods for the instance variables, because I think you can figure those out. But from your description of the Team class, it could look something like this.

public class Team {

    //Instance variables and constants
    private static final int MAX_PLAYERS = 11;
    private Player[] players;
    private String name;
    private String mascot;


    //Constructor
    public Team(String name, String mascot) {
        this.name = name;
        this.mascot = mascot;
        this.players = new Player[MAX_PLAYERS];
    }


    //Add players to the team
    public void addPlayers(List<Player> p) {
        //Ensures only 11 players are added to the team
        for (int i = 0; i < MAX_PLAYERS; i++)
            this.players[i] = p.get(i);
    }

}

To match the implementation of this Team class, your App should be more of something like this, to be able to pass an already build List<Player> to the addPlayers method of the Team class...

public class app {
    public static void main(String[] args) {
        List<Player> players = new ArrayList<>();

        players.add(new Player(/* Parameters for each player object */));
        //Not writing this 11 times!

        Team team = new Team("The Bulldogs", "Bull Dog");
        team.addPlayers(players);
    }
}

Solution 4:[4]

Since you have 11 players,It would better of creating an array of objects as they share similar properties. Like this:

Player[] players = new Player[11];

And then add their parameters and then pass it to the constructor class of app, which takes a list of a palyers.

Solution 5:[5]

I have updated your solution. This is how you can create your classes and method. Please see inline comments for more information. Hope this helps.

            public class App {
        public static void main(String[] args) {
            //creating a team
            Team t1 = new Team("Penn State", "Nittany Lions");

            Player pl1 = new Player("Christian", "Campbell", "Cornerback", "Alabama", "Central");
            System.out.println(pl1.getInfo());
            //for example, adding one player to the team t1 created above.

            t1.addPlayer(pl1); 
            //you can repeat this step for adding all the teams

            Player pl2 = new Player("Marcus", "Allen", "Safety", "Maryland", "Dr. Henry A. Wise, Jr");
            System.out.println(pl2.getInfo());
            Player pl3 = new Player("Tommy", "Stevens", "Quarterback", "Indiana", "Decatur Central");
            System.out.println(pl3.getInfo());
            Player pl4 = new Player("Nyeem", "Wartman-White", "Linebacker", "Pennsylvania", "Valley View");
            System.out.println(pl4.getInfo());
            Player pl5 = new Player("George", "Foreman", "Defensive back", "Georgia", "Parkview");
            System.out.println(pl5.getInfo());
            Player pl6 = new Player("Andre", "Robinson", "Right Tackle", "Pennsylvania", "Bishop McDevitt");
            System.out.println(pl6.getInfo());
            Player pl7 = new Player("Malik", "Golden", "Safety", "Connecticut", "Chesire Academy");
            System.out.println(pl7.getInfo());
            Player pl8 = new Player("Koa", "Farmer", "Safety", "California", "Notre Dame");
            System.out.println(pl8.getInfo());
            Player pl9 = new Player("Jake", "Zembiec", "Quarterback", "New York", "Aquinas Institute");
            System.out.println(pl9.getInfo());
            Player pl10 = new Player("Brandon", "Polk", "Wide Receiver", "Virgina", "Briar Woods");
            System.out.println(pl10.getInfo());
            Player pl11 = new Player("Trace", "McSorley", "Quarterback", "Virgina", "Briar Woods");
            System.out.println(pl11.getInfo());

        }

    }

    class Player {

        // ---------Declaring attributes----
        String firstName;
        String lastName;
        String position;
        String State;
        String Highschool;

        Player(String inf_firstName, String inf_lastName, String inf_position, String inf_State, String inf_Highschool) {
            firstName = inf_firstName;
            lastName = inf_lastName;
            position = inf_position;
            State = inf_State;
            Highschool = inf_Highschool;
        }

        String getInfo() {
            return "Name: " + firstName + " " + lastName + ", " + "position: " + position + ", State: " + State
                    + ", High School: " + Highschool;
        }

    }

    class Team {

        String team = "Penn State";
        String mascot = "Nittany Lions";
        Player[] playerArr = new Player[11]; //this is the main part. Array of players (11)
        int playerCount = 0;  //this will track the number of players being added to the team.

        Team(String inf_team, String inf_mascot) {
            team = inf_team;
            mascot = inf_mascot;
        }

        public String getInfo() {
            return "Team Name: " + team + "Team Mascot: " + mascot;
        }

        //Using this method you can add the players to the Player Array.
        public void addPlayer(Player player) {
            if (playerCount < 11) {
                playerArr[playerCount] = player;
                playerCount++;
            }
        }

        //Using this method you find out a player with specific quality 
        //like in this case its position of the player.
        //you can iterate through the Players Array and can find out the player 
        // And after that you can get the information of the player by your method 
        //getInfo
        public String getPlayerInfo(String positionTemp) {
            Player player = null;
            for (int i = 0; i < playerArr.length; i++) {
                if (playerArr[i].position.equals(positionTemp)) {
                    player = playerArr[i];
                    break;
                }
            }
            return player.getInfo();
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Ziebert
Solution 2 Steve Gula
Solution 3 m_callens
Solution 4 thetraveller
Solution 5