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

I used 9 methods. I broke the problem into small pieces. I used a method to solv

ID: 666496 • Letter: I

Question

I used 9 methods. I broke the problem into small pieces. I used a method to solve each of the pieces. Some of the methods were very short and some took some work. The algorithm for this problem requires the following 7 steps to be performed: 1) Print an introduction 2) Read an input string 3) Check for legal characters in the input string. I only allowed characters from the alphabet and the blank character. 4) Create an answer string of question marks and a string that indicates the characters that have been used. 5) Read a legal character. 6) Place a legal character in the answer string. 7) Determine if all the characters have been found. Steps 5 through 7 are in a loop. I used a method to solve each of these steps. I encourage you first to work through this algorithm with sample input to demonstrate to your satisfaction that it does solve the problem. Then solve each step with a method. The one public method will call these methods to solve the problem. Remember the String class has a method contains() that returns true or false depending on whether the string contains the string passed in as an argument. Example: String str = “the string”; s.contains(“t”); will return true because string str has a t in it. This is what I got so far: Thanks

import java.util.Scanner;
public class GuesstheString
{
   static String wordChoice;
   static boolean playAgain = true;

   public static void GuesstheString()
   {
       Introduction();
       readStringWords();
   }

   private static void Introduction()
   {
       System.out.println("This game prompts for a string of words. It" + " "
               + "then allows the player to guess the characters" + " "
               + "in the string of words. It keeps track of the" + " "
               + "characters guessed and tells the player when he wins");
   }

   private static void readStringWords()
   {
       System.out.println();
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Please enter a string of words");
       wordChoice = keyboard.nextLine();

       System.out.println(" Original string: " + wordChoice);
       System.out.println(" Guessed String: ");
       System.out.println();
       System.out.println(" Characters to choose from:");
       System.out.println(" abcdefghijklmnopqrstuvwxyz");
       System.out.println();
       System.out.println(" Please guess a character: ");
       System.out.println(" ");
       guesstheString();
   }

   private static void guesstheString()
   {
       while(playAgain)
       {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Original string: " + wordChoice);
           System.out.println(" Guessed String: ");
           System.out.println();
           System.out.println(" Characters to choose from:");
           System.out.println(" abcdefghijklmnopqrstuvwxyz");
           System.out.println();
           System.out.println(" Please guess a character: ");
           String guess = keyboard.nextLine();
           System.out.println("Character read: " + guess);
       }
       playAgain = false;

   }
  
   private static void alphabetString()
   {
         
   }

   private static void allCharactersHadBeenFound()
   {
      
   }

   private static void makeChoiceToContinue()
   {
       Scanner keyboard = new Scanner(System.in);
       System.out.println();
       System.out.println("Would you like to play again?(yes/no)");
       String continueGame = keyboard.next();

       if (continueGame.equalsIgnoreCase("yes"))
       {
           readStringWords();
       }
       else
       {
           System.out.println();
           Menu.printMenuChoices();
       }
   }

}

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

class Hangman
{


public static void main(String[] args)
{
Start();
}
public static void Start()
{
String[] words = { "cat", "dog", "book", "breakfast", "telephone", "pencil","smoke", "drive", "town", "polish", "hat"};
String word;
Random rand = new Random();
int w1 = rand.nextInt(words.length);
word = words[w1];
String guess = "";
int wl = word.length()+1;
for (int i = 1; i < wl; i++)
{
guess += "*";
}
String lu = "";
int tt = 0;
boolean canPlay = true;
Main(guess , tt, canPlay, lu, word);
}
public static void Main (String guess, int tt, boolean canPlay, String lu, String word)
{
if(tt==6)
{
//canPlay = false;
Lose();
return;
}
else
{
char g1[]= guess.toCharArray();
char w2[]= word.toCharArray();
System.out.print (" (Guess) Enter a letter in word");
for(int x = 0; x < g1.length; x++)
{
System.out.print(g1[x]);
}
System.out.print(" > ");
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
public static void Guess(String guess, int tt, boolean canPlay, String lu, String word, char g1[], char w2[])
{
String tg1 = new String(g1);
String tg2 = new String(w2);
if(tg1.equals(tg2))
{
System.out.println("The word is " + word + ", " + " You missed " + tt + "time");
Win();
}
else
{
if(tt == 6)
{
System.out.println("You lost. The word was: " +word);
Lose();
}
else
{
Scanner input = new Scanner(System.in);
String letter = input.next();
if(word.contains(letter))
{
if(lu.contains(letter))
{
tt +=1;
System.out.println( " " + letter + " is already in the word ");
}
else
{
int wl = word.length();
for(int i = 0; i < wl; i++)
{
char aChar = letter.charAt(0);
char bChar = w2[i];
if(bChar == aChar)
{
g1[i]= aChar;
}
}
}
}
else
{
tt +=1;
System.out.println(" " + letter + " is not in the word ");
lu +=letter;
}
System.out.println();
boolean display = false;
for(int x=0; x < g1.length; x++)
{
if(g1[x] == '*')
display = true;
}
if(display)
{
System.out.println("(Guess) Enter a letter in word");
for(int x = 0; x<g1.length; x++)
{
System.out.print(g1[x]);
}
System.out.print(" > ");
}
lu += letter;
Guess(guess, tt, canPlay, lu, word, g1, w2);
}
}
}
public static void Lose()
{
Scanner input1 = new Scanner(System.in);
System.out.print(" Do you want to guess for another word? Enter y/n >");
String pa1 = input1.next();
if(pa1.contains ("y") || pa1.contains ("Y"))
{
Start();
}
}
public static void Win()
{
Scanner input2 = new Scanner(System.in);
System.out.print(" Do you want to quess for another word? Enter y/n > ");
String pa = input2.next();
if(pa.contains("y") || pa.contains("Y"))
{
Start();
}
}
}

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