Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part A - Balanced Parentheses We that a stack can convert infix to postfix, but a queue? If not, then there is nothing to do

Part A - Balanced Parentheses

We that a stack can convert infix to postfix, but a queue? If not, then there is nothing to do for this lab, so I guess the answer is yes. Before beginning, study and add the commenting to the imported Java code. The queue code is a variation of the reference-based queue from our lecture and book. I will test your program with various expressions by typing them in your main method in place of the example expression. You do not need a prompt.

Open the Expression class and study the code. This class will check an expression for balanced parentheses and can convert an expression from infix to postfix. It needs a private RQueue data member to use in the methods, and the constructor will instantiate the RQueue data member.

The checkbalance method brings in the expression to check for balance parentheses. The expression is a string processed one character at a time (hint: charAt) using the queue data member. If it passes the check, the method returns true; otherwise false. (Hint: Be carefulthink about using the QueueException to your advantage.) Remember that the queue is FIFO with enqueue, dequeue, and peek operations.

You should be able to test it using the following line in the main method. Remember to try it with different expressions with different parentheses, including some that are not balanced.

System.out.println(converter.checkbalance(infix));

Part B - Converting Infix to Postfix

The precedence method determines what precedence to use (remember PEMDAS). The higher the precedence, the smaller the integer we return. In our case, we will keep this simple by only working with multiplication, division, addition, and subtraction in our expressions. Specifically, multiplication or division has higher precedence, so it returns a smaller integer than addition or subtraction. (Hint: remember that multiplication and division have the same precedence, and addition and subtraction have the same precedence.)

The isOperand method returns true if the character is an operand; false otherwise. Our operands can be upper-case or lower-case alphabetic characters (A to Z, a to z) and single digits (0 to 9). Numbers in the expression will be single digits.

The infixToPostfix method converts the infix expression to postfix using the queue data member. As you process the string one character at a time (hint: charAt), you apply the rules we learned previously; however, you must adjust them for the FIFO queue instead. It concatenates the characters to the postfix string and returns the final postfix string.

Now in the main method, set it up to first check if the parentheses are balanced, and if so, convert it to postfix and display it to the console. For example, suppose the expression isx*(y/(5*z))+(2), then the results will be:

true

xy5z*/*2+

Test your program with different expressions to make sure it works. Expressions can be any String length, including zero, and contain characters A to Z, a to z, 0 to 9, *, /, +, -, ( and ). You can assume that the only error in an infix expression would be misbalanced parentheses.

Here is all the classes i had been provided below

package driver;

public class Driver {

public static void main(String[] args) { Expressions converter = new Expressions(); String infix = "x*(y/(5*z))+(2)";

} }

package driver;

public class Expressions {

public Expressions() { } public boolean checkbalance(String expression) { return false; } public int precedence(char c) { return 0; } public boolean isOperand(char c) { return false; } public String infixToPostfix(String infix) { String postfix = ""; return postfix; } }

package queue;

public class Node {

protected Object item; protected Node next;

Node(Object item) { this.item = item; this.next = null; }

Node(Object item, Node next) { this.item = item; this.next = next; }

}

package queue;

public class QueueException extends RuntimeException {

private static final long serialVersionUID = 8513121132880862866L;

public QueueException(String s) { super(s); } }

package queue;

public class RQueue {

private Node back;

public RQueue() { back = null; }

public boolean isEmpty() { return back == null; }

public void enqueue(Object item) { Node newNode = new Node(item);

if(isEmpty()) { newNode.next = newNode; } else { newNode.next = back.next; back.next = newNode; } back = newNode; }

public Object dequeue() throws QueueException { if(!isEmpty()) { Node front = back.next; if(front == back) { back = null; } else { back.next = front.next; } return front.item; } else { throw new QueueException("QueueException on dequeue: queue empty"); } }

public void dequeueAll() { back = null; }

public Object peek() throws QueueException { if (!isEmpty()) { return back.next.item; } else { throw new QueueException("Queue exception on peek queue empty"); } } public void displayQueue() { System.out.print("f "); if(back != null) { Node temp = back.next; while(temp != back) { System.out.print(temp.item + " "); temp = temp.next; } System.out.print(temp.item + " "); } System.out.println("b"); }

}

DO NOT COPY ANSWER FROM ANYWHERE ELSE MUST BE YOUR OWN WORK WITH A DETAILED EXPLANATION TOWARDS THE END

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

Recommended Textbook for

Data Analysis And Decision Making

Authors: Christian Albright, Wayne Winston, Christopher Zappe

4th Edition

538476125, 978-0538476126

Students also viewed these Programming questions

Question

What does SMART stand for? (p. 86)

Answered: 1 week ago