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

Your assignment is to write and submit two java files. One is a class definition

ID: 3762978 • Letter: Y

Question

Your assignment is to write and submit two java files. One is a class definition named

Game (Game.java), and another is the test driver program, TestDriver.java.

The Game is a class to keep the players in a team for a game, and it has the three instance variables: players, count, and name.

Game

players : String[]

count : int

name : String

+ Game(int, String)

+ add(String)    : void

+ remove(String) : void

+ print()        : void

- search(String) : int

The count is the number of players, 2) players is an array object storing names of all players as Strings, and 3) name is a string object for game name.

The class Game must include the constructors and methods in the table: (If your class does not contain any of the following methods, points will be deducted). The public and private access modifiers are represented as "+" and "-" respectively.

Game(int, String): (2 pts)

The constructor is to

Create an instance of a String-type array with the length of input parameter. Each element in the array is initialized as "" (i.e. an empty String).

The count is set as 0. It is not the length of array but the number of valid players. In other words, the players is a partially filled array, and the count is used as an END position of valid players.

Set the name with the second input parameter.

add(String):void (2 pts)

If there is room (left in) the array, add the parameter value to the END(count) position of the array and adjust the count to reflect how many values are in the array. If the value was added, DO NOT print any output. If the array is full, print the error message as shown below. This is the output if the value 3 was to be added and there is no room.

Array is full. The player Joey cannot be added.

print(): void (2 pts)

Print the name of game and the valid game players in the array as shown below. Not that it prints at most 5 players per line and that the player names are separated by three spaces. If the players {Mike, Smith, John, Tony, Nick, Sam} are in the array, then the output is

Chinese Checkers

Mike   Smith   John   Tony   Nick Sam

search(String):int (2 pts)

This method should perform a linear or sequential search. The method is to look at the elements in the array until the value of the parameter is found or all values have been examined. If a value occurs in the array more than once, the method should return the first position where the value was found. If the value is not in the array, then the method should return a negative 1 (-1). There is NO output from this method.

remove(string):void (2 pts)

Remove calls search for the player in the array. If the player is currently in the array (index returned by search is not -1), delete the player from the array by moving the other players “up” and adjust the count to reflect how many players are in the array. If the player was successfully removed, do not print any output. Only if the player is not found, then print the error message as shown below. It is an example case when the player James was to be deleted from the array {Mike, Smith, John, Tony, Nick, Sam}.

The player James was not found and cannot be deleted.

TestDrive.java contains the main method to create a Game instance object and test the methods in the class. The program will ask a user to enter one of the commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by calling (invoking) one of the methods you defined in the Game class. It will terminate when the user enters 'q'. The tester program provided in the last assignments may help. In addition to the main method, the Assignment7 class has a static method, printMenu(), to print out the following commands.

Command Options

-----------------------------------

n: Create a new game a: Add a player

r: Remove a player

p: Print the information

?: Display the menu again q: Quit this program

Here is the description for each option:

n: ask and read the size of array and the name of game a: ask and add a player to the array

r: ask and remove a player in the array p: print the information of array.

?: displays the menu q: quits the program

Output Example

Program Output (Input in bold)

****** Game Manager Program ******"

Command Options

-----------------------------------

n: Create a new game a: Add a player

r: Remove a player

p: Print the information

?: Display the menu again q: Quit this program

Please enter a command or type ?: n

n [Create a new Game team] [Input the number of players]: 6

[Input the name of Game]: Chinese Checkers

Please enter a command or type ?: a

a [Add a player]: Mike

Please enter a command or type ?: a

a [Add a player]: Smith

Please enter a command or type ?: a

a [Add a player]: John

Please enter a command or type ?: a

a [Add a player]: Tony

Please enter a command or type ?: a

a [Add a player]: Nick

Please enter a command or type ?: a

a [Add a player]: Sam

Please enter a command or type ?: a

a [Add a player]: Joey

Array is full. The player Joey cannot be added.

Please enter a command or type ?: P

p [Print the information]:

Chinese Checkers

Mike   Smith   John   Tony   Nick Sam

Please enter a command or type ?: d

[Invalid input]

Please enter a command or type ?: r

r [Remove a player]: Tony

Please enter a command or type ?: r

r [Remove a player]: James

The player James was not found and cannot be deleted.

Please enter a command or type ?: r

r [Remove a player]: Sam

Please enter a command or type ?: p

p [Print the information]:

Chinese Checkers

Mike   Smith   John   Nick

Please enter a command or type ?: ?

Command Options

-----------------------------------

n: Create a new game a: Add a player

r: Remove a player

p: Print the information

?: Display the menu again q: Quit this program

Please enter a command or type ?: q

****** End of Program ******

Game

players : String[]

count : int

name : String

+ Game(int, String)

+ add(String)    : void

+ remove(String) : void

+ print()        : void

- search(String) : int

Explanation / Answer

If you have any further queries, just get back to me.

The file Game.java is:

class Game
{
String[] players;
int count;
String name;
  
public Game(int size, String teamName)
{
players = new String[size];
for(int i = 0; i < size; i++)
players[i] = "";
count = 0;
name = teamName;
}

public void add(String playerName)
{
if(count < players.length)
{
players[count] = playerName;
count++;
}
else
System.out.println("Array is full. The player "+playerName+" cannot be added.");
}
  
public void remove(String playerName)
{
int pos = this.search(playerName);
if(pos != -1)
{
if(pos == count-1)
{
count--;
}
else
{
for(int i = pos+1; i < count; i++)
players[i-1] = players[i];
count--;
}
}
else
System.out.println("The player "+playerName+" was not found and cannot be deleted.");
}
  
public void print()
{
System.out.println(name);
for(int i = 0; i < count; i++)
{
if(i % 5 == 0)
System.out.println();
System.out.print(players[i]+" ");
}
}
private int search(String playerName)
{
for(int i = 0; i < count; i++)
{
if(players[i].equals(playerName))
{
return i;
}
}
return -1;
}
}

The file TestDrive.java is:

import java.util.*;
class TestDrive
{
public static void printMenu()
{
System.out.println("Command Options");
System.out.println("-----------------------------------");
System.out.println("n: Create a new game");
System.out.println("a: Add a player");
System.out.println("r: Remove a player");
System.out.println("p: Print the information");
System.out.println("?: Display the menu again");
System.out.println("q: Quit this program");
}
public static void main(String[] args)
{
printMenu();
String playerName;
Scanner sc = new Scanner(System.in);
Game game = new Game(0, "");
while(true)
{
System.out.print("Please enter a command or type ?:");
char choice = sc.next().charAt(0);
switch(choice)
{
case 'n':
           System.out.print("Enter the size of the array: ");
           int sz = sc.nextInt();
           sc.nextLine();
           System.out.print("Enter the name of the game: ");
           String gameName = sc.nextLine();
           game = new Game(sz, gameName);
           break;
case 'a':
           System.out.print("Enter the player name to add: ");
           playerName = sc.next();
           game.add(playerName);
           break;
case 'r':
           System.out.print("Enter the player name to delete: ");
           playerName = sc.next();
           game.remove(playerName);
           break;
case 'p':
           game.print();
           break;
case '?':
           printMenu();
           break;
case 'q':
           return;
default :
           System.out.println("Invalid menu Option.");
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote