In: Computer Science
Guidelines for the program:
You are allowed to use the following methods from the Java API:
Create a class called HW2 that contains the following methods:
replaceFirstK: takes a String, two chars, and an int as input and returns a String that is the same as the parameter String except that first int occurrences of the first char parameter are replaced with the second char parameter.
> HW2.replaceFirstK("Mississippi River", 'i', 'I', 3) "MIssIssIppi River" > HW2.replaceFirstK("Missouri River", 'r', '*', 3) "Missou*i Rive*"
allChars: takes two char parameters. Creates a String containing all characters, in order, from the first char parameter (inclusive) to the last (inclusive).
> HW2.allChars('d', 'm') "defghijklm"
showCharOfString: takes two String parameters. Outputs a new String that is the same as the first String except that: for each character of the first String, if the character is not in the second String, replace that character with the underscore ('_').
> HW2.showCharOfString("Missouri River", "s SR!r") "__ss__r_ R___r"
hangman: Takes a String and an int and returns a boolean. The method plays the game of hangman where the String is the word the player must guess and the int is the maximum number of "bad guesses" allowed. The game should work as follows:
hiddenString: takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
> HW2.hiddenString(new char[]{'a','b','r','a','c','a','d','a'}, "acad") true > HW2.hiddenString(new char[]{'a','b','r','a','c','a','d','a'}, "carb") true > HW2.hiddenString(new char[]{'a','b','r','a','c','a','d','a'}, "brad") false
hiddenString: takes a 2-dimensional array of char and a String as input parameters and returns a boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading in any straight direction (up, down, left, right, or diagonal).
> HW2.hiddenString(new char[][]{{'a', 'b', 'c'},{'r','c','a','d'},{'b','r'}}, "bcc") true > HW2.hiddenString(new char[][]{{'a', 'b', 'c'},{'r','c','a','d'},{'b','r'}}, "ace") false > HW2.hiddenString(new char[][]{{'a', 'b', 'c'},{'r','c','a','d'},{'b','r'}}, "cad") true
Extra Credit (up to 10%)capitalizeWords takes a String as input and returns a String as output. Any word in the input string (a word is defined as a sequence of Enlish letters plus the hypen -) that contains any capitalized letter will have all its letters capitalized in the output string.
> HW2.capitalizeWords("Guess what?? There are twenty-sIx letters in the English alphABEt!") "GUESS what?? THERE are TWENTY-SIX letters in the ENGLISH ALPHABET!"
The below program illustrates the above mentioned functionalities upto hangman:
import javax.swing.JOptionPane;
public class HW2 {
public static String replaceFirstK(String str, char c1, char c2, int n){
char[] cArray = str.toCharArray();
int count=0;
for(int i=0;i<cArray.length;i++){
if(count==n)
break;
if(cArray[i]==c1){
cArray[i]=c2;
count++;
}
}
return String.valueOf(cArray);
}
public static String allChars(char c1, char c2){
int ascii1 = (int)c1;
int ascii2 = (int)c2;
String result="";
for(int i=ascii1;i<=ascii2;i++){
result= result+(char)i;
}
return result;
}
public static String showCharOfString(String s1, String s2){
String s3="";
char[] cArray = s1.toCharArray();
for(int i=0;i<cArray.length;i++){
if(s2.indexOf(cArray[i])==-1){
s3=s3+"_";
}else{
s3=s3+cArray[i];
}
}
return s3;
}
public static boolean hangman(String s1, int num){
StringBuilder s2 = new StringBuilder("");
int badGuesses=0;
String guess="",result2="";
String result1=showCharOfString(s1, s2.toString());
char c;
while(badGuesses<=num && !(result2.equals(s1))){
while ((guess=JOptionPane.showInputDialog("Guess a letter: ")).length() != 1){
System.out.println("enter single character");
}
s2.append(guess.charAt(0));
result2=showCharOfString(s1, s2.toString());
if(result1.equals(result2)){
badGuesses++;
}
System.out.println(result2+" "+badGuesses);
result1=result2;
}
if(result2.equals(s1))
return true;
return false;
}
public static void main(String[] args){
System.out.println(replaceFirstK("JyothiSreeBhima", 'h', 'p', 1));
System.out.println(allChars('d','m'));
System.out.println(showCharOfString("Missouri River","s SR!r"));
if(hangman("Missouri River",5))
System.out.println("You have guessed correctly");
else
System.out.println("You re not able to guess the word");
}
}
The possible outputs for the above program is :
I have provided the code for only first 4 questions as we can answer maximun 4 parts of a quetion. Please submit the remaining question as a seperated question.
If you have any queries regarding this answer, please reach out through comment section.