In: Computer Science
You are given the main class RecursiveMethods.java which invokes methods that can be selected from a menu. Your task is to implement two of these methods. -The first method isPal(s) determines whether a given string s is a palindrome. For example "12321" is a palindrome but "1231" is not. Currently the method isPal is a stub and you are expected to implement the method. -The second method isSAE(s) determines whether the given string s is a strict arithmetic expression (see below). This method is fully implemented for you. -The third method valueStrict(s) computes the value of the strict arithmetic expression s. Currently the method valueStrict is a stub and you are expected to implement the method using as a guide the implementation of isSAE(s). The methods should be implemented using recursive methods, no points will be given for iterative solutions. You should only need to work with the file RecursiveMethods.java but you will include your util1228 package for obvious reasons. Sample execution is shown below. SUBMISSION NOTES You will submit a single .jar file named A04.jar. Make sure that: 1) The jar file runs. In project properties under Run make sure the main class is set to a04.RecursiveMethods 2) The jar file includes all your source code. In project properties under packaging make sure .java files are not excluded. (see our Brightspace site for details about making JAR files) IMPORTANT NOTES A strict arithmetic expression (SAE) is one of the following: 1. Any nonnegative integer is a SAE (that is a nonempty string made of digits) 2. (x+y), (x*y), (-x), (+x), if x,y are SAEs Strict arithmetic expressions do not allow spaces and do not allow omitting parentheses. The following are valid SAEs: 12, (-12), (3*5), ((3*5)+10), ((-3)*((-5)+10)) The following are NOT valid SAEs: -12, 3*5, (-3)*5, (3), (5+4, (3 + 5) SAMPLE EXECUTION Menu 1. Test whether a given string is a palindrome 2. Test whether a given string is a strict arithmetic expression 3. Evaluate a strict arithmetic expression 4. Quit Enter your choice here: 1 Enter a string: 1234321 "1234321" is a palindrome Press enter to continue... Menu 1. Test whether a given string is a palindrome 2. Test whether a given string is a strict arithmetic expression 3. Evaluate a strict arithmetic expression 4. Quit Enter your choice here: 3 Enter a strict arithmetic expression: ((3*5)+10) The value is 25 Press enter to continue... Menu 1. Test whether a given string is a palindrome 2. Test whether a given string is a strict arithmetic expression 3. Evaluate a strict arithmetic expression 4. Quit Enter your choice here: 3 Enter a strict arithmetic expression: (-3)*5 ERROR: invalid arithmetic expression Press enter to continue... Menu 1. Test whether a given string is a palindrome 2. Test whether a given string is a strict arithmetic expression 3. Evaluate a strict arithmetic expression 4. Quit Enter your choice here: 4 Buy
ackage a04; import java.util.Scanner; import util1228.Menu; import static util1228.Utilities.pause; import static util1228.Utilities.isNumeric; /** * * @author Joshua, Stavros */ public class RecursiveMethods { public static void main(String[] args) { Scanner kbd=new Scanner(System.in); Menu m=createMenu(); int choice,n; String s; do{ m.display(); choice=m.getChoice(); switch(choice){ case 1: System.out.print("Enter a string: "); s=kbd.nextLine().trim(); System.out.println("\""+s+"\" is " +((isPal(s.replace(" ", "").toLowerCase()))?"":"not ") +"a palindrome"); pause(); break; case 2: System.out.print("Enter a string: "); s = kbd.nextLine().trim(); System.out.printf("The string is %sa valid strict arithmetic" + " expression\n", (isSAE(s))?"":"*not* "); pause(); break; case 3: System.out.print("Enter a strict arithmetic expression: "); s = kbd.nextLine().trim(); if (!isSAE(s)) { System.out.println("ERROR: invalid arithmetic expression"); pause(); break; } System.out.printf("The value is %d\n", valueStrict(s)); pause(); break; case 4: System.out.println("\nBuy!\n"); break; case -1: System.out.println("*** Invalid Choice"); pause(); break; } } while(choice!=4); } /** * This method creates the Menu for the program and adds each of the options * * @return the created Menu */ private static Menu createMenu() { Menu m = new Menu("Menu", 8, 50); m.addOption("Test whether a given string is a palindrome"); m.addOption("Test whether a given string is a strict arithmetic " + "expression"); m.addOption("Evaluate a strict arithmetic expression"); m.addOption("Quit"); return m; } private static boolean isPal(String input) { System.out.println("***Method isPal is a stub. It always returns false." + "\n***You are expected to implement this method."); return false; } public static boolean isSAE(String s) { int l = s.length(); if (l==0) return false; if (isNumeric(s)) return true; if (l<3) return false; if (s.charAt(0)!='(' || s.charAt(l-1)!=')') return false; //Here we know that s is of the form (...) String s1, s2; //Next test whether s is of the form (+...) or (-...) if (s.charAt(1)=='+' || s.charAt(1)=='-') { s1 = s.substring(2,l-1); return isSAE(s1); } //Here we know that s is not of the form (+...) or (-...) //Next test whether s is of the form (x+y) or (x*y); that is, //there is a position in s that contains '+' or '*' and the //parts of s to the left and right of '+' or '*' are SAEs for (int i=2; i<l-1; i++) { if (s.charAt(i)=='+' || s.charAt(i)=='*') { s1 = s.substring(1, i); s2 = s.substring(i+1,l-1); if (isSAE(s1) && isSAE(s2)) return true; } } return false; } public static int valueStrict(String s) { System.out.println("***Method valueStrict is a stub. It always returns " + "0.\n***You are expected to implement this method."); return 0; } }
Hello, I have implemented both of the functions for you, I cannot test your program but I have tested the functions explicitly because in your program the Menu class was not given. If you have any further doubts you can always reach out to me in the comment section
/**
*
* @author Joshua, Stavros
*/
import java.util.*;
class RecursiveMethods {
public static void main(String[] args) {
Scanner kbd=new Scanner(System.in);
Menu m=createMenu();
int choice,n;
String s;
do{
m.display();
choice=m.getChoice();
switch(choice){
case 1:
System.out.print("Enter a string: ");
s=kbd.nextLine().trim();
System.out.println("\""+s+"\" is "
+((isPal(s.replace(" ", "").toLowerCase()))?"":"not ")
+"a palindrome");
pause();
break;
case 2:
System.out.print("Enter a string: ");
s = kbd.nextLine().trim();
System.out.printf("The string is %sa valid strict arithmetic"
+ " expression\n",
(isSAE(s))?"":"*not* ");
pause();
break;
case 3:
System.out.print("Enter a strict arithmetic expression: ");
s = kbd.nextLine().trim();
if (!isSAE(s)) {
System.out.println("ERROR: invalid arithmetic expression");
pause();
break;
}
System.out.printf("The value is %d\n",
valueStrict(s));
pause();
break;
case 4:
System.out.println("\nBuy!\n");
break;
case -1:
System.out.println("*** Invalid Choice");
pause();
break;
}
} while(choice!=4);
}
/**
* This method creates the Menu for the program and adds each of the options
*
* @return the created Menu
*/
private static Menu createMenu() {
Menu m = new Menu("Menu", 8, 50);
m.addOption("Test whether a given string is a palindrome");
m.addOption("Test whether a given string is a strict arithmetic "
+ "expression");
m.addOption("Evaluate a strict arithmetic expression");
m.addOption("Quit");
return m;
}
private static boolean isPal(String input) {
System.out.println("***Method isPal is a stub. It always returns false."
+ "\n***You are expected to implement this method.");
int n=input.length();
if(n==0 || n==1) return true;
if(input.charAt(0)==input.charAt(n-1)){
return isPal(input.substring(1, n-1));
}
return false;
}
public static boolean isSAE(String s) {
int l = s.length();
if (l==0) return false;
if (isNumeric(s)) return true;
if (l<3) return false;
if (s.charAt(0)!='(' || s.charAt(l-1)!=')')
return false;
//Here we know that s is of the form (...)
String s1, s2;
//Next test whether s is of the form (+...) or (-...)
if (s.charAt(1)=='+' || s.charAt(1)=='-') {
s1 = s.substring(2,l-1);
return isSAE(s1);
}
//Here we know that s is not of the form (+...) or (-...)
//Next test whether s is of the form (x+y) or (x*y); that is,
//there is a position in s that contains '+' or '*' and the
//parts of s to the left and right of '+' or '*' are SAEs
for (int i=2; i<l-1; i++) {
if (s.charAt(i)=='+' || s.charAt(i)=='*') {
s1 = s.substring(1, i);
s2 = s.substring(i+1,l-1);
if (isSAE(s1) && isSAE(s2))
return true;
}
}
return false;
}
public static int valueStrict(String s) {
System.out.println("***Method valueStrict is a stub. It always returns "
+ "0.\n***You are expected to implement this method.");
if (!s.contains("+") && !s.contains("-") && !s.contains("*") && !s.contains("/")) {
return Integer.parseInt(s);
}
int i;
for (i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '+' || s.charAt(i) == '-') {
break;
} else if (s.charAt(i) == '*' || s.charAt(i) == '/') {
break;
}
}
String r1 = s.substring(0, i);
String r2 = s.substring(i + 1, s.length());
int result = 0;
switch (s.charAt(i)) {
case '+':
result = valueStrict(r1) + valueStrict(r2);
break;
case '-':
result = valueStrict(r1) - valueStrict(r2);
break;
case '*':
result = valueStrict(r1) * valueStrict(r2);
break;
case '/':
int right = valueStrict(r2);
if (right == 0) //if denominator is zero
{
System.out.println("Invalid divisor");
System.exit(1);
} else {
result = valueStrict(r1) / right;
}
break;
}
return result;
}
}