View the step-by-step solution to:

To do the program you will practice the following techniques. Practice creating a structured program to solve a problem. Write methods that use...

I need to do a java assignment, which is a hangman game. Can I get any help?
I have attached the required documents below.

The purpose of this assignment is to implement a program that plays the game of hangman. To do the program you will practice the following techniques. Practice creating a structured program to solve a problem. Write methods that use parameters. Write an interactive program. Practice using indefinite loops. Use methods from the String class and another class that we provide. For this assignment you are limited to the language features in chapters 1 through 5 of the textbook. Provided Files: (A shell file with a main method and the header information.) (Another program that is used to read from a file and provide phrases for the game.) HangmanMovies.txt (A file with movies to use for the game.) Description: Write a program to play Hangman. The computer picks a secret phrase from a list of phrases or words. . The human player guesses letters until they reveal the entire phrase or lose due to picking too many letters that are not in the phrase. Look at the sample output which shows a series of games. Your program shall implement the same level of error checking. Your output must match the output shown exactly given the same secret phrases and user input. The program will: Display an intro. (This is already done.) Create a new PhraseBank object. (This is already done in the main method of the shell file.) When this is done a window will appear where the user selects the text file that has the topic for the secret phrases and the phrases themselves. The PhraseBank data type has two methods for you to use, getTopic() which returns a String that is the topic for the phrases in the file and getNextPhrase() which returns a String that is the next secret phrase for the game. Each secret phrase contains only upper case letters and underscore characters in place of spaces. The program then plays a game of Hangman. At the start of each game the program gets a secret phrase from the PhraseBank object. Each round the program displays the current form of the secret phrase. Spaces are shown as underscore characters. Letters that have not been guessed are shown as asterisks. Letters that have been guessed are revealed. The program then displays the letters that have not been guessed so far in alphabetical order. Next the program prompts the user for the next guess, a single capital letter. If the value entered is not a letter or it is a letter that has already been guessed the program prompts the user again until they enter a letter that has not been guessed yet. The results of the guess are shown. If the guessed letter appears in the secret phrase all instances of that letter are revealed. If the guessed letter does not appear the number of wrong guesses is incremented. If all of the letters in the secret phrase are revealed the player wins. If the player makes 5 wrong guesses they lose. (It should be VERY easy to change the number of wrong guesses allowed.) After the first game is completed the program will ask the user if they want to play again. As long as the user responds they want another game the program continues. Approach: Divide the program into parts. Complete and test each part before moving on. Use the methods from the String class to help make your job easier. The String methods used in the sample solution are the length, charAt, replace, indexOf, substring, and toUpperCase methods. In addition String concatenation with an accumulator pattern is used as in the ciphers program, as is conversion of char to String by adding an empty String.
Background image of page 1
Divide the program up into methods to provide a structured solution. Some of the methods will be void and others will return values. You will have to make use of parameters, for loops, while loops, and if statements. By way of comparison the instructor solution has 13 methods (6 of them void) including the main method. One method is 28 lines long (without comments), and the rest are shorter. The program is about 160 lines long including single braces, white space, and comments, but not including the large header comment at the top. Don't worry if your program is longer, or you could not get the methods to be very brief; you will lose points only if you have long methods that have poor structure and can easily be broken up into smaller ones. Do try not to exceed, say, 40 lines per method. Testing notes and suggestions: Testing individual methods 'in-situ' is possible with BlueJ. Also, of course, BlueJ has a step-by-step debugger. Adding debug print statements in your code is useful, and may be easily turned off (before submissiion) if enclosed in if (DEBUG) blocks. The graders replace the and the HangmanMovies.txt file with their own to run the programs with a fixed choice of phrases and without the user having to click the choice of input file. You could instead always set your secret phrase to a fixed String -- "ABC_D_XYZ", for example -- during debugging. You may also explore what would happen if your HangmanMovies.txt file has only ONE movie in it. Running the program repeatedly during debugging can be tedious because of the user input. Use the command line (Linux, MAC OSX, or DOS) to speed it up. On Linux, for example, you could run java on the command line with: java Hangman < input.txt > output.txt This will read from input.txt as if you were typing it in, and the console output goes to output.txt for later examination. Notes: You may use class constants to make your code readable, and to make easy changes. But as in previous assignments, you may not use global (class) variables. You must create your Scanner object in main and pass it on to other methods. Your program may do whatever you want if a user enters lower case letters, or if a user enters more than one letter, as we don't test those scenarios. (The sample solution converts all input to the first character, upper-case.) Blank lines at the beginning and end will be ignored by the grader.
Background image of page 2
import java.util.Scanner; public class Hangman { public static void main(String[] args) { intro(); PhraseBank phrases = new PhraseBank(); Scanner keyboard = new Scanner(; } // show the intro to the program public static void intro() { System.out.println("This program plays the game of hangman."); System.out.println("When the window opens select the file"); System.out.println("with the phrases you want to use.\n"); System.out.println("The computer will pick a random phrase."); System.out.println("Enter capital letters as your guesses."); System.out.println("After 5 wrong guesses you lose."); } }
Background image of page 1
Movie The Shawshank Redemption The Godfather Pulp Fiction Schindlers List Twelve Angry Men One Flew Over the Cuckoos Nest The Empire Strikes Back The Dark Knight The Lord of the Rings The Return of the King Star Wars Casablanca Goodfellas Fight Club The Lord of the Rings The Fellowship of the Ring Raiders of the Lost Ark Rear Window The Usual Suspects Psycho The Silence of the Lambs The Matrix Seven Memento Its a Wonderful Life The Lord of the Rings The Two Towers Dr Strangelove or How I Learned to Stop Worrying and Love the Bomb Sunset Blvd North by Northwest Citizen Kane Apocalypse Now American Beauty Forrest Gump American History X Taxi Driver Vertigo Terminator Two Judgment Day Lawrence of Arabia Alien WALLE Saving Private Ryan A Clockwork Orange The Shining Paths of Glory The Departed The Pianist To Kill a Mockingbird Aliens Double Indemnity Eternal Sunshine of the Spotless Mind Chinatown Requiem for a Dream The Third Man L.A. Confidential Reservoir Dogs Das Boot The Treasure of the Sierra Madre Avatar Monty Python and the Holy Grail City Lights The Bridge on the River Kwai Inglourious Basterds Raging Bull The Prestige Up
Background image of page 1
Two Thousand and One A Space Odyssey Singin in the Rain Back to the Future Modern Times Some Like It Hot Amadeus Full Metal Jacket The Maltese Falcon Gran Torino Braveheart Cinema Paradiso Once Upon a Time in America All About Eve Slumdog Millionaire Metropolis The Green Mile The Elephant Man Sin City The Great Dictator Rebecca The Apartment The Sting Indiana Jones and the Last Crusade The Great Escape Gladiator Return of the Jedi Unforgiven Batman Begins Jaws Blade Runner Hotel Rwanda Die Hard Mr Smith Goes to Washington No Country for Old Men On the Waterfront Oldboy Touch of Evil District Nine Fargo The Wizard of Oz Heat Strangers on a Train Donnie Darko Cool Hand Luke High Noon There Will Be Blood Notorious The Sixth Sense The Wrestler The Manchurian Candidate The Deer Hunter Annie Hall Kill Bill Vol One Snatch The General Platoon The Big Lebowski Ben Hur Into the Wild The Big Sleep Million Dollar Baby Ran It Happened One Night The Lion King
Background image of page 2
Show entire document
import; import; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.UIManager; public class PhraseBank { private ArrayList<String> phrases; private int currentIndex; private String topic; /* * When a wordbank is created a window will open to * select the file with the phrases. Each line in the file * will be treated as a single phrase. */ public PhraseBank(){ phrases = new ArrayList<String>(); currentIndex = -1; try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){ System.out.println("Unable to change look and feel"); } loadWords(); } /* * Call this method to get the next phrase. * The returned String will contain upper case * letters are spaces */ public String getNextPhrase(){ currentIndex = (currentIndex + 1) % phrases.size(); return phrases.get(currentIndex); } public String getTopic(){ return topic; } // to consider. Shoudl this be broken up into two methods? private void loadWords() { // get the file JFileChooser chooser = new JFileChooser("."); int retval = chooser.showOpenDialog(null); File f = null; chooser.requestFocusInWindow(); if (retval == JFileChooser.APPROVE_OPTION) f = chooser.getSelectedFile(); // first line is assumed to be the topic! // load the phrases try{ Scanner s = new Scanner(f); topic = s.nextLine(); while(s.hasNextLine()){ String phrase = trim(s.nextLine().trim()); phrases.add(phrase.toUpperCase()); }
Background image of page 1
} catch(IOException e){ System.out.println("Unable to load phrases. Please exit."); } // if no values add in one if(phrases.size() == 0) phrases.add("HANGMAN"); Collections.shuffle(phrases); } // I assume nextLine is not null. // Return a String with only characters and spaces. // All other characters in org are removed. private static String trim(String org) { String result = ""; for(int i = 0; i < org.length(); i++){ char ch = org.charAt(i); if( Character.isLetter(ch)) result += ch; else if(ch == ' ') result += '_'; } return result; } }
Background image of page 2
Sign up to view the entire interaction

Top Answer

Dear , Time is too less.... View the full answer - Preview not available

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.


Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online