Question: Must be in java and can you include comments explaining what each step does ? Using the ArrayBoundedStack or ArrayListStack class, create an application EditString
Must be in java and can you include comments explaining what each step does ? Using the ArrayBoundedStack or ArrayListStack class, create an application EditString that prompts the user for a string and then repeatedly prompts the user for changes to the string, until the user enters an X, indicating the end of changes. Legal change operations are: Umake all letters uppercase Lmake all letters lowercase Rreverse the string C ch1 ch2change all occurrences of ch1 to ch2 Zundo the most recent change --------- User input "All dogs go to heaven" U R Z C O A C A t Z "output should be "ALL DAGS GA TA HEAVEN"
import java.util.Scanner;
/*
* @Aidan Conniff
* If the user enters:
* All dogs go to heaven
* U
* R
* Z
* C - O A
* C - A t
* Z
* X
* the output from the program will be "All DAGS GA TA HEAVEN"
*
* If the user enters:
* Fly Eagles Fly
* L
* R
* C - yl it
* Z
* R
* U
* X
* the output from the program will be "FLY EAGLES FLY"
*
*
*/
public class EditStringStarterCode {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String editString;
ArrayBoundedStack
System.out.println("Welcome to Edit String");
System.out.println("You can do the following operations on the String:");
System.out.println("U - make all letters uppercase");
System.out.println("L - make all letters lowercase");
System.out.println("R - reverse the String");
System.out.println("C - change all occurences of substring 1 with substring 2");
System.out.println("Example: C ch1 ch2 will change any occurence of ch1 within a String with ch2");
System.out.println("Z - undo the most recent change");
System.out.println("Please enter a String");
editString = in.nextLine();
System.out.println("Enter an operation");
String operation = in.nextLine();
String reverse;
while(!operation.equalsIgnoreCase("X")) {
if(operation.equalsIgnoreCase("U")) {
editString = editString.toUpperCase();
}
else if(operation.equalsIgnoreCase("L")) {
editString = editString.toLowerCase();
}
else if(operation.equalsIgnoreCase("R")) {
StringBuilder sb=new StringBuilder(editString);
editString=sb.reverse().toString(); //reverse string
stack.push(editString); //push this string in stack
}
else if(operation.equalsIgnoreCase("C")) {
System.out.println("Enter substring to repalce");
String replace = in.nextLine();
System.out.println("Enter substring to replace with");
String replaceWith = in.nextLine();
replace=stack.peek().replace(replaceWith.charAt(2),replaceWith.charAt(4));//replace() replaces old char with new char
stack.push(replace);
} else if(operation.equalsIgnoreCase("Z")) {
stack.pop(); //remove recent operate string from stack
}
System.out.println("Enter next operation or X to quit");
operation=in.nextLine();
}
stack.push(editString);
System.out.println("Thanks for playing. Your final string is");
if(!stack.isEmpty()) {
System.out.println(stack.top());
}
in.close();
}
}
HAVE THE MAJORITY DONE CANT GET "Z" AND "C"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
