Ask Java Expert


Home >> Java

Programming Assignment

1. Getting Started

In this assignment, you will be writing a word game that makes use of arrays and static methods. A class called WordList has been posted along with this assignment. You will be using a method in this class for generating a random word. Download WordList.java to the same folder as your code. You may need to refresh the folder for the file to appear in Eclipse: select the folder and either press the F5 function key or select File > Refresh from the menu. Do NOT make any changes to this class and do NOT submit it. Only submit your Hangman program.

2. Programming Assignment

Hangman

In this game, one player (Player A) picks a word and lets the other player (Player B) know how many letters are in it by drawing a template in which each letter is represented by a dash. Player B then guesses one letter at a time. If the letter is in the word, Player A writes it into the template in however many places that it appears. Player B continues to guess until either the number of allowed guesses runs out or the correct word has been guessed.

In the version you will be writing, the program will display a template in which all letters in the word to be guessed originally appear as dashes ('-'). After a correct guess by the player, the program will update the template by filling in all occurrences of that letter. If the guess is incorrect, a message stating "Incorrect" will be printed. The program will also list, in alphabetic order, all incorrect guesses so far if there are any. This will be followed by the template being printed. Prompting for another letter will continue until (1) the player has guessed the word correctly, in which case a message including the word "congratulations" will be displayed, or (2) the player has used up the allowed maximum of six incorrect guesses, in which case a message that includes the words "game over" will be displayed along with the correct word. In the following sample interactions, player input appears in bold:

Be sure to read through the entire set of requirements assignment before beginning.

Basic code requirements:

1. The player is allowed a maximum of six incorrect guesses. Store this value to a named constant. If the player has used up all incorrect guesses, the player has lost and the game ends. The player wins if the word is guessed prior to having amassed six incorrect guesses.

2. You must use a minimum of two arrays of type char in completing this assignment. One will store the letters in the word being guessed (wordToGuess below). The other will store the letters that the player has guessed correctly (playerGuess below).

• To generate a word for the player to guess, you will be invoking the static getWord() method from the WordList class, which will return a pseudorandomly selected word as an array of type char. Initially, it is recommended that you do not call this method from your code. Instead, initialize the array yourself so that you can test repeatedly with the same word until the code is working correctly. For example, you could start with:

char[] wordToGuess = {'e','f','f','e','c','t'};

When you are satisfied that your code is correct, replace the hardcoded array with the call to the the getWord() method from the WordList class. DO NOT copy this method into your code.

• The playerGuess array must be initialized with dashes (i.e., char values of '-­-').

• After prompting the player to enter a letter, use the charAt() method in conjunction with the next() method for getting the first character in the player's input.

• If the player has correctly guessed a letter, replace the dash(es) in the corresponding position(s) in the playerGuess array with the lowercase value of that letter. For example, given the letters stored to the wordToGuess array shown above, if the player's first guess is the letter ‘f', then the playerGuess array should be updated to contain {'-­-', 'f', 'f', '-­-', '-­-', '-­-'}.

• After an incorrect guess, display a message including the word "incorrect" (see additional requirements for listing all incorrect guesses and verifying input; it is recommended that you get this basic functionality working first).

• Display the contents of the player's array, regardless of whether the template has been updated.

• If the player has won, display a message including the word "congratulations." If the player has lost, include the word "over" in your message and print the word being guessed.

Additional requirements: static methods and input verification. Note: you are not limited to defining and using only the two methods specified below; this is a minimum requirement.

After getting the basic logic described above to work, add the following methods and functionality:

1. Define a static method with the following return type and signature:

char[] fillArray(char, int)

This method returns a new char array whose elements are set to the value of the first parameter and whose size is set to the value of the second parameter. For example, if passed the char '?' and the integer 5, the new array to be returned by this method would contain: {'?','?','?','?','?'}.

Use this method to create the player's array and initialize its contents to dashes.

2. Verify that the player has entered a letter (see the Character methods in section 4.3.4 of the textbook). If the input is not a letter, inform the player of this, using the word "not" in your error message. Then reprompt until a letter has been entered. Invalid guesses do not count as incorrect. See sample output 3.

3. If the player has entered a letter, store it in lowercase (see section 4.3) to make your code case-­- insensitive (note that all of the words in the WordList class contain only lowercase letters).

4. If a letter has already been guessed, the player must be informed of this, with the word "before" included in your message. A repeated guess does not count as incorrect. See sample output 3. An additional array would be helpful here, and an additional method may be as well.

5. Display the list of incorrect guesses after any valid input, with each letter except for the last one in the list followed by a comma and a space (", ") as shown in the sample outputs. If the guess is incorrect, the list must come after the message informing the user of this. For full credit, display the list in alphabetical order. You may want to write a method to provide this functionality.

6. Define a static method with the following return type and signature:

replaceChar boolean (char, char[], char[])

This method first checks if the two arrays passed as arguments are of the same size. If they are the same, it searches for each occurrence of the value passed to the first parameter in the array passed to the second parameter. Each time it is found, that value is added to the same position in the array passed to the third parameter. For example,

if passed: 'a', {'b','a','n','a','n','a'}, {'b','-­-','-­-','-­-','-­-','-­-'}
then the contents of the second array will be updated to: {'b','a','-­-','a','-­-','a'}

A value of true is returned if any replacements have been made. If the second array is unchanged, a value of false is returned (note that if the arrays are of different sizes, no replacements will be made and the value returned will be false). Use this method to check if a guess is correct and to update the player's array with the correctly guessed letter.

Attachment:- Wordlist.rar

Java, Programming

  • Category:- Java
  • Reference No.:- M92709288

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As