Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need some help coding this Java program. Only need to fill in the missing code I do not have. Thank you! New Skills

Hello, I need some help coding this Java program. Only need to fill in the missing code I do not have. Thank you!

New Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Stacks

Program Description

Class Diagram:

You need to complete the precedence and convertToPostfix methods in the PostFixConversion class.

Instruction:

The PostfixConversion class is a class that has a utility that converts an infix expression to its postfix expression. It does not have any class level (instance) variables. It must have the following two static methods:

public static boolean precedence(char first, char second)

The precedence method determines the precedence between two operators. (here, they are either '+', '-', '*', or '/') If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false. (for instance, if the new operator is '+' or '-', then all operators (+,-,*,/) have precedence greater than or equals to the operator. If the new operator is '*' or '/', then only the operators '*' and '/' have precedence greater than or equals to the operator.) This method will be called from the convertToPostfix method described below.

public static String convertToPostfix(String infixExp)

The convertToPostfix method converts the infix expression (the parameter string for this method) into a postfix expression.

1. If all parentheses are matching in the input infix expression string, then computes its postfix expression, and the method should return the string as:

"The Postfix Expression is: ABC++DHTY++R-*-"

Here, ABC++DHTY++R-*- is an example of postfix expression and the correct one for each input should be displayed instead of it.

2. If there is an open parenthesis that does not have its corresponding close parenthesis, then for the first such character, return a string with the message as:

"There is no matching close parenthesis."

For instance, if the input infix expression string is: (A+(B+C) then for the first open parenthesis, there is no corresponding close parenthesis. In this case, the above message should be returned instead of any post fix expression.

3. If there is a close parenthesis that does not have its corresponding open parenthesis, then for the first such character, return a string with the message as:

"There is no matching open parenthesis."

For instance, if the input infix expression string is: A)+(B+C) then for the first close parenthesis, there is no corresponding open parenthesis. In this case, the above message should be returned instead of any post fix expression.

The rules to convert an infix expression to an equivalent postfix expression are as follows: a. Check/scan each character of a given infix expression (a string) from left to right. (One pass is sufficient.)

b. If the next scanned symbol (character) is an operand (here only alphabet letters - both upper cases and lower cases are used.), append it to the postfix expression.

c. If the next scanned symbol (character) is an open parenthesis '(', then push it onto the stack.

d. If the next scanned symbol (character) is a close parenthesis ')', then pop and append all the symbols from the stack until the most recent matching open parenthesis. i.e., pop and append all the symbols above the open parenthesis that is located in the highest position in the stack. Then pop and discard that open parenthesis at the highest position in the stack.

e. If the next scanned symbol (character) is an operator (here, they are either '+', '-', '*', or '/'),

i). Pop and append to the postfix expression every operator from the stack that is above the most recently scanned open parenthesis, and that has precedence greater than or equals to the new operator (for instance, if the new operator is '+' or '-', then all operators (+,-,*,/) have precedence greater than or equals to the operator. If the new operator is '*' or '/', then only the operators '*' and '/' have precedence greater than or equals to the operator.)

ii). Push the new operator onto the stack.

f. After the infix string is completely processed, pop and append to the postfix string everything from the stack.

In this program, you are to consider the following (binary) arithmetic operators only: +, -, *, and /.

Requirements:

Requirements:

You need to implement this method using an object of the Stack class in java.util package. One way to utilize it is to treat it as a stack of Character objects. The Character class is a wrapper class for the primitive data type "char". If you use the compiler version 5 or above, you can use Auto-boxing for automatic conversion between a primitive data character and an object of the Character class.

To manually convert between them, you can make use of some methods in the Character class such as:

valueOf() method (an example below to convert a character '(' to an object)

Character obj = Character.valueOf('(');

charValue() method (an example below to convert a Character object to a primitive data type character)

char char1 = obj.charValue();

This is the code that I already have that is for sure 100% right, I just need some things added to it

Description: Assignment 11 class displays a menu of choices to a user // and performs the chosen task. It will keep asking a user to // enter the next choice until the choice of 'Q' (Quit) is entered.

import java.io.*;

public class Assignment11 { public static void main (String[] args) throws IOException { char input1; String inputInfo; String line = new String();

printMenu();

InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr);

do // will ask for user input { System.out.println("What action would you like to perform?"); line = stdin.readLine(); input1 = line.charAt(0); input1 = Character.toUpperCase(input1);

if (line.length() == 1) { // matches one of the case statements switch (input1) { case 'E': //Enter String System.out.print("Please enter a string. "); inputInfo = stdin.readLine().trim(); System.out.println(PostFixConversion.convertToPostfix(inputInfo)); break; case 'Q': //Quit break; case '?': //Display Menu printMenu(); break; default: System.out.print("Unknown action "); break; } } else { System.out.print("Unknown action "); } } while (input1 != 'Q' || line.length() != 1); }

/** The method printMenu displays the menu to a user**/ public static void printMenu() { System.out.print("Choice\t\tAction " + "------\t\t------ " + "E\t\tEnter String " + "Q\t\tQuit " + "?\t\tDisplay Help "); } }

import java.util.Stack;

public class PostFixConversion { public static boolean precedence(char first, char second) { /*********************COMPLETE this method**********/ }

public static String convertToPostfix(String infixExp) { String postfixExp = ""; Stack stack1 = new Stack();

/*********************COMPLETE this method**********/ } }

this is an example of what should be inputted and outputted for it to be correct

INPUT

E ((A+B)*C-D-E*(F+G)) E ((A+B)*C-(D-E)*(F+G)) E (A*(B+(C/(D-(E+F))))) E ((A*(B+C))+(G/F-E)-(T*(S-H))) Q 

OUTPUT

Choice Action ------ ------ E Enter String Q Quit ? Display Help What action would you like to perform? Please enter a string. The Postfix Expression is: AB+C*D-EFG+*- What action would you like to perform? Please enter a string. The Postfix Expression is: AB+C*DE-FG+*- What action would you like to perform? Please enter a string. The Postfix Expression is: ABCDEF+-/+* What action would you like to perform? Please enter a string. The Postfix Expression is: ABC+*GF/E-+TSH-*- What action would you like to perform? 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions