Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Specification: There is a \"fun\" children\'s game where one child think

ID: 3631703 • Letter: P

Question

Program Specification:

There is a "fun" children's game where one child thinks of a "common phrase", then the second child repeatedly makes guesses as to the letters that it contains.
You are to write a Java program that:

1-Prompts a user to enter a "common phrase", and reads it into a variable using Scanner.nextLine() - under th assumption that the phrase consists of nothing but undercase letters and spaces.
2-The following is than repeated until the entire "common phrase" is revealed:
The "common phrase" is displayed with all of the letters (that have not yet been correctly guessed) replaced with the ? character and all of the letters that have been correctly guessed displayed as is.
Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess.
Hints:

Store the letters that have been correctly guessed in an initially empty ("") String object, using the concatenation operator (+=).
A fair amount of partial credit shall be given for just being able to respond with "correct" / "incorrect" for each second user guess.
Grading:

Performance Indicator [1] [2] [3]
Readability and documentation 0 1 1
Use of if statements 1 2 2
Use of while loops 1 2 2
Functional requirements 2 3 4
Efficiency 0 1 1
Sample run(s):
Please enter the phrase to guess at : who do you love



Common Phrase
-------------
??? ?? ??? ????


Enter a lowercase letter guess : f



Common Phrase
-------------
??? ?? ??? ????


Enter a lowercase letter guess : o



Common Phrase
-------------
??o ?o ?o? ?o??


Enter a lowercase letter guess : s



Common Phrase
-------------
??o ?o ?o? ?o??


Enter a lowercase letter guess : w



Common Phrase
-------------
w?o ?o ?o? ?o??


Enter a lowercase letter guess : h



Common Phrase
-------------
who ?o ?o? ?o??


Enter a lowercase letter guess : d



Common Phrase
-------------
who do ?o? ?o??


Enter a lowercase letter guess : e



Common Phrase
-------------
who do ?o? ?o?e


Enter a lowercase letter guess : y



Common Phrase
-------------
who do yo? ?o?e


Enter a lowercase letter guess : u



Common Phrase
-------------
who do you ?o?e


Enter a lowercase letter guess : l



Common Phrase
-------------
who do you lo?e


Enter a lowercase letter guess : v



Common Phrase
-------------
who do you love



Explanation / Answer

those steps you described at the bottom are exactly it! all you were missing was a little knowledge about java Strings and how to play around with them. you can read about all the neat functions that come with strings right here (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html)

Here is code that behaves the way you want it, it evern accepts uppercase phrases and converts them to lowercase before comparing :). The code is fairly straightforward, so run it a couple times, read through it carefully to understand each step, and if anything isn't as it should be - you can PM me for modifications and/or further explanation. Otherwise , please remeber to rate, and good luck :)

import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the phrase to guess at: ");
        String commonPhrase = input.nextLine();
        commonPhrase = commonPhrase.toLowerCase();
        String guessed = "";
        for (int i = 0; i < commonPhrase.length(); i++) {
            if (commonPhrase.charAt(i) != ' ') {
                guessed += "?";
            } else {
                guessed += " ";
            }
        }

        do {
            System.out.println(" Common Phrase ------------- " + guessed);
            char aguess = ' ';
            System.out.print("Enter a lowercase letter guess: ");
            aguess = input.nextLine().charAt(0);
            if (commonPhrase.indexOf(aguess) > -1) {
                String prev = guessed;
                guessed = "";
                for (int i = 0; i < commonPhrase.length(); i++) {
                    if (commonPhrase.charAt(i) == aguess) {
                        guessed += aguess;
                    } else {
                        guessed += prev.charAt(i);
                    }
                }
            }
        } while (guessed.compareTo(commonPhrase) != 0);
        System.out.println(" Common Phrase ------------- " + guessed);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote