Question
Hello, im doing a Java assignment with Arrays and am stuck on getting everything to work properly. I need to get my addGameDetails method working
Hello, im doing a Java assignment with Arrays and am stuck on getting everything to work properly. I need to get my addGameDetails method working in my switch and have the arrays working as instructed. Any help? The Instructions are as follows:
Hockey Tracker Assignment
Create a HockeyPlayer class. This class must have an array which holds the goals that the player scored in each game. It must also have an array which holds the opponent team name. There is a ten game season. The class should also have a name and player number attribute.
The default constructor should prompt for the name and player number. The default constructor should initialize the number of goals to -1. This is used to indicate that the number of goals has not been specified.
create an instance addGameDetails method which returns void. This method should prompt for the game number, opponent name, and number of goals scored by the player. Array should be updated appropriately.
The toString method should return player details as shown in the table below.
Table: Player details
#9 Will MacLean
Game details:
Game 1: Morell 2 goals
Game 3: Souris 0 goals
Game 4: Montague 4 goal
Total games: 3
Total goals: 6
Create a HockeyMain class which has a player array. There are 12 players on the team. Their numbers are from 1-12. Provide a menu which continues until they enter x.
Hockey Tracker
A - Add player
G - Add game details
S - Show players
X - Exit
When A is chosen a new player should be added. Remember their numbers are 1 to 12 and this should be used to determine their index in the array.Note that is good to create the hockey player instance and then use the player number from the object to determine where to put the player in the array.
When G is chosen prompt the user for the player number to determine the correct index and then the addGameDetails from that instance should be invoked.
When S is chosen any players that have been added should be shown.
And this is my current code:
HockeyPlayer Class
import java.util.Arrays; import java.util.Scanner;
public class HockeyPlayer { private final int[] goals = new int[10]; private final String[] teamName = new String[10]; private final int[] gameNumber = new int[10]; private int[] game = new int[10]; private static String name; private static int playerNumber;
public HockeyPlayer() { Scanner input = new Scanner(System.in); int goalsScored = -1; System.out.println("Enter the name of the player"); name = input.nextLine(); System.out.println("Enter the player number 1-12"); playerNumber = input.nextInt();
} public void addGameDetails() { HockeyPlayer Player1 = new HockeyPlayer(); Scanner input = new Scanner(System.in); System.out.println("Enter the game number"); gameNumber [0] = input.nextInt(); //Clear Buffer input.nextLine(); System.out.println("Opponent team name?"); teamName[0] = input.nextLine(); System.out.println("How many goals did "+name+" score"); goals[0] = input.nextInt(); }
public int[] getGoals() { return goals; }
public String[] getTeamName() { return teamName; }
public int[] getGameNumber() { return gameNumber; }
public int[] getGame() { return game; }
public static String getName() { return name; }
public static int getPlayerNumber() { return playerNumber; }
public void setGame(int[] game) { this.game = game; }
public static void setName(String name) { HockeyPlayer.name = name; }
public static void setPlayerNumber(int playerNumber) { HockeyPlayer.playerNumber = playerNumber; }
@Override public String toString() { String output = "Player Details " + "-------------- " + "#" + playerNumber + " " + name + " Game Details:" + " Game" + Arrays.toString(gameNumber) + " Total Games: " + Arrays.toString(game) + " Total Goals: " + Arrays.toString(goals) ;
return output; }
}
My main class HockeyMain
import java.util.Arrays; import java.util.Scanner;
public class HockeyMain { private static HockeyPlayer[] players = new HockeyPlayer[12]; private static String choice;
private static final String MENU = " ***Hockey Tracker*** " + "A- Add player " + "G- Add game details " + "S- Show players " + "X- Exit ";
public static void main(String[] args) { Scanner input = new Scanner(System.in);
do { System.out.println(MENU); choice = input.nextLine(); switch (choice) { case "A": //for(int i = 0; i < players.length; ++i){ HockeyPlayer player = new HockeyPlayer(); players[HockeyPlayer.getPlayerNumber() - 1] = player; break;
case "G": // int playerId = Integer.valueOf(input.nextLine()); // players[playerId - 1].addGameDetails(); break;
case "S": System.out.println(Arrays.toString(players)); }
} while (!choice.equalsIgnoreCase("X"));
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started