in JAVA -- need to use an arraystack for this and can only manipulate smartstrin
ID: 3871647 • Letter: I
Question
in JAVA -- need to use an arraystack for this and can only manipulate smartstring.java aka the one that says :
public class SmartString implements SmartStringADT at the beginning
//for SmartStringTest.java:
import static org.junit.Assert.*;
import org.junit.Test;
public class SmartStringTest {
//Test insert method
@Test
public void testinsert1() {
SmartString evaluator = new SmartString();
evaluator.insert(0, "Hello");
evaluator.insert(4, ", how are you?");
assertEquals("Hello, how are you?", evaluator.toString());
}
@Test
public void testinsert3() {
SmartString evaluator = new SmartString("Hello, Goodbye");
assertEquals("Hello, Goodbye", evaluator.toString());
}
@Test(expected = InvalidSmartStringException.class)
public void testinsert5() {
SmartString evaluator = new SmartString("Hi");
evaluator.insert(-1, ", how are you?");
fail("Invalid Smart String action not detected");
}
// Test delete method
@Test
public void testdelete4() {
SmartString evaluator = new SmartString("Hello, Goodbye");
evaluator.delete(5, 20);
assertEquals("Hello", evaluator.toString());
}
@Test
public void testdelete6() {
SmartString evaluator = new SmartString("Hello, Goodbye");
evaluator.delete(0, 20);
assertEquals("", evaluator.toString());
}
@Test(expected = InvalidSmartStringException.class)
public void testdelete7() {
SmartString evaluator = new SmartString("Hello, Goodbye");
evaluator.delete(20, 25);
fail("Invalid Smart String action not detected");
}
@Test(expected = InvalidSmartStringException.class)
public void testdelete10() {
SmartString evaluator = new SmartString("Good morning.");
evaluator.delete(-1, 1);
fail("Invalid Smart String action not detected");
}
//Test insert and delete
public void testinsertAndDelete2() {
SmartString evaluator = new SmartString();
evaluator.insert(0, "Hello, how are you?");
evaluator.delete(5, 14);
assertEquals("Hello", evaluator.toString());
}
//Test undo methods
@Test
public void undo3() {
SmartString evaluator = new SmartString("Hello, Goodbye");
evaluator.delete(7, 7);
evaluator.insert(6, "how are you?");
evaluator.insert(18, " I am great!");
evaluator.delete(21,2);
evaluator.insert(20, "'");
evaluator.undo();
evaluator.undo();
evaluator.undo();
assertEquals("Hello, how are you?", evaluator.toString());
}
@Test(expected = InvalidSmartStringException.class)
public void undo5() {
SmartString evaluator = new SmartString();
evaluator.undo();
fail("Invalid Smart String action not detected");
}
}
//next class- SmartStringDriver.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class SmartStringDriver {
public static void main(String[] args) {
//Declare variables and objects
Scanner scan = new Scanner(System.in);
int action = 1, start = 0, count = 0;
String menu = " (1) insert (2) delete (3) Undo (4) Quit";
SmartString str;
String insertS = "";
System.out.println("Enter your initial Smart String:");
str = new SmartString(scan.nextLine());
try{
do{
//Prompt for an action - Insert, Delete, Undo, Quit
System.out.println("Your smart string is: " + str + " (indexing from 0 to " + (str.toString().length()-1) + " - length of " + str.toString().length() + ")");
System.out.println(menu);
System.out.println("Enter an action");
action = scan.nextInt();
scan.nextLine();
//Determine which action and prompt for necessary input
switch (action) {
case 1:
System.out.println("Enter the position (begin counting at 0) of the letter after which to insert " + insertS);
start = scan.nextInt();
scan.nextLine();
System.out.println("Enter the string to insert");
insertS = scan.nextLine();
//Invoke SmartString insert method
str.insert(start, insertS);
break;
case 2:
System.out.println("Enter the position (begin counting at 0) of the first letter to delete");
start = scan.nextInt();
System.out.println("Enter the number of letters to delete (count)");
count = scan.nextInt();
//Invoke SmartString delete method
str.delete(start, count);
break;
case 3:
//Invoke SmartString undo method
str.undo();
break;
}
}while (action != 4);
}
catch(InputMismatchException e) {
System.out.println("Entry must be numeric!");
}
System.out.println(" Your final Smart String is " + str);
}
}
//next class - SmartStringException
public class InvalidSmartStringException extends RuntimeException {
public InvalidSmartStringException()
{
super("Unable to change Smart String!! ");
}
}
//next class -- SmartString.java
public class SmartString<T> implements SmartStringADT<T>
{
ArrayStack<Changes> stack1 = new ArrayStack<Changes>();
StringBuilder string;
public SmartString(String s){
string = new StringBuilder(s);
}
"public void insert(int pos, String sstring){"
"string.insert(pos, sstring);"
"Changes n = new Changes(pos, pos+sstring.length(), string,""ins"");"
stack1.push(n);
}
"public void delete(int pos, int count){"
"StringBuilder temp = new StringBuilder(string.substring(pos, pos+count));"
"string.delete(pos, pos + count);"
"Changes n = new Changes(pos, count, temp,""del"");"
stack1.push(n);
}
public String toString(){
return string.toString();
}
public void undo() {
Changes n = astack.pop();
String type = n.getType();
"if(type.equals(""ins"")){"
"string.delete(n.getBegin(), n.getEnd());"
}
else{
"string.insert(n.getBegin(), n.getStr());"
}
}
}
//last class - SmartStringADT.java
public interface SmartStringADT {
public void insert(int pos, String sstring);
public void delete(int pos, int count);
public void undo();
public String toString();
}
Explanation / Answer
testcase01.c
================================================================================
//SmartArray.c
//SmartArray.h
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.