Need help with this please: Goals • Learn test-driven development. • Learn how t
ID: 3910468 • Letter: N
Question
Need help with this please:
Goals
• Learn test-driven development.
• Learn how to write and use methods to simplify and reduce the code.
Problem
The scramble program is useful, but its limitations for 5-letter words is a problem and not a limitation that is
appreciated by the users. Also, management has looked at the line count and has expressed concern that the
code base and have suggested that it may need to be “refactored.”
User Story
Users want essentially the same interface but want unlimited sized words so there are minimal changes to the
original interface except for using “end” as the sentinel value (because someone may want end scrambled).
Now end should be used. For example:
Enter a word: croak
Scrambled word: AKORC
Fake word: KAROC (note that this is only if you did the extra credit on the first scramble assignment)
Enter a word: 0
Tasks
1. Create a new project called Scramble2.
2. Copy and paste this into Scramble2.java file in the project.
package scramble2;
import java.util.Scanner;
public class Scramble2 {
static boolean TESTIT = false;
public static String removeCharAt(String word, int i) {
// Return a string with the character at i removed from it
}
public static String scramble(String word) {
String scram_word = "";
while (word.length() > 0) {
// Build a scrambled word using elements of you previous code and use
// removeCharAt() instead of the substrings.
}
return scram_word;
}
public static int test() {
int errors = 0;
String word = "break";
String scram_word = scramble(word);
System.out.println(word + " scrambled is '" + scram_word + "'");
return errors;
}
public static void main(String[] args) {
if (TESTIT) {
int errors = test();
System.exit(errors);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = input.next();
while (!word.equals("0")) {
String scram_word = scramble(word);
System.out.println(scram_word);
System.out.print("Enter a word: ");
word = input.next();
}
}
}
3. Implement the methods not yet implemented. If you are struggling with removeCharAt()
implementation, consider adding a “unit test” to the test() function to specifically test that method.
Checklist
Use the following as guidance for getting a 3 on this project:
• Working removeCharAt(). (2 point)
• Working scramble(). (3 point)
• Add a robust set of test cases (at least 5) to test(). (1 point)
• Extra credit (4 points): Write a getFakeWord() method that interleaves produces consonant-vowel-
consonant-vowel until all consonants and vowels have been used in the scrambled word. Must use
removeCharAt() to get credit. Hint: You should write two methods:
o getConsonant(String word) which returns the index of the first consonant in the given word.
Have it return -1 if no consonants exist in the given word. Create a unit test for this if you are
struggling to get it working.
o getVowel(String word) which returns the index of the first vowel in the given word. Have it
return -1 if no consonants exist in the given word. Create a unit test for this if you are
struggling to get it working.
This is what I have so far but its only doing the scramble portion I dont think I am doing the rest correctly.
package scramble;
import java.util.Scanner;
public class Scramble {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 5-letter word: ");
String word = input.next();
int wordLength = word.length();
if (wordLength == 5) {
word = word.toUpperCase(); //to uppercase
String scram_word = ""; //scrambled word
//variables
int i;
char c;
int j; //to iterate
//used to generate random number 4 times (5 to 2)
for (j = 5; j >= 2; j--) {
i = (int) (Math.random() * j); //generate random number between 0 and j
c = word.charAt(i); //get char to remove
scram_word = scram_word + c; //add char to scram_word
String before = word.substring(0, i); //string before c
String after = word.substring(i + 1); //string after c
word = before + after; //word without c
}
scram_word = scram_word + word; //add last char
System.out.println("Scrambled word: " + scram_word); //print scram_word
String fake_word = ""; //fake word
String vowels = ""; //vowels
String consonants = ""; //consonants
//get consonants and vowels
for (j = 0; j < scram_word.length(); j++) {
if (scram_word.charAt(j) == 'A' || scram_word.charAt(j) == 'E' || scram_word.charAt(j) == 'I' || scram_word.charAt(j) == 'O' || scram_word.charAt(j) == 'U') {
vowels = vowels + scram_word.charAt(j);
} else {
consonants = consonants + scram_word.charAt(j);
}
}
//alternate vowels and constants to fake_word
for (j = 0; j < vowels.length() && j < consonants.length(); j++) {
fake_word = fake_word + consonants.charAt(j) + vowels.charAt(j);
}
//for remaining vowels left add them to fake_word
if (vowels.length() > j) {
fake_word = fake_word + vowels.substring(j);
}
//for any consnant left add them to fake_word
if (consonants.length() > j) {
fake_word = fake_word + consonants.substring(j);
}
//fake_word
System.out.println("Fake word: " + fake_word);
} else {
System.out.println("Word must be 5 letters");
System.exit(1);
}
}
}
Explanation / Answer
here is your answer : ---------->>>>>>>>
import java.util.Scanner;
import java.util.Random;
public class Scramble2 {
static boolean TESTIT = false;
public static String removeCharAt(String word, int i) {
// Return a string with the character at i removed from it
if(i == 0){
return word.substring(1);
}
if(i == word.length()-1){
return word.substring(0,word.length()-1);
}
String s = word.substring(0,i) + word.substring(i+1);
return s;
}
public static String scramble(String word) {
String scram_word = "";
int i;
while (word.length() > 0) {
// Build a scrambled word using elements of you previous code and use
// removeCharAt() instead of the substrings
i = (int)(Math.random()*word.length());
scram_word = scram_word + word.charAt(i);
word = removeCharAt(word,i);
}
scram_word = scram_word.toUpperCase();
return scram_word;
}
public static String getFake(String scram_word){
String fake_word = ""; //fake word
String vowels = ""; //vowels
String consonants = ""; //consonants
//get consonants and vowels
int j;
for (j = 0; j < scram_word.length(); j++) {
if (scram_word.charAt(j) == 'A' || scram_word.charAt(j) == 'E' || scram_word.charAt(j) == 'I' || scram_word.charAt(j) == 'O' || scram_word.charAt(j) == 'U') {
vowels = vowels + scram_word.charAt(j);
} else {
consonants = consonants + scram_word.charAt(j);
}
}
//alternate vowels and constants to fake_word
for (j = 0; j < vowels.length() && j < consonants.length(); j++) {
fake_word = fake_word + consonants.charAt(j) + vowels.charAt(j);
}
//for remaining vowels left add them to fake_word
if (vowels.length() > j) {
fake_word = fake_word + vowels.substring(j);
}
//for any consnant left add them to fake_word
if (consonants.length() > j) {
fake_word = fake_word + consonants.substring(j);
}
return fake_word;
}
public static int test() {
int errors = 0;
String word = "break";
String scram_word = scramble(word);
System.out.println(word + " scrambled is '" + scram_word + "'");
return errors;
}
public static void main(String[] args) {
if (TESTIT) {
int errors = test();
System.exit(errors);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = input.next();
while (!word.equals("0")) {
String scram_word = scramble(word);
System.out.println("Scramble : "+scram_word);
System.out.println("Fake Word : "+getFake(scram_word));
System.out.print("Enter a word: ");
word = input.next();
}
}
}