Question: Can you complete this code for me please below is the requirement. Create a stack based program StackMath that accepts a user's infix or postfix

Can you complete this code for me please below is the requirement.
Create a stack based program StackMath that accepts a user's infix or postfix expression string and displays it in the other notation along with an evaluation of this expression (hint: see textbook sections 5.16,5.18 and 5.21). Also necessary is StackInterface.
Your program will contain the following methods, which may be in separate classes:
infix2postfix converts infix to postfix
postfix2infix converts postfix to infix
evaluatePostfix evaluates the postfix expression (using a stack)
evaluateInfix evaluates the postfix expression (using a stack)
Have your program allow the user to optionally use the classes:
LinkedStack, as outlined in Listing 6-1
ArrayStack, as outlined in Listing 6-2
Each of the above classes include these ancillary methods, at least:
pop
push
peek
isEmpty
clear
Within LinkedStack's comments, explain if toArray is should be included. Include it if so.
Within ArrayStack's comments, explain if toArray is should be included. Include it if so.
Program displays correct infix expression of user's postfix expression.
Program displays correct postfix expression of user's infix expression.
Program properly evaluates user's infix expression, displaying the correct result.
Program properly evaluates user's postfix expression, displaying the correct result.
Here is my code which I almost finish it.
import java.util.Scanner;
public class StackMath {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an expression (infix or postfix):");
String expression = scanner.nextLine();
StackInterface stack;
System.out.println("Choose stack implementation: (1) LinkedStack (2) ArrayStack");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (choice ==1){
stack = new LinkedStack<>();
} else if (choice ==2){
stack = new ArrayStack<>();
} else {
System.out.println("Invalid choice. Exiting program.");
return;
}
if (isInfix(expression)){
String postfix = infixToPostfix(expression);
System.out.println("Postfix expression: "+ postfix);
System.out.println("Evaluation: "+ evaluatePostfix(postfix, stack));
} else {
String infix = postfixToInfix(expression);
System.out.println("Infix expression: "+ infix);
System.out.println("Evaluation: "+ evaluateInfix(infix, stack));
}
}
public static boolean isInfix(String expression){
// Simple heuristic to determine if the expression is infix or postfix
return expression.contains("+")|| expression.contains("-")|| expression.contains("*")|| expression.contains("/");
}
public static String infixToPostfix(String infix){
// Implement infix to postfix conversion
// You can use the StackInterface here
return "";
}
public static String postfixToInfix(String postfix){
// Implement postfix to infix conversion
// You can use the StackInterface here
return "";
}
public static int evaluatePostfix(String postfix, StackInterface stack){
// Implement postfix expression evaluation
// You can use the StackInterface here
return 0;
}
public static int evaluateInfix(String infix, StackInterface stack){
// Implement infix expression evaluation
// You can use the StackInterface here
return 0;
}
}
interface StackInterface {
void push(T element);
T pop();
T peek();
boolean isEmpty();
void clear();
}
class LinkedStack implements StackInterface {
// Implementation of LinkedStack
}
class ArrayStack implements StackInterface {
// Implementation of ArrayStack
}
just do the implementations and fix if there mistakes thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!