You are given the main class RecursiveMethods.java which invokes methods that can be selected from a...

60.1K

Verified Solution

Question

Programming

You are given the main class RecursiveMethods.java which invokesmethods that can be selected from a menu. Your task is to implementtwo of these methods. -The first method isPal(s) determines whethera given string s is a palindrome. For example \"12321\" is apalindrome but \"1231\" is not. Currently the method isPal is a stuband you are expected to implement the method. -The second methodisSAE(s) determines whether the given string s is a strictarithmetic expression (see below). This method is fully implementedfor you. -The third method valueStrict(s) computes the value of thestrict arithmetic expression s. Currently the method valueStrict isa stub and you are expected to implement the method using as aguide the implementation of isSAE(s). The methods should beimplemented using recursive methods, no points will be given foriterative solutions. You should only need to work with the fileRecursiveMethods.java but you will include your util1228 packagefor obvious reasons. Sample execution is shown below. SUBMISSIONNOTES You will submit a single .jar file named A04.jar. Make surethat: 1) The jar file runs. In project properties under Run makesure the main class is set to a04.RecursiveMethods 2) The jar fileincludes all your source code. In project properties underpackaging make sure .java files are not excluded. (see ourBrightspace site for details about making JAR files) IMPORTANTNOTES A strict arithmetic expression (SAE) is one of the following:1. Any nonnegative integer is a SAE (that is a nonempty string madeof digits) 2. (x+y), (x*y), (-x), (+x), if x,y are SAEs Strictarithmetic expressions do not allow spaces and do not allowomitting parentheses. The following are valid SAEs: 12, (-12),(3*5), ((3*5)+10), ((-3)*((-5)+10)) The following are NOT validSAEs: -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 givenstring is a strict arithmetic expression 3. Evaluate a strictarithmetic expression 4. Quit Enter your choice here: 1 Enter astring: 1234321 \"1234321\" is a palindrome Press enter tocontinue... 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 choicehere: 3 Enter a strict arithmetic expression: ((3*5)+10) The valueis 25 Press enter to continue... Menu 1. Test whether a givenstring is a palindrome 2. Test whether a given string is a strictarithmetic expression 3. Evaluate a strict arithmetic expression 4.Quit Enter your choice here: 3 Enter a strict arithmeticexpression: (-3)*5 ERROR: invalid arithmetic expression Press enterto continue... Menu 1. Test whether a given string is a palindrome2. Test whether a given string is a strict arithmetic expression 3.Evaluate a strict arithmetic expression 4. Quit Enter your choicehere: 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

Answer & Explanation Solved by verified expert
4.3 Ratings (601 Votes)
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 javautil class RecursiveMethods public static void    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students