Program Specication: There is a \"fun\" children\'s game where one child thinks
ID: 3573579 • Letter: P
Question
Program Specication: 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 the assumption that the phrase consists of nothing but lowercase letters and spaces. 2. A character array is declared with size exactly equal to that of the "common phrase" entered) to serve as the template of the "common phrase". You then use your initTemplateArray() method to assign its elements such that they are all either '?'s or spaces. Then when letters are guessed, you use your updateTemplateArray() method to "expose" correct guesses in this template array. 3. A variable is declared and initialized to zero, to keep track of how many spaces were placed in the template array. Hint: the return value of your initTemplateArray() method. 4. A variable is declared and initialized to zero, to keep track of how many consonants have been revealed. 5. A variable is declared and initialized to zero, to keep track of how many vowels have been revealed. 6. The following is than repeated until the entire "common phrase" is revealed - the sum of the three variables holding the number of spaces, consonants, and vowels revealed is equal to the length of the "common phrase". (a) The template array is displayed to the second user using your printTemplateArray() method. (b) If any consonants have been revealed then the second user is able to guess vowels or consonants, otherwise they may only guess consonants: Using your getConsonant() method, the second user enters a single lowercase consonant guess. Using your getVowel() method, the second user enters a single lowercase vowel guess. (c) If the consonant guessed is contained in the common phrase, use your updateTemplateArray() method to reveal that guessed character in the template array, and use its return value to update the variable tracking how many consonants have been revealed. (d) If the vowel guessed is contained in the common phrase, use your updateTemplateArray() method to reveal that guessed character in the template array, and use its return value to update the variable tracking how many vowels have been revealed. 7. Once the "common phrase" is completely revealed, you must also report the values of the three variables holding the number of spaces, consonants, and vowels revealed. Requirements Your program must implement and use the following methods: public static int initTemplateArray(String sPhrase, char [] tmpArr) This method modies an array argument via the array parameter tmpArr. It initializes tmpArr to be a template version of sPhrase, that is: { At every index where sPhrase is equal to the space character ' ', the element in tmpArr at that index is set equal to the space character. { At every index where sPhrase is not equal to the space character, the element in tmpArr at that index is set equal to the character '?' And then returns the number of space characters that were used in the "common phrase" - copied into the template array. public static void printTemplateArray(char [] tmpArr) This method prints the elements in the tmpArr parameter to the screen. public static boolean isVowel(char c) This method returns true exactly when c is ( 'a', 'e', 'i', 'o', 'u' ) public static char getConsonant(Scanner stdIn) This method repeatedly prompts for and obtains a user's char response, until it is a lowercase consonant in the range of 'a' to 'z' (inclusive) and then returns it. Hint: a lowercase consonant is a lowercase letter that is not a vowel. public static char getVowel(Scanner stdIn) This method repeatedly prompts for and obtains a user's char response, until it is a lowercase vowel: ( 'a', 'e', 'i', 'o', 'u' ) and then returns it. public static int updateTemplateArray(char [] tmpArr, String sPhrase, char guess) This method modies an array argument via the array parameter tmpArr. It "reveals" all occurrences of the character guess in the array tmpArr, that is: { At every index where sPhrase is equal to the character guess, the element in tmpArr at that index is set equal to the character guess - replacing the '?' character. { At every index where sPhrase is not equal to the character guess, no modication of the element tmpArr at that index is preformed - leaving the '?' or ' ' character. and then returns the number of such occurrences that were "revealed". Tips and Hints You will need to declare / create an array template in your main method, and then pass it into the various methods above as an argument. The size of this array will be the same as the "common phrase" you obtained from the user. Remember that when an array is passed into a method, that method may both access its elements (read) and modify its elements (write) by using the subscript operator. Sample run(s): Please enter the phrase to guess at : hello world Common Phrase ------------- ????? ????? Enter a lowercase consonant guess : l Common Phrase ------------- ??ll? ???l? Would you like to buy a vowel : g Would you like to buy a vowel : n Enter a lowercase consonant guess : 8 Enter a lowercase consonant guess : h Common Phrase ------------- h?ll? ???l? Would you like to buy a vowel : y Enter a lowercase vowel guess : e Common Phrase ------------- hell? ???l? Would you like to buy a vowel : n Enter a lowercase consonant guess : e Enter a lowercase consonant guess : w Common Phrase ------------- hell? w??l? Would you like to buy a vowel : y Enter a lowercase vowel guess : d Enter a lowercase vowel guess : o Common Phrase ------------- hello wo?l? Would you like to buy a vowel : n Enter a lowercase consonant guess : r Common Phrase ------------- hello worl? Would you like to buy a vowel : n Enter a lowercase consonant guess : d Common Phrase ------------- hello world The common phrase contained: 1 space(s), 7 consonsant(s) and 3 vowel(s).
Explanation / Answer
SOURCE CODE:
import java.util.Arrays;
import java.util.Scanner;
public class guessingGame {
private static char[] TemplateArray;
public static int initTemplateArray(String sPhrase, char [] tmpArr)
{
int count = 0;
for(int i=0;i<sPhrase.length();i++)
{
if(sPhrase.charAt(i)==' ')
{
count++;
tmpArr[i]=' ';
}
else
tmpArr[i]='?';
}
TemplateArray = Arrays.copyOf(tmpArr, tmpArr.length);
return count;
}
public static void printTemplateArray(char [] tmpArr)
{
System.out.println("Common Phrase-----------"+String.valueOf(tmpArr));
}
public static boolean isVowel(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
return true;
return false;
}
public static char getConsonant(Scanner stdIn)
{
char inp;
while(true)
{
System.out.print("Enter a lowercase consonant guess : ");
inp = stdIn.nextLine().charAt(0);
int temp = (int)inp;
if((temp>=97 && temp<=122) && isVowel(inp)==false)
break;
}
return inp;
}
public static char getVowel(Scanner stdIn)
{
char inp;
while(true)
{
System.out.print("Enter a lowercase vowel guess : ");
inp = stdIn.nextLine().charAt(0);
if(isVowel(inp))
break;
}
return inp;
}
public static int updateTemplateArray(char [] tmpArr, String sPhrase, char guess)
{
int revealed = 0;
for(int i=0;i<sPhrase.length();i++)
{
if(sPhrase.charAt(i)==guess)
{
revealed++;
tmpArr[i] = guess;
}
}
TemplateArray = Arrays.copyOf(tmpArr, tmpArr.length);
return revealed;
}
public static void main(String[] args) {
int space = 0, vowel = 0, consonant = 0;
char inp;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the common phrase: ");
String sPhrase = sc.nextLine();
char tmpArr[]=new char[sPhrase.length()];
TemplateArray = new char[sPhrase.length()];
space = initTemplateArray(sPhrase,tmpArr);
while((space+consonant+vowel)!=sPhrase.length())
{
inp = getConsonant(new Scanner(System.in));
tmpArr = Arrays.copyOf(TemplateArray, TemplateArray.length);
int count = updateTemplateArray(tmpArr,sPhrase,inp);
if(count > 0)
{
consonant=consonant+count;
tmpArr = Arrays.copyOf(TemplateArray, TemplateArray.length);
printTemplateArray(tmpArr);
}
if((space+consonant+vowel)==sPhrase.length())
break;
System.out.print("Would you like to buy a vowel : ");
inp = (sc).nextLine().charAt(0);
if(inp == 'y')
{
inp = getVowel(new Scanner(System.in));
tmpArr = Arrays.copyOf(TemplateArray, TemplateArray.length);
count = updateTemplateArray(tmpArr,sPhrase,inp);
if(count > 0)
{
vowel=vowel+count;
tmpArr = Arrays.copyOf(TemplateArray, TemplateArray.length);
printTemplateArray(tmpArr);
}
}
}
System.out.println("The common phrase contained:"+space+" space(s), "+consonant+" consonsant(s) and "+vowel+" vowel(s)");
}
}
OUTPUT:
Enter the common phrase: hello world
Enter a lowercase consonant guess : l
Common Phrase-----------??ll? ???l?
Would you like to buy a vowel : n
Enter a lowercase consonant guess : 8
Enter a lowercase consonant guess : h
Common Phrase-----------h?ll? ???l?
Would you like to buy a vowel : y
Enter a lowercase vowel guess : e
Common Phrase-----------hell? ???l?
Enter a lowercase consonant guess : e
Enter a lowercase consonant guess : w
Common Phrase-----------hell? w??l?
Would you like to buy a vowel : y
Enter a lowercase vowel guess : d
Enter a lowercase vowel guess : o
Common Phrase-----------hello wo?l?
Enter a lowercase consonant guess : r
Common Phrase-----------hello worl?
Would you like to buy a vowel : n
Enter a lowercase consonant guess : d
Common Phrase-----------hello world
The common phrase contained:1 space(s), 7 consonsant(s) and 3 vowel(s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.