Can someone help me a java  program that write sa showLetters method to be used in a Hangman game. The method should have two  parameters: a word and a string of characters representing a sequence of guesses. The method should then return a new string with any guessed letters in their correct position and unguessed letters shown as underscores. For example, the method call: Word.showLetters("computing", "gpo"); would display _o_p____g That is, any letters guessed are shown in their correct location and an underscore represents letters that are not guessed. The method should be a static method of a Word class and could be used as shown in the following program (see section in bold):

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Can someone help me a java  program that write sa showLetters method to be used in a Hangman game. The method should have two  parameters: a word and a string of characters representing a sequence of guesses. The method should then return a new string with any guessed letters in their correct position and unguessed letters shown as underscores.

For example, the method call:

Word.showLetters("computing", "gpo");

would display

_o_p____g

That is, any letters guessed are shown in their correct location and an underscore represents letters that are not guessed.

The method should be a static method of a Word class and could be used as shown in the following program (see section in bold):

import java.util.Scanner; public class TestShowLetters { public static void main(String [] args) { Scanner in = new Scanner(System.in); // Ask the user for a word and some guesses System.out.print("Enter a word and some guesses: "); String word = in.next(); String guesses = in.next(); String show = Word.showLetters(word, guesses); System.out.println(show); } }

And the program would be executed as follows:

$ java TestShowLetters Enter a word and some guesses: computing gpo _o_p____g

Note : Your method will actually be tested with the program below:

import java.util.Scanner; public class Main { // generate the alphabet static String getAlphabet() { String alphabet = ""; for(int i = 0; i < 26; i++) alphabet += (char) ('a' + i); return alphabet; } static boolean contains(String word, char let) { for(int i = 0; i < word.length(); i++) if(word.charAt(i) == let) return true; return false; } public static void main(String []args) { Scanner input = new Scanner(System.in); String alphabet = getAlphabet(); System.out.println("Enter a word:"); String word = input.next(); String guesses = ""; // Start with no guesses // 1. try every second letter in the alphabet for(int i = 0; i < alphabet.length(); i += 2) if(contains(word, alphabet.charAt(i))) { guesses += alphabet.charAt(i); // Try this letter System.out.println(Word.showLetters(word, guesses)); } // 2. Try the same thing backwards guesses = ""; // reset guesses for(int i = alphabet.length() - 1; i >= 0; i -= 2) if(contains(word, alphabet.charAt(i))) { guesses += alphabet.charAt(i); // Try this letter System.out.println(Word.showLetters(word, guesses)); } // 3. try no guesses (i.e. empty string) System.out.println(Word.showLetters(word, "")); // 4. all letters for(int i = 0; i < alphabet.length(); i++) if(contains(word, alphabet.charAt(i))) guesses += alphabet.charAt(i); // Try this letter System.out.println(Word.showLetters(word, guesses)); } }

This code reads in a word and then tests your method in four different ways

  1. Every second letter in alphabetic order.
  2. Every second letter in reverse alphabetic order.
  3. No letters
  4. Every letter in the word, though in alphabetic order.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY