Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ASAP I nearly finished this assignment and after working on one single error all day i came to the conclusion i need help. My Assignment

ASAP

I nearly finished this assignment and after working on one single error all day i came to the conclusion i need help.

My Assignment is this:

Scramble2 class: In Assignment 2 your Scramble class was somewhat limited. It always produced the words in the same order (the order that they were stored in the file) and each time the words were needed they had to be read from the file. In this assignment your class (called Scramble2 to avoid confusion) will have the following required improvements:

- When your Scramble2 object is created, it will read in and store all of the words from the file into an array or ArrayList instance variable. Just like in Assignment 2, you should use the file "words.txt" for your words.

- When a getRealWord() / getScrambledWord() call is made, the word returned should be randomly chosen from within the list of words. This process must not return the same word twice and must use all of the words in the list before returning null.

- A reset() method must be added which will reinitialize the list. However, this should NOT read the words from the file again. The only time the file should be accessed is when the list is created in the constructor. The reset() method should work even if the words have only been "partially" used (ex: 5 out of a list of 10 words are used and then the reset() method is called).

- To test your Scramble2 class, run it with the program NewScramTest.java. It should produce output similar to that of NewScramTest.txt (however, since the words are produced randomly, the ordering in your runs should differ from that provided in the test file.

- Player class: In Assignment 2, players of the game had no individual histories. Once they were finished playing the game their individual information was lost. In this version you will maintain players individually through two new classes: the Player class and (described below) the PlayerList class.

- A Player object will be an encapsulation of the player's name, win / loss information and methods for accessing and mutating the player information.

- While a player is playing the game, his / her information should be accessed through a Player object, and that object should be updated after each round to reflect the result of that round.

- There are several ways of implementing your Player class, but some minimal requirements are shown in the test program PlayerListTest.java. Your Player class (and PlayerList class) must work with this program and the output must be consistent with that shown in the file PlayerTest.txt. Note, however, that this test program may not test everything that you will need in your Player class.

- PlayerList class: To organize / maintain your Player objects, you must also implement a PlayerList class. This is a simple "database" class similar to the one from Lab 7. This class should allow the Player objects to be read in from a text file (use the file "players.txt") and then to be accessed as needed during your game.

- The "players.txt" file will be formatted with the information for one player on each line of the file, as shown below

where is the name of the player, is the total number of rounds played by the player, is the total number of rounds won by the player and is the total number of rounds lost by the player. There is a single blank space between each item on each line. The file can have an indefinite number of lines in it.

- At the end of the game all of the Player objects should be saved back to the file so that the information is updated if the program is run again.

- Some of the required functionality of your PlayerList class is shown in the program PlayerListTest.java. Your PlayerList class must run with this program as given and the output should be consistent with that provided in PlayerTest.txt. Note, however, that this test program may not test everything that you will need in your PlayerList class.

- Assig3 class (main program): Playing a single "game" will be the same as it was in Assignment 2 (see Assignment 2 for details). However, your Assig3 program will be enhanced in the following ways:

- Now you will have an extra, outer loop, which will allow multiple players to play the game, one at a time. For each "game" you will ask the player to enter his / her name. If he / she is already registered you should then retrieve the correct Player object from the PlayerList and use that during the playing of the game. However, if the player is not registered you should create a new Player object and add it to the PlayerList.

- At the end of each game you should show to the player the results from that "game" (i.e. rounds, wins and losses in the current session) followed by the overall results (from all sessions together) for that Player (by using the toString() method from the Player object).

- Once all players are done playing the game you should show the cumulative statistics, as provided by the toString() method of the PlayerList object.

- Since you are now keeping the individual results in the Player objects and the overall stats in the PlayerList object, you will no longer need the Results class in this program.

- Since your Scramble2 class has a reset() method, you should reset() it before each Player plays the game. You should NOT make a new Scramble2 object for each Player.

- For an example of how the program output may look, see a3out.txt. Note that your output does not have to match this exactly, but it should be well-formatted and have the same functionality.

Issues and Hints:

- You will need to use files in several places in this program. In particular you will need to read the words from "words.txt" into your Scramble2 object and you will need to read the players into the PlayerList object from "players.txt". Note that in both cases you should read from the file only one time for the entire program execution.

- The words.txt and players.txt files will be provided for you. Note, however, that these files could be changed and/or different versions of them could be used to test your programs. Your program should work for any properly formatted "words.txt" and "players.txt" files.

Test your program thoroughly before handing it in. Make sure it functions as required in all cases. I have generated some sample output that may help you with this. See: a3out.txt.

Don't forget your Assignment Information Sheet -- you will lose points without it.

Don't forget to comment your code - you will lose points without them.

Note that you must have 9 files in your .zip submission file for this assignment:

Assig3.java

Scramble2.java

Player.java

PlayerList.java

NewScramTest.java

PlayerListTest.java

words.txt

players.txt

Your assignment information sheet

The first 4 files you must write yourself and the next 4 files you should pass on unaltered from how they are provided for you on the web site. Make sure all of these files are present or you will lose submission points!

Extra Credit Options

The Player in this game is identified by an id, but no password is required. Add a password field for each Player and require the Player to enter the correct password before playing the game. Note that this will also require changes to the format of your players.txt file.

Write an administrator program that allows an administrator to log in (using an id and password) and to see / update the players (change a score for a player, add a player, remove a player, etc). This should utilize the Player.java and PlayerList.java classes.

The expected output is this:

> cat players.txt Inigo 9 8 1 Roberts 8 5 3 Vizzini 2 0 2 Fezzik 11 6 5 Buttercup 6 3 3 Humperdinck 5 2 3 ---------------------------------------- > java Assig3 Welcome to The Scrambler! Please enter your name to play (just hit  to quit): Vizzini Welcome Back Vizzini Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: OKSLRIPMSENAT Your Guess: inconceivable Sorry, Vizzini that is not correct Here are the letters you got correct: ____________E You have 2 guesses remaining Scramble: OKSLRIPMSENAT Your Guess: unscrupulous Sorry, Vizzini that is not correct Here are the letters you got correct: _____________ You have 1 guesses remaining Scramble: OKSLRIPMSENAT Your Guess: iocane Sorry, Vizzini that is not correct Here are the letters you got correct: _____________ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is SPORTSMANLIKE Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: UTOMNT Your Guess: mutton Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: ERITAP Your Guess: eitrap Sorry, Vizzini that is not correct Here are the letters you got correct: _I____ You have 2 guesses remaining Scramble: ERITAP Your Guess: riprate Sorry, Vizzini that is not correct Here are the letters you got correct: _I____ You have 1 guesses remaining Scramble: ERITAP Your Guess: pirate Great Job Vizzini! You got it! Would you like to play another round (Y/N)? n Thanks for playing Vizzini! Vizzini, in today's game you had the following results: Rounds played: 3 Wins: 2 Losses: 1 Here are your cumulative stats: Name: Vizzini Total Rounds Played: 5 Total Rounds Won: 2 Total Rounds Lost: 3 Please enter your name to play (just hit  to quit): Herbert You are not in our player list. Do you wish to add your name to our player list? (Y/N): n Please enter your name to play (just hit  to quit): Marge You are not in our player list. Do you wish to add your name to our player list? (Y/N): y Welcome Marge Marge, you have 3 guesses to get the Scramble Good luck! Scramble: DDAER Your Guess: dread Great Job Marge! You got it! Would you like to play another round (Y/N)? y Marge, you have 3 guesses to get the Scramble Good luck! Scramble: NPCIERSS Your Guess: pincers Sorry, Marge that is not correct Here are the letters you got correct: P_____S_ You have 2 guesses remaining Scramble: NPCIERSS Your Guess: princess Great Job Marge! You got it! Would you like to play another round (Y/N)? n Thanks for playing Marge! Marge, in today's game you had the following results: Rounds played: 2 Wins: 2 Losses: 0 Here are your cumulative stats: Name: Marge Total Rounds Played: 2 Total Rounds Won: 2 Total Rounds Lost: 0 Please enter your name to play (just hit  to quit): Vizzini Welcome Back Vizzini Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: ISNPCERS Your Guess: princess Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: AGADPRN Your Guess: grandpa Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: NITNIASY Your Guess: fantasy Sorry, Vizzini that is not correct Here are the letters you got correct: ________ You have 2 guesses remaining Scramble: NITNIASY Your Guess: heresy Sorry, Vizzini that is not correct Here are the letters you got correct: ________ You have 1 guesses remaining Scramble: NITNIASY Your Guess: idonotknowthisword Sorry, Vizzini that is not correct Here are the letters you got correct: I_______ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is INSANITY Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: UTMOTN Your Guess: mutton Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: SENRDOT Your Guess: rodents Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: NAOIENIECLVCB Your Guess: unretainable Sorry, Vizzini that is not correct Here are the letters you got correct: _N___________ You have 2 guesses remaining Scramble: NAOIENIECLVCB Your Guess: unpronouncable Sorry, Vizzini that is not correct Here are the letters you got correct: _N___________ You have 1 guesses remaining Scramble: NAOIENIECLVCB Your Guess: indeceivable Sorry, Vizzini that is not correct Here are the letters you got correct: IN___________ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is INCONCEIVABLE Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: KSSRTEPNLIMOA Your Guess: abcdefghij Sorry, Vizzini that is not correct Here are the letters you got correct: _____________ You have 2 guesses remaining Scramble: KSSRTEPNLIMOA Your Guess: thiswordistough Sorry, Vizzini that is not correct Here are the letters you got correct: _____________ You have 1 guesses remaining Scramble: KSSRTEPNLIMOA Your Guess: IGiveUp Sorry, Vizzini that is not correct Here are the letters you got correct: _____________ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is SPORTSMANLIKE Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: RADDE Your Guess: DREAD Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: WMSAP Your Guess: SwAmP Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: IGIHNNLGT Your Guess: thunder Sorry, Vizzini that is not correct Here are the letters you got correct: _________ You have 2 guesses remaining Scramble: IGIHNNLGT Your Guess: fog Sorry, Vizzini that is not correct Here are the letters you got correct: __G______ You have 1 guesses remaining Scramble: IGIHNNLGT Your Guess: hail Sorry, Vizzini that is not correct Here are the letters you got correct: _________ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is LIGHTNING Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: TAPIRE Your Guess: pirate Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: EPASIDR Your Guess: repaside Sorry, Vizzini that is not correct Here are the letters you got correct: _E___I_ You have 2 guesses remaining Scramble: EPASIDR Your Guess: despire Sorry, Vizzini that is not correct Here are the letters you got correct: DESP___ You have 1 guesses remaining Scramble: EPASIDR Your Guess: despair Great Job Vizzini! You got it! Would you like to play another round (Y/N)? y Vizzini, you have 3 guesses to get the Scramble Good luck! Scramble: EUBSDRNL Your Guess: subdural Sorry, Vizzini that is not correct Here are the letters you got correct: ________ You have 2 guesses remaining Scramble: EUBSDRNL Your Guess: dranelals Sorry, Vizzini that is not correct Here are the letters you got correct: ___N____ You have 1 guesses remaining Scramble: EUBSDRNL Your Guess: sandals Sorry, Vizzini that is not correct Here are the letters you got correct: ________ You have 0 guesses remaining Round over! Better luck next time Vizzini The actual word is BLUNDERS There are no more words left! Thanks for playing Vizzini! Vizzini, in today's game you had the following results: Rounds played: 13 Wins: 8 Losses: 5 Here are your cumulative stats: Name: Vizzini Total Rounds Played: 18 Total Rounds Won: 10 Total Rounds Lost: 8 Please enter your name to play (just hit  to quit): Game Over! Here are the player stats: Total Players: 7 Total Rounds Played: 59 Total Rounds Won: 36 Total Rounds Lost: 23 Pct of Rounds Won: 61.0 ------------------------------------------------- > cat players.txt Inigo 9 8 1 Roberts 8 5 3 Vizzini 18 10 8 Fezzik 11 6 5 Buttercup 6 3 3 Humperdinck 5 2 3 Marge 2 2 0 -------------------------------------------------- > java Assig3 Welcome to The Scrambler! Please enter your name to play (just hit  to quit): Marge Welcome Back Marge Marge, you have 3 guesses to get the Scramble Good luck! Scramble: RISEPSNC Your Guess: princess Great Job Marge! You got it! Would you like to play another round (Y/N)? n Thanks for playing Marge! Marge, in today's game you had the following results: Rounds played: 1 Wins: 1 Losses: 0 Here are your cumulative stats: Name: Marge Total Rounds Played: 3 Total Rounds Won: 3 Total Rounds Lost: 0 Please enter your name to play (just hit  to quit): Herbert You are not in our player list. Do you wish to add your name to our player list? (Y/N): y Welcome Herbert Herbert, you have 3 guesses to get the Scramble Good luck! Scramble: DNARPGA Your Guess: grandpa Great Job Herbert! You got it! Would you like to play another round (Y/N)? y Herbert, you have 3 guesses to get the Scramble Good luck! Scramble: RDDEA Your Guess: fraid Sorry, Herbert that is not correct Here are the letters you got correct: _R__D You have 2 guesses remaining Scramble: RDDEA Your Guess: scared Sorry, Herbert that is not correct Here are the letters you got correct: _____ You have 1 guesses remaining Scramble: RDDEA Your Guess: dunno Sorry, Herbert that is not correct Here are the letters you got correct: D____ You have 0 guesses remaining Round over! Better luck next time Herbert The actual word is DREAD Would you like to play another round (Y/N)? n Thanks for playing Herbert! Herbert, in today's game you had the following results: Rounds played: 2 Wins: 1 Losses: 1 Here are your cumulative stats: Name: Herbert Total Rounds Played: 2 Total Rounds Won: 1 Total Rounds Lost: 1 Please enter your name to play (just hit  to quit): Game Over! Here are the player stats: Total Players: 8 Total Rounds Played: 62 Total Rounds Won: 38 Total Rounds Lost: 24 Pct of Rounds Won: 61.3 --------------------------------------------------- > cat players.txt Inigo 9 8 1 Roberts 8 5 3 Vizzini 18 10 8 Fezzik 11 6 5 Buttercup 6 3 3 Humperdinck 5 2 3 Marge 3 3 0 Herbert 2 1 1 

Note: After every iteration it should update the text file with the players.

I need help fixing my error, implementing the extra credit, and fixing any errors that show up after the error i already have.

My error:

E:\Intermediate Programming\assig3>javac *.java

E:\Intermediate Programming\assig3>java Assig3 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at PlayerList.(PlayerList.java:27) at Assig3.main(Assig3.java:12)

My code:

Assig3:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import java.io.*;

import java.util.*;

public class Assig3 {

public static void main(String[] args) throws FileNotFoundException {

char compscram, compguess;

PlayerList list = new PlayerList("players.txt");

Scramble2 scrambler = new Scramble2("words.txt");

Scanner in = new Scanner(System.in);

while(true){

System.out.println("Enter your name: ");

String name = in.nextLine();

if(name.length()<1) break;

Player player;

if(list.getPlayer(name)==null){

player = new Player(name);

list.addPlayer( player);

System.out.println("Welcome "+name+" ");

}else{

player = list.getPlayer(name);

System.out.println("Welcome back, "+name+" ");

}

System.out.println(name+",you have 3 guesses to get the Scramble");

System.out.println("Good luck!");

String real = scrambler.getRealWord();

String inp = scrambler.getScrambledWord();

int guess=3;

while(guess>0){

System.out.println("Scramble: "+inp.toUpperCase());

System.out.println("Your Guess: ");

inp = in.nextLine();

if(inp.equalsIgnoreCase(real)){

System.out.println("Great Job "+name+"! You got it!");

int wins = player.getWins();

player.setWins(wins+1);

break;

}else{

guess--;

System.out.println("Sorry, "+name+" that is not correct");

System.out.print(" Here are the letters you got correct! ");

int len = real.length();

if (len > inp.length())

len = inp.length();

for(int counter = 0; counter < len; counter++ )

{

compscram = real.charAt(counter);

compguess = inp.charAt(counter);

if(compscram != compguess)

{

System.out.print("_");

}

else

{

System.out.print(compscram);

System.out.print("");

}

}

System.out.println("You have "+ guess+" remaining ");

}

}

if(guess==0){

System.out.println("You lost!");

int losses = player.getLosses();

player.setLosses(losses+1);

}

System.out.println("Would you like to play another round (Y/N)?");

inp = in.nextLine();

if(inp.equalsIgnoreCase("N")){

System.out.println(" Your stats:");

System.out.println(player);

break;

}

}

in.close();

}

}

Scramble2:

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import java.util.Scanner; import java.util.Stack; public class Scramble2 { ArrayList words; Stack disposableWords; String currentRealWord;

public Scramble2(String filename) throws FileNotFoundException{ words = new ArrayList<>(); disposableWords = new Stack<>();

Scanner in = new Scanner(new File(filename)); while(in.hasNextLine()){ String word = in.nextLine(); words.add(word); } in.close(); reset(); }

public String getRealWord(){ if(!disposableWords.isEmpty()){ currentRealWord=disposableWords.pop(); }else{ currentRealWord=null; }

return currentRealWord; } public String getScrambledWord(){ String s=currentRealWord;

if (s==null) return null;

Random rand = new Random(); char[] temp = s.toCharArray();

for(int i=s.length();i>=0;i--){ //Random source and dest index int src = rand.nextInt(s.length()); int dest= rand.nextInt(s.length());

//Swapping char dt = temp[dest]; temp[dest]=temp[src]; temp[src]=dt; }

return new String(temp); }

public void reset(){

for(String word: words){ disposableWords.push(word); }

//This is the way to make words random, without repetitions Collections.shuffle(disposableWords); }

}

PlayerList:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class PlayerList { ArrayList players; String filename;

public PlayerList(String filename) throws FileNotFoundException{ Scanner in = new Scanner(new File(filename));

this.filename=filename; players=new ArrayList<>();

while(in.hasNextLine()){ String line = in.nextLine(); String[] data = line.split(" "); Player p = new Player(data[0],Integer.parseInt(data[2]),Integer.parseInt(data[3])); players.add(p); }

in.close(); } public Player getPlayer(String s) { for(Player p: players){ if(p.getName().equals(s)){ return p; } }

return null; } public void addPlayer(Player onePlayer) { players.add(onePlayer); }

@Override public String toString(){ StringBuilder sb = new StringBuilder();

for(Player p: players){ sb.append(p.toString()); } return sb.toString(); } public String toStringAdmin() { String s= "None";

return s; } public Player removePlayer(String rName) { Player p=getPlayer(rName); if(p!=null){ players.remove(p); return p; } return null; } public void saveList() throws IOException { FileWriter writer = new FileWriter(filename); for(Player p:players){ writer.write(p.toString()); } writer.close(); } }

Player:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

public class Player { String name; int wins; int losses;

public Player(String name, int wins, int losses) { this.name = name; this.wins = wins; this.losses = losses; }

public Player(String name) { this.name = name; this.wins = 0; this.losses = 0; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWins() { return wins; } public void setWins(int wins) { this.wins = wins; } public int getLosses() { return losses; } public void setLosses(int losses) { this.losses = losses; } @Override public String toString(){ int total = wins+losses;

return name+" "+total+" "+wins+" "+losses+" "; }

}

These can not be changed in any way but must work when ran with the other classes:

NewScramTest:

// CS 401 Fall 2017

// Test file for Scramble2 class. This program should run as is with your Scramble2

// class. The scrambled versions of the words do not have to exactly match the ones

// shown in the test execution and since the orders are also pseudorandom, those don't

// have to match either. However, all of the words should be produced where shown.

import java.io.*;

import java.util.*;

public class NewScramTest

{

public static void main(String[] args) throws IOException

{

System.out.println("Testing normal use... ");

Scramble2 theScramble = new Scramble2("words.txt");

// Create Scramble object using the file name as an argument

showWords(theScramble);

// Rather than creating a new Scramble2 object, we now reset the previous one.

// This will allow us to reuse the same words over and over, with a different

// pseudorandom order being given each time.

System.out.println("Resetting words...");

theScramble.reset();

showWords(theScramble);

theScramble.reset();

System.out.println(" Testing special cases... ");

System.out.println("Initial call to getScrambledWord(): ");

String word = theScramble.getScrambledWord();

if (word == null)

System.out.println("No scrambled word yet");

String word1 = theScramble.getRealWord();

String word2 = theScramble.getRealWord();

System.out.println(" Two calls to getRealWord(): ");

System.out.println("Word 1: " + word1);

System.out.println("Word 2: " + word2);

System.out.println(" Two calls to getScrambledWord(): ");

word1 = theScramble.getScrambledWord();

word2 = theScramble.getScrambledWord();

System.out.println("Scram word 1: " + word1);

System.out.println("Scram word 2: " + word2);

System.out.println(" Now a call to each: " );

word1 = theScramble.getRealWord();

word2 = theScramble.getScrambledWord();

System.out.println("Word: " + word1);

System.out.println("Scram word: " + word2);

theScramble.reset();

System.out.println(" Partial use then reset: ");

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

{

word1 = theScramble.getRealWord();

word2 = theScramble.getScrambledWord();

System.out.println("Word: " + word1);

System.out.println("Scram word: " + word2);

}

System.out.println();

theScramble.reset();

showWords(theScramble);

}

public static void showWords(Scramble2 theScramble)

{

// Sentinel controlled loop. Keep getting words and showing them until returned

// word is null. This code is the same as in ScramTest. However, now the words

// should not appear in the order shown in the file, but in pseudorandom order

System.out.println("Here are the words:");

String word = theScramble.getRealWord();

while (word != null)

{

System.out.println("\tReal word is : " + word);

System.out.println("\tScrambled word is: " + theScramble.getScrambledWord());

word = theScramble.getRealWord();

}

System.out.println();

}

}

PlayerListTest:

// CS 0401 Fall 2017

// Program to test PlayerList and Player classes. Note the sample output shown

// for this program -- the first time run some new players are added but the

// second time none are added since the updated list was saved back to the file.

// Your Player and PlayerList classes can be implemented in various ways and must

// have additional methods to implement your Scramble game effectively. However, the

// required methods below demonstrate how the classes should fundamentally work, and

// your Player and PlayerList classes must produce the same results given the same

// players.txt file.

// For some help with these files you should look at Lab7 and, in particular the

// Movie class and the MovieDB class.

import java.util.*;

import java.io.*;

public class PlayerListTest

{

public static void main(String [] args) throws IOException

{

System.out.println("Testing PlayerList and Player classes");

// Create a PlayerList from a file of players. Note the format of the file

// and note that after this constructor all of the player information is stored

// within the allPlayers object. Note also the effect of the toString() method.

PlayerList allPlayers = new PlayerList("players.txt");

System.out.println("Here are the player stats: ");

System.out.println(allPlayers.toString());

System.out.println();

String [] names = {"Marge", "Fezzik", "Ingmar", "Inigo"};

for (String S: names)

{

// getPlayer will return a Player object if the player's name is within the

// PlayerList or null otherwise

Player onePlayer = allPlayers.getPlayer(S);

if (onePlayer != null)

{

System.out.println("Returning player info:");

// Note what is produced by the toString() method for a Player

System.out.println(onePlayer.toString());

}

else

{

// Create a new player with just a String for the name. The remaining

// fields should be 0

onePlayer = new Player(S);

System.out.println("New player info: ");

System.out.println(onePlayer.toString());

// Add the player to the

allPlayers.addPlayer(onePlayer);

}

}

// Note the format and information returned by the toString() method. Your

// output should contain the same, well formatted information.

System.out.println("Here are the player stats: ");

System.out.println(allPlayers.toString());

System.out.println();

// The toStringAdmin() method adds the individual player information to the

// output. This would not be used in the game but might be used by an

// administrator (see extra credit)

System.out.println("Here are the stats with the individual Player info:");

System.out.println(allPlayers.toStringAdmin());

String rName = "Fezzik";

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

{

System.out.println("Trying to remove " + rName);

// The removePlayer() method will look for the Player with the name

// matching the argument, remove that Player from the PlayerList, and

// return that Player to the caller. If the Player is not found the

// method should return null

Player P = allPlayers.removePlayer(rName);

if (P != null)

{

System.out.print(P.toString());

System.out.println("...has been removed ");

}

else

{

System.out.println("Player " + rName + " was not found");

}

}

// Write the players back to a text file so that they can be retrieved later

allPlayers.saveList();

}

}

Thankyou for any and all help given. I need this question answered within 3 and a half hours please.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5. Describe how contexts affect listening

Answered: 1 week ago