Question & Answer: I need the following code in Java. Please make this simple and easy to understand. Please read thoroughly……

I need the following code in Java. Please make this simple and easy to understand. Please read thoroughly.

Using the ArrayBoundedStack 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:

U – make all letters uppercase

L – make all letters lowercase

R – reverse the string

C ch1 ch2 – change all occurences of ch1 to ch2

Z – undo the most recent change

You may assume a “friendly user”, that is, the user will not enter anything illegal. When the user is finished, the resultant string is printed. For example, if the user enters:

All dogs go to heaven

U

R

Z

C O A

C A t

Z

the output from the program will be “ALL DAGS GA TA HEAVEN

Expert Answer

 

import java.util.Vector;//Vector class is used since size of vector can grow or shrink as elements are added or removed
import java.util.Scanner;
import java.util.EmptyStackException;
class ArrayBoundedStack<t> extends Vector<t> //generic class which is parameterized with generic type t can be of any type of data which is inheriting Vector class
{
// method for pushing element into stack
public t push(t x)
{
addElement(x);//addElement() in Vector class adds the given element
return x;
}
//method for popping element from stack
public t pop()
{
t obj;
int len = size();
obj = peek();//gets the top most element
removeElementAt(len – 1);//removes top most element
return obj;
}
//gets the topmost element
public t peek() {
int len = size();
if (len == 0) //throws stack is empty
throw new EmptyStackException();
return elementAt(len – 1);//returns top most element
}
}
public class EditString
{
public static void main(String[] args)
{
ArrayBoundedStack<String> obj=new ArrayBoundedStack<String>();
//reading input from console using Scanner class
Scanner s=new Scanner(System.in);
//read string
String s1=s.nextLine();
obj.push(s1);
String s2=s.nextLine();
while(!(s2.equals(“X”)))
{
if(s2.equals(“U”))
{
s1=obj.peek().toUpperCase();//converting string to uppercase
obj.push(s1);
}
else if(s2.equals(“L”))
{
s1=obj.peek().toLowerCase();//converting string to lowercase
obj.push(s1);
}
else if(s2.equals(“R”))
{
s1=new StringBuffer(obj.peek()).reverse().toString();//reversing string using StringBuffer class
obj.push(s1);
}
else if(s2.charAt(0)==’C’)
{
s1=obj.peek().replace(s2.charAt(2),s2.charAt(4));//replace() replaces old char with new char
obj.push(s1);

}
else if(s2.equals(“Z”))//undo operation
{
obj.pop();//pop the top most element
}
s2=s.nextLine();
}
System.out.println(obj.peek());//prints top most element
}
}

Output

All dogs go to heaven
U
R
Z
C O A
C A T
Z
X
ALL DAGS GA TA HEAVEN

 

Still stressed from student homework?
Get quality assistance from academic writers!