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

JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA I need help with the foll

ID: 3720559 • Letter: J

Question

JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA JAVA

I need help with the following program.

EXAMPLE RUN AT THE BOTTOM

Problem Summary
This program will store roster for a soccer team. Coaches rate players to ensure a balanced team.
Each player has a jersey number (0 - 99) and a rating (1 - 9). The roster is stored in a data file.
Each line of the file stores data for one player (jersey number and rating).
Test Data Files (included in the zyLab)

r2.txt
8 7
55 9

r5.txt
8 7
55 9
14 8
22 6
34 8

r12.txt

11 1
22 2
33 3
44 4
55 5
66 6
77 7
88 8
99 9
12 5
23 6
32 7


The program will read the roster data from the specified data file, and store the data into an array of
objects. The program will then allow the user to modify the roster, via a menu of choices (display, add,
update, save, and exit), until the user chooses to exit.
This project contains 3 classes:
? The RosterManager class contains the main() method (outline code provided).
? The Player class has data fields and methods for the player’s jersey number and rating (complete
code provided).
? The PlayerArrayImpl class has data fields and methods for an array of Player objects and a
count of the number of players (outline code provided).
Each step of the instructions is associated with specific tests, so you can test your program as you go
along.
Test 1 (15 pts)
Implement the method to read the data file and store data in the array.
Within the PlayerArrayImpl class, in the readRosterFile() method:
? Define File and Scanner variables
? Open the file specified in the parameter for reading
? Loop:
o Read the jersey number and the rating from one data line
o Create a new Player object, using the data read as the arguments for the constructor
o Store the object in the playerArray
o Increment the count
until all data has been read from the file
? Close the file
NOTES
o Exceptions will not be handled inside this method. They will be thrown to the calling
method.
o Your code should not check to see if the array is full – this error will be handled by the
calling method when the system automatically throws an
ArrayIndexOutOfBoundsException if there is too much data in the file.

Tests 2 & 3 (10 pts)
Try to read the data from the data file and catch any IOExceptions
Within the RosterManager class, in the main() method:
? Within a try block:
o Use the roster object to call the readRosterFile() method
o Use the roster object and a getter to display the number of players data stored
5 players stored
? Within a catch block for an IOException:
o Display the following error messages, including the filename:
**Error reading input file filename.txt
**Program exiting
o Exit the program with: System.exit(99);
Test 4 (5 pts)
Create a method to display a menu and read a choice from the user
Within the RosterManager class, below the main() method,
create a new public static method that:
? Has one parameter – a Scanner to read input from the keyboard
? Displays a blank line, and then the following menu of options for a user to modify the roster:
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
? Reads and returns the user’s uppercased choice character
Test 5 - 6 & Test 16 (20 pts)
Add code to process menu choice and recognize invalid entries
Implement the "Display Roster" menu option
Within the PlayerArrayImpl class, in the displayRoster() method:
? Add the code to display all the roster data stored in the playerArray, formatted as follows:
Player 1 - jersey #8, rating is 7
Player 2 - jersey #55, rating is 9
Within the RosterManager class, in the main() method,
? Loop to:
o Call the menu method and save the returned choice
o Execute a switch statement with a case for each choice
? For choice D, use the roster object to call the displayRoster() method
? For all other choices, just break from the switch statement
? Include a default clause which displays the message:
Error -- invalid menu choice
until the user enters E to exit

Tests 7 & 8 (10 pts)
Implement the "Add a player" menu option
Within the PlayerArrayImpl class, in the addPlayer() method:
? If the playerArray is full, display the message:
Roster is full -- cannot add more players
? Otherwise:
o Create a new Player object, using the parameters as arguments to the constructor
o Store the new Player object in the playerArray and increment the count
o Display the following message that includes the jersey number:
Player 55 successfully added
Within the RosterManager class, in the main() method,
? For choice A:
o Prompt for and read a jersey number and rating
o Use the roster object to call the addPlayer() method
with the jersey number and rating as arguments
Tests 9 – 11 (15 pts)
Implement the "Update a player’s rating" menu option
Within the PlayerArrayImpl class, in the updatePlayerRating() method:
? Search the playerArray for the player with the jersey number specified in the first parameter
(Hint: use a getter to access the jersey number within each Player object)
? If the jersey number is found
o Update that player's rating using the value from the second parameter
(Hint: Use a setter to change the rating within the object)
? Otherwise:
o Display the error message
Jersey number 33 not found -- cannot update rating
Within the RosterManager class, in the main() method,
? For choice U:
o Prompt for and read a jersey number and a new rating
o Use the roster object to call the updatePlayerRating() method
with the jersey number and rating as arguments
Tests 12 & 13 (15 pts)
Implement the "Save roster" menu option
Within the PlayerArrayImpl class, in the writeRosterFile() method:
? Within a try block:
o Define File and PrintWriter variables
o Open the file specified in the parameter for writing
o For each Player object in the playerArray:
? Write the jersey number and the rating to one line of the file, separated by a space
(Hint: Use getters to access the data fields of the Player object)
o Close the file

o Display the following confirmation message including the filename:
Roster saved to file filenameV2.txt
? Within a catch block for IOExceptions:
o Display the following error message including the filename:
**Error: cannot create new roster file filenameV2.txt
Within the RosterManager class, in the main() method,
? For choice U:
o Use some String methods to create a new filename, starting with the input data filename
? Extract the characters before the “.txt”
? Append “V2.txt” onto the extracted characters
Example: For input file “data.txt”, output file will be “dataV2.txt”
o Use the roster object to call the writeRosterFile() method,
using the new filename as an argument to the method.
Test 14 & 15 (10 pts)
Implement check if file has too many lines of data (and check different length filename for saving)
Within the RosterManager class, in the main() method:
? For the try block that calls the readRosterFile() method,
after the catch block for an IOException,
add a second catch block for an ArrayIndexOutOfBoundsException
o Display the following error messages:
**Too many players in file
**Program will only use the first X players
where X is the ROSTER_MAX value
(Player.java and PlayerArrayImpl and RosterManager.java)
SAMPLE RUN
Enter input data filename:
r2.txt
2 players stored
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
d
Player 1 - jersey #8, rating is 7
Player 2 - jersey #55, rating is 9

MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
a
Enter jersey number of player to add:
12
Enter rating of player:
8
Player 12 successfully added
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
u
Enter jersey number of player to update:
8
Enter new rating of player:
6
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
d
Player 1 - jersey #8, rating is 6
Player 2 - jersey #55, rating is 9
Player 3 - jersey #12, rating is 8
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
s
Roster saved to file r2v2.txt
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
x

Error -- invalid menu choice
MENU
D - Display Roster
A - Add a Player
U - Update a Player's Rating
S - Save Roster
E - Exit program
Enter choice:
e

Explanation / Answer

Please find my implementation.

Please let me know in case of any issue.

#####################      PlayerRoster.java       ###############


import java.util.Scanner;

/**
* @author : Pravesh Kumar
*
*/
public class PlayerRoster {
   /**
   * @param args
   */
   public static void main(String[] args) {
       /* Type your code here. */

       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           int[] playerJerseyNumber = new int[5];
           int[] playerRating = new int[5];

           for (int i = 0; i < 5; i++) {
               System.out.print("Enter player " + (i + 1)
                       + "'s jersey number: ");
               playerJerseyNumber[i] = scanner.nextInt();
               System.out.print("Enter player " + (i + 1) + "'s rating: ");
               playerRating[i] = scanner.nextInt();

           }

           System.out.println("ROSTER");

           for (int i = 0; i < 5; i++) {
               System.out.println("Player " + (i + 1) + " -- Jersey number: "
                       + playerJerseyNumber[i] + ", Rating: "
                       + playerRating[i]);

           }

           do {
               System.out.println("MENU" + " u - Update player rating"
                       + " a - Output players above a rating"
                       + " r - Replace player" + " o - Output roster"
                       + " q - Quit");

               System.out.print("Choose an option: ");
               char choice = scanner.next().charAt(0);
               switch (choice) {
               case 'u': {
                   System.out.print("Enter a jersey number: ");
                   int playerJersey = scanner.nextInt();
                   System.out.print("Enter a new rating for player:");
                   int newRating = scanner.nextInt();
                   for (int i = 0; i < 5; i++) {
                       if (playerJerseyNumber[i] == playerJersey) {
                           playerRating[i] = newRating;
                           break;

                       }
                   }
               }
                   break;
               case 'a': {
                   System.out.print("Enter a rating:");
                   int aboveRating = scanner.nextInt();
                   for (int i = 0; i < 5; i++) {
                       if (playerRating[i] > aboveRating) {
                           System.out.println("Player " + (i + 1)
                                   + " -- Jersey number: "
                                   + playerJerseyNumber[i] + ", Rating: "
                                   + playerRating[i]);

                       }
                   }
               }

                   break;
               case 'r': {
                   boolean flag = true;
                   do {
                       System.out.print("Enter a jersey number: ");
                       int playerJersey = scanner.nextInt();
                       System.out.print("Enter a new jersey number: ");
                       int playerNewJersey = scanner.nextInt();
                       System.out.print("Enter a new rating for player:");
                       int newRating = scanner.nextInt();
                       for (int i = 0; i < 5; i++) {
                           if ((playerJerseyNumber[i] == playerJersey)) {
                               playerJerseyNumber[i] = playerNewJersey;
                               playerRating[i] = newRating;
                               flag = false;
                               break;

                           }
                       }
                       if (!flag) {
                           System.out
                                   .println("Error: Invalid Jersey Number... Try Again...");
                       }
                   } while (flag);

               }
                   break;
               case 'o': {
                   System.out.println("ROSTER");

                   for (int i = 0; i < 5; i++) {
                       System.out.println("Player " + (i + 1)
                               + " -- Jersey number: " + playerJerseyNumber[i]
                               + ", Rating: " + playerRating[i]);

                   }
               }
                   break;
               case 'q':

                   break;

               default:
                   break;
               }
               if (choice == 'q')
                   break;
           } while (true);
       } catch (Exception e) {
           // TODO: handle exception
       }
       return;
   }
}

###########      Sample Run     #################

Enter player 1's jersey number: 84
Enter player 1's rating: 7
Enter player 2's jersey number: 23
Enter player 2's rating: 4
Enter player 3's jersey number: 4
Enter player 3's rating: 5
Enter player 4's jersey number: 30
Enter player 4's rating: 2
Enter player 5's jersey number: 66
Enter player 5's rating: 9
ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 5
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 5
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: a
Enter a rating:5
Player 1 -- Jersey number: 84, Rating: 7
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: r
Enter a jersey number: 4
Enter a new jersey number: 12
Enter a new rating for player:8
Error: Invalid Jersey Number...
Try Again...
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: u
Enter a jersey number: 23
Enter a new rating for player:6
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 6
Player 3 -- Jersey number: 12, Rating: 8
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: q