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

Java Programming There is a ”fun” children’s game where one child thinks of a ”c

ID: 3688985 • Letter: J

Question

Java Programming

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 modified such that any/all locations where guess occurs in the ”com-

mon phrase”, the ’?’ character has been replaced with guess.

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.

You must write and use a method named expose that takes the values of the ”common phrase”, the expSoFar, and the current guess guess as input and returns a String that is equal to expSoFar except any/all locations where guess occurred in the ”common phrase”, the ’?’ character has been replaced with 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.

Sample run(s):

Explanation / Answer

Java program code:

//Header section
import java.util.Scanner;

//class FunGame.java
   public class FunGame
   {
       public static void main(String[] args)
       {
           String commonPhrase;
           char guessYesNo;
           int space=0, consonant=0, vowel=0;
          
           //Read input
           Scanner scn=new Scanner(System.in);
          
           System.out.print("Please enter the phrase to guess at : ");
           commonPhrase = scn.nextLine();
           char temp [] = new char [commonPhrase.length()];
      
           space = initialize(commonPhrase, temp);
      
           display(temp);
           System.out.println();
           guessYesNo = constantCh(scn);
           System.out.println();
          
           consonant=update(temp, commonPhrase, guessYesNo);
          
           while(!commonPhrase.equals(new String(temp)))
           {
               display(temp);
               System.out.println();
               String choice;
               do
               {
                   System.out.print("Would you like to buy a vowel : ");
                   choice = scn.next();
                  
               }while(!choice.equals("y") && !choice.equals("n"));
              
               System.out.println();
              
               if(choice.equals("y"))
               {
                   guessYesNo = getVowel(scn);
                   vowel+=update(temp, commonPhrase, guessYesNo);
               }
               else if(choice.equals("n")) // readability
               {
                   guessYesNo = constantCh(scn);
                   consonant+=update(temp, commonPhrase, guessYesNo);
               }
                  
           }
          
           display(temp);
           System.out.println();
           System.out.println("The common phrase contained: " + space + " space(s), "+ consonant +" consonsant(s) and "+ vowel +" vowel(s)." );
           scn.close();
       }
      
       public static int update(char [] tmpArr, String sPhrase, char guessYesNo)
       {
           int count=0, loc=sPhrase.indexOf(guessYesNo);
          
           while(loc!=-1)
           {
               count++;
               tmpArr [loc] = guessYesNo;
               loc = sPhrase.indexOf(guessYesNo, loc+1);
           }
           System.out.println();
           return count;
       }
          
       public static int initialize(String sPhrase, char [] tmpArr)
       {
           int space=0;
           for(int i=0; i< sPhrase.length(); i++)
           {
               if(Character.isWhitespace(sPhrase.charAt(i)))
               {
                   tmpArr[i] = ' ';
                   space++;
               }
               else
                   tmpArr[i]='?';
           }
           return space;
       }
      
       public static void display(char [] tmpArr)
       {
          
           System.out.println();
           System.out.println("Common Phrase");
           System.out.println("-------------");
          
           for(int i=0; i<tmpArr.length; i++)
               System.out.print(tmpArr[i]);
          
           System.out.println();
          
       }
      
       public static boolean isVowel(char guessYesNo)
       {
           return(guessYesNo=='a'||guessYesNo=='e'||guessYesNo=='i'||guessYesNo=='o'||guessYesNo=='u');
       }
      
       public static char getVowel(Scanner scn)
       {
          
           String guessYesNo;
          
           do
           {
               System.out.print("Enter a lowercase letter guess : ");
               guessYesNo=scn.next();
           }while(guessYesNo.length()!=1 || !(guessYesNo.charAt(0)>='a')|| !(guessYesNo.charAt(0)<='z') ||!isVowel(guessYesNo.charAt(0)));
          
           return guessYesNo.charAt(0);
       }
      
       public static char constantCh(Scanner scn)
       {
           String guessYesNo;
          
           do
           {
               System.out.print("Enter a lowercase letter guess : ");
               guessYesNo=scn.next();
           }while(guessYesNo.length()!=1 || !(guessYesNo.charAt(0)>='a')|| !(guessYesNo.charAt(0)<='z') ||isVowel(guessYesNo.charAt(0)));
          
           return guessYesNo.charAt(0);
       }
          
          
   }

output:

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
-------------
??? ?? ??? ????

Would you like to buy a vowel : n

Enter a lowercase letter guess : o
Enter a lowercase letter guess : s


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

Would you like to buy a vowel : n

Enter a lowercase letter guess : w


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

Would you like to buy a vowel : h
Would you like to buy a vowel : d
Would you like to buy a vowel : e
Would you like to buy a vowel : y

Enter a lowercase letter guess : u


Common Phrase
-------------
w?? ?? ??u ????

Would you like to buy a vowel : n

Enter a lowercase letter guess : i
Enter a lowercase letter guess : v


Common Phrase
-------------
w?? ?? ??u ??v?

Would you like to buy a vowel : y

Enter a lowercase letter guess : o


Common Phrase
-------------
w?o ?o ?ou ?ov?

Would you like to buy a vowel : y

Enter a lowercase letter guess : e


Common Phrase
-------------
w?o ?o ?ou ?ove

Would you like to buy a vowel : y

Enter a lowercase letter guess : h
Enter a lowercase letter guess : d
Enter a lowercase letter guess : y
Enter a lowercase letter guess : l
Enter a lowercase letter guess : o


Common Phrase
-------------
w?o ?o ?ou ?ove

Would you like to buy a vowel : y

Enter a lowercase letter guess : h
Enter a lowercase letter guess : n
Enter a lowercase letter guess : e


Common Phrase
-------------
w?o ?o ?ou ?ove

Would you like to buy a vowel : n

Enter a lowercase letter guess : h


Common Phrase
-------------
who ?o ?ou ?ove

Would you like to buy a vowel : n

Enter a lowercase letter guess : d


Common Phrase
-------------
who do ?ou ?ove

Would you like to buy a vowel : n

Enter a lowercase letter guess : y


Common Phrase
-------------
who do you ?ove

Would you like to buy a vowel : n

Enter a lowercase letter guess : l


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

The common phrase contained: 3 space(s), 6 consonsant(s) and 11 vowel(s).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote