足球队:对象和类

时间:2017-01-15 22:40:39

标签: java arrays class object instances

在发布我的问题之前,我只需要指导如何改进我的java程序,因为我想学习。

因此,对于我的任务,我需要:

创建3个班级,应用程序,足球运动员和足球队。应用程序(app)将使用其他两个类别(足球运动员和足球队)。

应用

  • 创建11个足球运动员(你可以把他们放在一个阵列或 的ArrayList)
  • 使用上面的玩家创建一支足球队 足球队实例(对象)显示所要求的信息 在实验室里

足球运动员班级

  • 至少有5个属性 您选择的方法是返回有关播放器的完整信息

足球队的课程

  • 一个名字和一个吉祥物
  • 11名足球运动员
  • 任何其他属性(可选)
  • 显示团队所有信息的方法,包括:
    • 名称
    • 吉祥物,以及团队中每位球员的信息
    • 使用诸如玩家位置或玩家号码之类的输入参数来显示关于团队中的特定玩家的信息的方法。例如,来自A队,显示有关四分卫的信息,或显示有关球员号码5的信息。

我和我的团队课混淆了。我不知道如何向班级添加11名玩家,并为玩家实例提供输入参数。我不确定我的应用课程,因为我不知道如何使用足球队实例(对象)来显示实验室要求的信息。

这是我到目前为止所拥有的

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;
     }
}

5 个答案:

答案 0 :(得分:0)

在你的团队课程中,在String team和String mascot下面,你需要一个player [] player数组。在你的构造函数中,传入一个包含所有玩家的数组。

答案 1 :(得分:0)

2个选项 -

在你的main方法中,你创建一个数组并将所有玩家添加到它,然后将它传递给你的Team构造函数,它将内部数组设置为传入数组的值

或您的Team对象有一个名为addPlayer(player)的方法,它将玩家添加到Team的内部玩家阵列中。然后在每个玩家创建后调用该方法。

你也应该大写你的班级名称..比如团队,玩家等。

答案 2 :(得分:0)

我没有为实例变量编写gettersetter方法,因为我认为你可以解决这些问题。但是根据你对Team类的描述,它可能看起来像这样。

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);
    }

}

为了匹配此Team课程的实施,您的App应该更像这样,以便能够将已经构建的List<Player>传递给addPlayers Team类的方法......

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);
    }
}

答案 3 :(得分:0)

因为你有11个玩家,所以最好创建一个对象数组,因为它们共享相似的属性。像这样:

Player[] players = new Player[11];

然后添加他们的参数,然后将其传递给app的构造函数类,其中包含一个palyers列表。

答案 4 :(得分:0)

我已更新您的解决方案。这是您创建类和方法的方法。 有关更多信息,请参阅内联注释。希望这会有所帮助。

            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();
        }
    }