Write a program that stores the name as a String and number as int's of 11 players

1.4k Views Asked by At

How do I get the average points from each player of both games?

Write a program that stores the name as a String and numbers as int's of 11 players on a team. Your program should display/output the average points scored of each player over the two games.

Nick (34), Joey (33), Ken (24), Ryan (11), Josh (3), Simon (6), Dillon (10), Mike (28), and Cameron (5).

Game 1: 4, 3, 3, 2, 4, 5, 6, 6, 7, 7, 8. Game 2: 8, 4, 3, 3, 5, 5, 7, 8, 8, 9, 5.

import java.util.Scanner;
import java.util.Arrays;
public class BasketballStats {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] jerseyNumber = {34, 33, 24, 11, 3, 13, 6, 4, 28, 10};

    String[] playerName = {"Jim", "Joe", "Ken", "James", "John", "Bud", 
"Clark", "Barry", "Jose", "Paul"};

    int[] game1 = new int[11];
    int[] game2 = new int[11];

    for (int i = 0; i < 10; i++)

            System.out.printf("Number %d: %s \n" , jerseyNumber[i], 
playerName[i]);
}

public void setGame1Score() {

int[] jerseyNumber = {34, 33, 24, 11, 3, 13, 6, 4, 28, 10};
String[] playerName = {"Jim", "Joe", "Ken", "James", "John", "Bud", "Clark", 
"Barry", "Jose", "Paul"};

Scanner input = new Scanner (System.in);
    for (int i = 0; i < 10; i++) {
        System.out.println("For game 1, how many points does he score?");
        int game2 = input.nextInt();

} 

    }

}
1

There are 1 best solutions below

0
On

Things tend to get very tedious when you handle data attributes in individual arrays. Conceptually, it makes more sense in this scenario to treat a Player as a unique Object to hold their name, number and scores.

public class Player {
    private String name;
    private int number;
    private int scores[];
    private int currentGame = 0;

    public Player(String name, int number, int numGames) {
       this.name = name;
       this.number = number;
       this.scores = new int[numGames];
    }
    // Getters and Setters
    public String getName() { return name; } // Required

    public void addScore(int score) { 
        if (currentGame < scores.length) {
            scores[currentGame++] = score;
        }

    }

    public int getAverage() {
        int total = 0;
        for (int i = 0; i < currentGame; i++) {
           total += scores[i];
        }
        return total / currentGame;
    }

    @Override
    public String toString() {
        return String.format("%s (%s) : avg %d", name, number, getAverage());
    }
}

You then can instantiate a player with their name, number and how many games they played. You might, however, like to treat the Player class as a single game and have an Object that describes each Game itself; consisting of 10 Players. The Object is lacking getters/setters for the name and number attribute.

Then you can create a single array to hold all data for a game. If you plan on having multiple games, you may want to have an array of scores instead of a single score per Player.

public static final String[] playerName = {"Jim", "Joe", "Ken", "James", "John", "Bud", 
"Clark", "Barry", "Jose", "Paul"}; 
public static final int[] jerseyNumber = {34, 33, 24, 11, 3, 13, 6, 4, 28, 10};

public Player[] createGame(int numGames) {
    Player[] players = new Player[10];

    for (int i = 0; i < 10; i++) {
        players[i] = new Player(playerName[i], jerseyNumber[i], numGames);
    }

    return players;
} 

Then when you will need to get the score somehow.

public void getScore(Player[] players, int numGames) {
    Scanner input = new Scanner(System.in);
    for (int i = 0; i < numGames; i++) {
        System.out.println("Game " + i);
        for (Player player : players) {
            System.out.print(player.getName() + " : ");
            player.addScore(input.nextInt());
        }
    }
}

public void getAverage(Player[] players) {
    for (Player player : players) {
        System.out.println(player.toString());
    }
}