Starting from:
$30

$24

Assignment 2 Classes, objects and simple files Solution




Submission note: Your .zip file should contain only .java files and your Assignment Information Sheet. There should be no project files or project subdirectories in the .zip file. Also, your TA must be able to compile your program from the command line using the javac command. Test your files to make sure this will work (especially if you use an IDE such as NetBeans for development). If the TA cannot compile your program you will receive minimal credit!




Introduction:




Word games can be a lot of fun and also help to keep our minds working. In this assignment you will implement a very simple word game in an object-oriented way.




The Game:




The game you will implement is very simple and found in newspapers across the country. Your program will read in words from a file, scramble up the letters and then show the scrambled letters to the player. The player will then have 3 chances to guess each correct unscrambled word. If he/she succeeds, he/she wins the round - otherwise he/she loses and the program shows him/her the actual word. For each guess the program will show which (if any) letters in the player's guess that were in the correct locations. Play will continue until the player elects to quit or until no more words remain in the file. Statistics will be kept on the overall words attempted (by all players) and how many were correctly solved. These statistics will be shown to each player at the end of a game.




Details and Requirements:




Scramble Class:




Words will be accessed by the main program through a class called Scramble. This class is required to have the following:




A constructor that takes the name of a text file as an argument. The file will be used as the source of the words, and should have one word per line. The constructor should open the file for reading but not actually read anything - that will be handled by the methods below.



A String method called getRealWord(). This method will have 3 possible return results:



The next word in the file. This will occur if a word is left in the file and getScrambledWord() has been called since the most recent call to getRealWord().
The same word returned as in 1) above. This will occur if getRealWord() has already been






1 of 3 10/2/19, 3:16 PM
https://web.archive.org/web/20171124132229/http://people.cs.pi...




















OCT NOV
DEC











Go
5 captures
24




f




2014 2017
2018




6 Oct 2014 - 24 Nov 2017
▾ About this capture




1) A scrambled version of the most recent word returned by
(). It can be inferred



from this requirement that two successive calls to getScrambledWord() will return the same scrambled word. The letters in the scrambled word must have been altered in some random way utilizing the Random class.




null. This will occur if no call to getRealWord() has ever been made on this object and any time after 3) has occurred for getRealWord() as explained above.



In order to demonstrate the correct functionality of your Scramble class I have provided a test program, ScramTest.java. This program should execute as is with your Scramble class and the output should be the same as that shown in file scramtest.txt (with the exception that the actual scrambles of the words may be different).




Submit the ScramTest.java file with your other files so that your TA may test your Scramble class.




Results Class:




Results will be maintained by the main program through a class called Results. This class is required to have the following:




A constructor that takes the name of a text file as an argument. The file will be used to store the results, and should have the following format:



ROUNDS WINS LOSSES




Each value is a single integer stored on a separate line in the file. ROUNDS is the total number of rounds played by all players of the scramble game. WINS is the total rounds won and LOSSES is the total rounds lost. WINS plus LOSSES should always equal ROUNDS. When the constructor is called these values should be read in and stored in instance variables.




A void method called won() which increments the number of wins but does not update the file.



A void method called lost() which increments the number of losses but does not update the file.



A void method called save() which saves the data in the object back to the file.



A String method called toString() which returns a nicely formatted String containing the current information in the Results object.



Main Program (Assig2):




Your main program must be called Assig2 and it should proceed in the following way:




Welcome the player and ask his/her name (reading it in).



Create the Scramble object using file "words.txt" and create the Results object using the file "results.txt".



Play the game with the user as specified above, updating the Results object at the end of each round. For each round, your program should obtain both the real word (to test the user's answer) and the scrambled word (to show the user) as specified in the Scramble class above.



Guesses should NOT be case sensitive, so, for example, a guess of "piRAtE" would match the word "pirate".



Once the player quits or runs out of words, show him/her the overall results and save them back to the file.



Issues and Hints:




You may not use any arrays (or ArrayLists or other similar types) in this program. They are not






2 of 3 10/2/19, 3:16 PM
https://web.archive.org/web/20171124132229/http://people.cs.pi...




















OCT
NOV
DEC













Go


5 captures


24




f






2014
2017
2018








6 Oct 2014 - 24 Nov 2017
▾ About this capture

































You will need to use files in several places in this program. Some of these files may have to be opened / re-opened several times in the course of a single execution of the program - this is fine. You can read from files in Java using the Scanner class - you simply need to pass a File into the constructor. You can write to files using a PrintWriter. However, there are some quirks to reading/writing text files in Java (related to exceptions that must be dealt with). See ex11.java, FileTest.java, FileTest2.java and Section 4.10 in your text for some help with file I/O in Java. In particular, note how IOException is dealt with in these handouts - you will need to deal with this exception in some way in your Scramble and Results classes.



The words.txt file will be provided for you. Note, however, that this file could be changed and/or a different version of it could be used to test your programs. Your program should work for any properly formatted "words.txt" file.



Create the "results.txt" file yourself using any text editor. Initialize it to three 0s, each on a separate line. When you submit your assignment, the "results.txt" file can be arbitrary, as long as it is correct.



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: a2out.txt. I may also put additional files online so be sure to check back regularly for updates.



Note that the words are obtained from the Scramble object in the order that they are stored in the file. This is fine and expected. Later we may consider improving this to randomize the word selection.



Don't forget your Assignment Information Sheet -- you will lose points without it. You do not have to use my .html file. If you prefer simply copy the contents into a .txt file.



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



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



Assig2.java (you must write this)



Scramble.java (you must write this)



Results.java (you must write this)



ScramTest.java (provided for you)



words.txt (provided for you)



results.txt (you must write this)



Your assignment information sheet (you must fill this out)



Make sure all of these files are present or you will lose submission points!




Extra Credit Option (up to extra 10 points total):




Rather than keeping one results.txt file for all users, keep separate results for each user. Note that in order to do this the users must log in to the program in some way.
















3 of 3 10/2/19, 3:16 PM

More products