JAVA: Fairly simple question using arrays: 1) The Game is a class to keep the pl
ID: 3762589 • Letter: J
Question
JAVA: Fairly simple question using arrays:
1) The Game is a class to keep the players in a team for a game, and it has the three (I think private) instance variables: players, count, and name.
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.
1. Game(int, String):
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.
2. add(String):void : 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.
3. print(): void 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
4. search(String):int : 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.
5. remove(String):void : Search for the player in the array using the search method. If the player is currently in the array, 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.
**Class game is for a driver class which will allow the user to choose one of the following options:
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
Explanation / Answer
class Game
{
String players[];
String game;
int count;
Game(int n, String s)
{
players=new String[100];
game=s;
for(i=0;i<n;i++)
{
players[i]="";
}
count=0;
}
void add(String s)
{
if(count!=100)
{
players[count]=s;
count++;
}
else
{
System.out.println("value"+count+" was to be added and there is no room.Array is full. The player Joey cannot be added.");
}
}
void print()
{
int i;
System.out.println(game);
for(i=0;i<count;i++)
{
if(players[i]!="")
{
System.out.print(players[i]);
}
}
}
int search(String s)
{
int i;
for(i=0;i<count;i++)
{
if(players[i].equals(s))
{
return i;
}
}
return -1;
}
void remove(String s)
{
int i;
int r=search(s);
if(r!=-1)
{
for(i=r;i<count;i++)
{
players[r]=players[r+1];
}
}
}
}
class players_game
{
public static void main(String args[])
{
int n;
String game,p;
Scanner scan = new Scanner(System.in);
Game g;
System.out.println("n: ask and read the size of array and the name of game");
System.out.println("a: ask and add a player to the array");
System.out.println("r: ask and remove a player in the array");
System.out.println("p: print the information of array.");
System.out.println("?: displays the menu");
System.out.println("q: quits the program");
System.out.println("Enter your Choice");
String ss=scan.nextLine();
Switch(ss)
{
case 'n':
System.out,println("Enter size of array");
n=scan.nextInt();
System.out,println("Enter name of Game");
game=scan.nextLine();
g=new Game(n,game);
break;
case 'a':
System.out,println("Enter name of Player");
p=scan.nextLine();
g.add(p);
break;
case 'r':
System.out,println("Enter name of Player");
p=scan.nextLine();
g.remove(p);
break;
case 'p':
g.print();
break;
case 'q':
return;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.