1. Specification Design, write and test a program that accepts an arithmetic exp
ID: 3880302 • Letter: 1
Question
1. Specification Design, write and test a program that accepts an arithmetic expression of unsigned integers in postfix notation in which the tokens are separated by spaces and builds and processes the arithmetic expression tree that represents that expression. The main class will be P2GUI. It should create a Swing based GUI shown below:
The GUI must be generated by code that you write. You may not use a drag-and-drop GUI generator. Pressing the Construct Tree button should cause the following: (i) the arithmetic expression tree that represents the entered postfix expression will be constructed, (ii) using that tree, the corresponding fully parenthesized infix expression should be generated and displayed in the GUI and finally (iii) a file should be generated that contains the 3-address format instructions corresponding to the arithmetic expression. These topics, including relevant classes for implementing and processing an expression tree, are discussed in the week 4 reading “Binary Trees, Expression Trees, BST (AVL) trees, and B-Trees” section II “Expression Trees”. The above example should produce the following output file containing the 3-address instructions: Add R0 5 9 Sub R1 3 R0 Mul R2 2 3 Div R3 R1 R2 It is not necessary to reuse registers within an expression as shown in the above mentioned reading and you can assume there are as many available registers as needed. Each new expression should, however, begin using registers starting at R0. You may assume that the expression is syntactically correct with regard to the order of operators and operands, but you should check for invalid tokens, such as characters that are not valid operators or operands such as 2a, which are not valid integers. If an invalid token is detected, a checked custom exception InvalidTokenException should be thrown and caught by the main class and an appropriate error message should be displayed in an JOptionPane. Below is an example:
Your program should compile without errors. The Google recommended Java style guide (https://google.github.io/styleguide/javaguide.html) should be used to format and document your code. Specifically, the following style guide attributes should be addressed: header comments include filename, author, date and brief purpose of the program; In-line comments used to describe major functionality of the code; the meaning and the role of variables and constants are indicated as code comments; meaningful variable names and prompts applied; class names are written in UpperCamelCase; variable names are written in lowerCamelCase; constant names are in written in All Capitals; braces use K&R style. In addition the following design constraints should be followed: declare all instance variables private; avoid the duplication of code.
Three Adddress Generator Enter Postfix Expression 359+ 23* onstruct Tree Infix Expression ((3-(5+9))/(2*3))Explanation / Answer
// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.util.*;
// Java extension packages
import javax.swing.*;
class InfixToPostfixConverter extends JFrame
{
private JLabel prompt, postfixLabel;
private JTextField infixField, postfixField;
// set up GUI
public InfixToPostfixConverter()
{
prompt = new JLabel("Enter infix arithmetic expression: ");
infixField = new JTextField( 20 );
infixField.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
// call convertToPostfix
StringBuffer input = new StringBuffer(
infixField.getText() );
postfixField.setEnabled( true );
postfixField.setText( convertToPostfix(
input ).toString() );
postfixField.setEditable( false );
}
}
);
postfixLabel = new JLabel("Expression in postfix notation: " );
postfixField = new JTextField( 20 );
postfixField.setEnabled( false );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( prompt );
container.add( infixField );
container.add( postfixLabel );
container.add( postfixField );
setSize( 260, 150 );
show();
}
// convert expression
public StringBuffer convertToPostfix( StringBuffer infix )
{
StringBuffer postfix = new StringBuffer();
Stack stack = new Stack();
stack.push( new Character( '(' ) );
infix.append( ")" );
int index = 0;
// convert expression
while( !stack.isEmpty() ) {
char temp = infix.charAt( index );
// digits
if ( Character.isDigit( temp ) )
postfix.append( temp );
// left parenthesis
else if ( temp == '(' )
stack.push( new Character( temp ) );
// operators
else if ( isOperator( temp ) ) {
char top =
( ( Character ) stack.peek() ).charValue();
while ( isOperator( top ) &&
!precedence( top, temp ) ) {
postfix.append(
( ( Character) stack.pop() ).charValue() );
top = ( ( Character ) stack.peek() ).charValue();
}
stack.push( new Character( temp ) );
}
// right parenthesis
else {
while ( ( ( Character ) stack.peek() ).charValue()
!= '(' )
postfix.append(
( ( Character ) stack.pop() ).charValue() );
stack.pop();
}
index++;
}
return postfix;
} // end method convertToPostfix
// determine if c is an operator
public boolean isOperator( char c )
{
if ( c == '+' || c == '-' || c == '*' ||
c == '/' || c == '^' || c == '%' )
return true;
return false;
}
// return true if operator1 has
// lower precedence than operator2
public boolean precedence( char operator1, char operator2 )
{
if ( ( operator1 == '+' || operator1 == '-' ) &&
operator2 != '+' && operator2 != '-' )
return true;
else if ( operator1 != '^' && operator2 == '^' )
return true;
return false;
}
// execute application
public static void main( String args[] )
{
P2GUI application =
new P2GUI();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
} // end class InfixToPostfixConverter
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.