Main topics: User dened methods Arrays Just focus on these topics. I haven`t lea
ID: 3690100 • Letter: M
Question
Main topics:
User dened methods
Arrays
Just focus on these topics. I haven`t learned anything past these topics. It can`t have any complex things in it, and look at the requirements, please.
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 String variable using Scanner.nextLine() - under the assumption that the phrase consists of nothing but under-case letters and spaces.
2. A second char array variable, expSoFar, is constructed such that:
(a) It has the same length as the ”common phrase”. (b) It contains spaces everywhere that the ”common phrase” does. (c) All its other characters are the ’?’ character.
3. The following is than repeated until the entire ”common phrase” is exposed:
• The current value of expSoFar is displayed. • Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess. • The value of expSoFar is modied such that any/all locations where guess occurs in the ”common phrase”, the ’?’ character has been replaced with guess.
Rules and Requirements:
You must write and use a method named init that takes the ”common phrase” as input and returns a char array that is the same length as the ”common phrase”; contains spaces everywhere the ”common phrase” does; and contains the ’?’ everywhere the ”common phrase” does not contain a space.
public static char[] init(String comPhrase)
• You must write and use a method named expose that takes the values of the ”common phrase”, the expSoFar array, and the current guess guess as input and returns nothing. However the eect of calling this methods results in the array actual aguement given for expSoFar to be modied such that: any/all locations where guess occurred in the ”common phrase”, the ’?’ character ( in expSoFar ) has been replaced with guess.
public static void expose(String comPhrase, char[] expSoFar, char guess)
• You must write and use a method named done that takes the values of the ”common phrase” and the expSoFar array as input and returns true exactly when all the letters in expSoFar have been exposed.
public static boolean done(String comPhrase, char[] expSoFar)
Sample run(s):
Please enter the phrase to guess at : who do you love
Common Phrase
------------
??? ?? ??? ????
Enter a lowercase letter guess : 5
Enter a lowercase letter guess : F
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
/** Prog09.java toimplement commom phrrase game **/
import java.util.Scanner;
public class prog09 {
public static void main(String [] args)
{
Scanner stdIn = new Scanner(System.in);
String comPhrase;
System.out.print("Please enter the phrase to guess at : ");
comPhrase = stdIn.nextLine();
char guess;
String expSoFar = initExpSoFar(comPhrase);
do
{
displayExpSoFar(expSoFar);
System.out.println();
guess = getGuess();
expSoFar = expose(comPhrase, expSoFar, guess);
} while (!done(comPhrase, expSoFar));
displayExpSoFar(expSoFar);
stdIn.close();
}
public static String initExpSoFar(String a)
{
String expSoFar="";
for (int b=0;b<a.length();++b)
{
if (a.charAt(b)==' ')
expSoFar+=" ";
else
expSoFar+="?";
}
return expSoFar;
}
public static void displayExpSoFar(String a)
{
System.out.println();
System.out.println("Common Phrase");
System.out.println("-------------");
System.out.println(a);
return;
}
public static char getGuess()
{
Scanner stdIn=new Scanner(System.in);
String guess;
do
{
System.out.print("Enter a lowercase letter guess : ");
guess = stdIn.nextLine();
} while (guess.length() != 1 ||
guess.charAt(0) < 'a' || guess.charAt(0) > 'z');
return guess.charAt(0);
}
public static String expose(String a,String b,char c)
{
int loc=a.indexOf(c);
while (loc!=-1)
{
b=b.substring(0,loc)+c+b.substring(loc+1);
loc=a.indexOf(c,loc+1);
}
return b;
}
public static boolean done(String a, String b)
{
boolean done;
done=a.equals(b);
return done;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.