Question
This program runs 3 different classes with no errors (in eclipse). I need the classes running in their own files, that compile together instead of
This program runs 3 different classes with no errors (in eclipse). I need the classes running in their own files, that compile together instead of being in one file.
The file that should compile would be GUI.java, ExpressionTree.java, Node.java. I am having trouble seperating them into their own files so that the code still works together
package GUI;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUI {
private JButton btnConstruct;
private JTextField tfPostfix;
private JTextField tfInfix;
private JFrame f;
// constructor
GUI() {
init();
}
// a method to initialize and create a frame
private void init() {
// create a frame
f = new JFrame("Three Address Generator");
f.setLayout(new GridLayout(3,1));
// create a panel that contains a label and
// input box for inputting the postfix notation
JPanel p1 = new JPanel(new GridLayout(1,2));
JLabel l1 = new JLabel("Enter Postfix Notation :");
tfPostfix = new JTextField();
p1.add(l1);
p1.add(tfPostfix);
// creat the buttons and text area
btnConstruct = new JButton("Construct Tree");
// create a panel that contains a label and
// output box for outputting the infix notation
JPanel p2 = new JPanel(new GridLayout(1,2));
JLabel l2 = new JLabel("Infix Expression :");
tfInfix = new JTextField();
p2.add(l2);
p2.add(tfInfix);
// adding action listener to the button
btnConstruct.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// read the postfix notation
String postfixExp = tfPostfix.getText().replaceAll(" ", "");
if(postfixExp.isEmpty()) {
// display some error message
// do something
System.out.println("No Postfix Notation is given");
return;
}
ExpressionTree et = new ExpressionTree();
String infixExp = et.constructTree(postfixExp);
if(infixExp.contains("Invalid")) {
JOptionPane.showMessageDialog(null, infixExp, "Error", JOptionPane.ERROR_MESSAGE);
} else {
tfInfix.setText(infixExp);
generateCode(infixExp);
}
}
});
// add components to the container
f.add(p1);
f.add(btnConstruct);
f.add(p2);
f.setSize(400, 200); // set the size of the frame
f.setLocationRelativeTo(null); // display frame in the middle of the screen
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private void generateCode(String infixExp) {
}
public static void main(String... aregs) {
GUI gui = new GUI();
}
}
class ExpressionTree {
PrintWriter p;
Stack
Stack
int operand = 0;
ExpressionTree() {
try {
this.p = new PrintWriter(new BufferedWriter(new FileWriter("_3AddressCode.txt")));
} catch (IOException ex) {
Logger.getLogger(ExpressionTree.class.getName()).log(Level.SEVERE, null, ex);
}
}
class Node {
char value;
Node left, right;
Node(char item) {
value = item;
left = right = null;
}
}
/**
* Method to check whether c is an operator or not
* @param c
* @return
*/
boolean isOperator(char c) {
return c == '+' || c == '-'
|| c == '*' || c == '/'
|| c == '^';
}
/**
* Method to check whether c is an operand or not
* @param c
* @return
*/
private boolean isOperand(char c) {
return c >= 48 && c <= 57;
}
/**
* This method will take the root of a tree(The Expression tree we constructed)
* and returns the array representing the inorder notation of that tree
* by doing the inorder traversal
* @param t
*/
String inorder(Node root) {
if (root != null) {
StringBuilder b = new StringBuilder();
b.append('('); // opening paranth of the expression
b.append(inorder(root.left)); // get the inorder notation of its left child
b.append(root.value);
b.append(inorder(root.right)); // get the inorder notaion of its right child
b.append(')'); // closing paranth of the expression
if(b.length() == 3) { // if there is only one value, i.e, only one operand
return root.value+"";
} else
return b.toString();
} else {
return "";
}
}
/**
* This method, Given a postfix notation,
* returns an Expression tree
* @param postfix
* @return
*/
String constructTree(String postfixExpression) {
Node root, right, left;
// tokenize the given postfix expression
char postfix[] = postfixExpression.toCharArray();
// for each token in the postfix expression
for (int i = 0; i < postfix.length; i++) {
// if the token is an operand
// then push it onto the stack
if (isOperand(postfix[i])) {
root = new Node(postfix[i]);
stack.push(root);
cstack.push(postfix[i]+"");
} else if(isOperator(postfix[i])){ // if it is an operator, then pop the stack twice and make those elements as children of root
// create a node for the root
root = new Node(postfix[i]);
// pop the stack
right = stack.pop();
left = stack.pop();
// make them children of the root
root.right = right;
root.left = left;
generate3AddressCode(postfix[i]);
// Add present root to the stack
stack.push(root);
} else {
// error case
// do something here
System.out.println("Invalid Expression");
p.close();
return "Invalid Token : "+postfix[i];
}
}
// only element will be present in the stack
// and that will be the root of our expression tree
root = stack.pop();
p.close();
// return the inorder traversal of this root
// to generate the desired infix expression
return inorder(root);
}
/**
* Generates the 3 address code
* @param s
*/
private void generate3AddressCode(char s) {
StringBuilder code = new StringBuilder();
code.append(getOperation(s));
code.append(' ');
code.append("R").append(operand); // R0, R1, R2, R3 etc
String right = cstack.pop(); // get the
String left = cstack.pop();
code.append(' ');
code.append(left);
code.append(' ');
code.append(right);
cstack.push("R"+operand);
operand++; // no of symboles incrementing
System.out.println(code.toString());
p.println(code.toString()); // write to file
}
/**
* Returns the Mnemonic for a given operation
* @param s
* @return
*/
private String getOperation(char s){
switch(s) {
case '+' : return "Add";
case '-' : return "Sub";
case '*' : return "Mul";
case '/' : return "Div";
default : return "";
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started