Question

Using Java programming: Modify the delimiter class (provided below) so that it can "incorrectly" ...

Using Java programming:

Modify the delimiter class (provided below) so that it can "incorrectly" evaluate simple parenthesized math expressions

.

a) ask the user for the whole expression to solve

b) Solve the expression only if there is no mismatch in the delimiters. If there is a mismatch, then output an error. If there is an error, allow the user to reenter a corrected/new expression.

c) The program should be able to evaluate expressions that uses only the following basic operations: +, - , *, and % (known as modulus)

d) The program should also work for single digit integers, along with multi-digit integers and/or double/floats

by the usage of "incorrectly", when reading a + operator, evaluate it as % (modulus). When a * is read, evaluate it as - (minus).

.

------------------------------------------

notes:

i) each operation should be explicitly written out using the basic operations, Instead of 4(2+9), it should be 4*(2+9)

ii) If the program encounters an invalid or implicit operator, then print a short but descriptive error. If there is an invalid or implicit operator, allow the user to fix the mistake or reenter the expression

-----------------------------------------

DELIMITER CLASS

.

.

import java.util.Scanner;
import java.util.Stack;
public class Delimiter {
   public static void main(String args[]) {
       Scanner in = new Scanner (System.in);
       String expression;
       System.out.print("Enter expression : ");
       expression = in.nextLine();
       if(check(expression))
           System.out.print(expression + " has no mismatch in the delimiters ");
       else
           System.out.print(expression + " has mismatch in the delimiters ");
    
       in.close();
   }

   public static boolean check(String expression) {
       Stack<Character> stack=new Stack<Character>();

for(int i=0;i<expression.length();i++)
{
char c=expression.charAt(i);
if (c == '{' || c == '(' || c == '[')
stack.push(c);

if (c == '}' || c == ')' || c == ']')
{
if (stack.isEmpty())
return false;


else if ( !isMatchingPair(stack.pop(), c) ) return false;
}
}
if (stack.isEmpty()) return true;
return false;
}
   public static boolean isMatchingPair(char character1, char character2)
   {
   if (character1 == '(' && character2 == ')')
   return true;
   else if (character1 == '{' && character2 == '}')
   return true;
   else if (character1 == '[' && character2 == ']')
   return true;
   else
   return false;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(e data Solution This code i^on( no OpCrcdors ohaue corste a code -that e atch Hexe P tho cutput mekS7 Sptemaut Print (Entert beoleam Chec Conttnue st- push (sb to shingo st push (h to string) r cohon me ot), pop and. talclateShing top ב St. Popes ele gt adi (or elem) Corvect lahe Yelun Cenrect Stahe boolean 1ろopcr velturn true

Add a comment
Know the answer?
Add Answer to:
Using Java programming: Modify the delimiter class (provided below) so that it can "incorrectly" ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • Im try to create a java program that checks to see if a given boolean expression...

    Im try to create a java program that checks to see if a given boolean expression is a tautology or not my code so far is as follows: public static class TreeNode    {        char data;        TreeNode left;        TreeNode right;               TreeNode(char item)        {            data = item;            left = null;            right = null;        }    } public static...

  • Pseudocode for main: Get a string from user If string has correctly matched delimiters print "All...

    Pseudocode for main: Get a string from user If string has correctly matched delimiters print "All matched correctly Pseudocode for FSM: Note: This is to be implemented as a subprogram that returns a boolean value of true or false indicating whether or not all delimiters matched correctly. State O (Starting State): .Read next character in input string . If char is open delimiter, next state is 1 lf char is close delimiter, next state is 2 . If char is...

  • Java Programming Language Edit and modify from the given code Perform the exact same logic, except...

    Java Programming Language Edit and modify from the given code Perform the exact same logic, except . . . The numeric ranges and corresponding letter grade will be stored in a file. Need error checking for: Being able to open the text file. The data makes sense: 1 text line of data would be 100 = A. Read in all of the numeric grades and letter grades and stored them into an array or arraylist. Then do the same logic....

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

  • Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)...

    Evaluateeg EvaluateInFix.java: import java.util.Stack; import java.util.Scanner; public class EvaluateInfix {    public static void main(String[] args)    {    Scanner keyboard = new Scanner(System.in); System.out.println("This program an inFix Expression: "); System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): "); String aString = keyboard.nextLine();    while (!aString.equals("!")) {    System.out.println (evaluateinFix(aString));    System.out.print("Enter an inFix Expression that you want to evaluate (or enter ! to exit): ");    aString = keyboard.nextLine(); } // end while...

  • Read Section 6.1.5 (p.235) on the Parenthesis Matching problem. They gave 5 examples: the first t...

    Please help with this. The hint refers to the attached picture. Read Section 6.1.5 (p.235) on the Parenthesis Matching problem. They gave 5 examples: the first two are " correct", and the remaining 3 "incorrect". Notice that the last one is actually "fixable". You just have to append the string")" to the input to get a properly matched expression! Here is another example: the string (O[(is incorrect, but is fixable if you append )]) Thus we want to classify the...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT