The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 314 Weeks Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Attached is the project description (P2GUI.pdf). I have most of it done; however, I need help with this part:
a file should be generated that contains the 3-address format instructions corresponding to the arithmetic expression. These topics is 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 as needed. Each new expression should, however, begin using registers starting at R0.
Attached are my java files with my current code.
package p2gui;
public class Operator extends Node {
  Â
   public Operator(char data) {
       this.value = data;
       this.left = null;
       this.right = null;
   }
  Â
   public Node getLeft() {
       return left;
   }
  Â
   public void setLeft(Node left) {
       this.left = left;
   }
  Â
   public Node getRight() {
       return right;
   }
  Â
   public void setRight(Node right) {
       this.right = right;
   }
  Â
   @Override
   public String toString() {
       return "( "+this.left+" "+value+" "+this.right+" )";
   }
}
package p2gui;
public class Operand extends Node {
  Â
   public Operand(char data) {
       this.value = data;
       this.left = null;
       this.right = null;
   }
  Â
   @Override
   public String toString() {
       return value + "";
   }  Â
}
package p2gui;
public abstract class Node {
  Â
   protected char value;
   protected Node left;
   protected Node right;
  Â
   public char getValue() {
       return value;
   }
  Â
   public void setValue(char value) {
       this.value = value;
   }
}
package p2gui;
public class Tree {
  Â
   // starting point to construct the tree
   private Node root;
   private boolean added = false;
  Â
   // constructs tree
   public Tree (String data) throws Exception {
       // takes the length of the data (-1) and decrements until reaching zero
       for (int i = data.length()-1; i >= 0; i--) {
           added = false;
           // stores values into root; calls addNode() method
           root = addNode(root, data.charAt(i));
       }
   }
  Â
   // method to add a node to either the left child or right child
   private Node addNode(Node node, char data) throws Exception {
       if(node != null) {
           // first checks to see if the node is an Operator object
           if(node instanceof Operator) {
               // if the right node is null and an Operator, then we will add it
               if(node.right == null || node.right instanceof Operator) {
                   node.right = addNode(node.right, data);
               // else if the node is an Operand we will add the Operand to the left side
               } else if (!added && node.right != null && node.right instanceof Operand) {
                   if(node.left == null || node.left instanceof Operand) {
                       node.left = addNode(node.left, data);
                   }
               }
               // or if true AND the left node is null OR the left node is an Operator, then add it to the left
               if(!added && node.left == null || node.left instanceof Operator) {
                   node.left = addNode(node.left, data);
               }
           }
       } else {
           added = true;
           // returns the data to proper object; Operator or Operand
           if(isOperator(data)) {
               return new Operator(data);
           } else {
              return new Operand(data);
           }              Â
       }
       return node;
   }
  Â
   // method to check if the user entered a proper token, if not let them know
   private boolean isOperator(char c) throws Exception {
       if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
           return true;
       if(c >= '0' && c <= '9')
           return false;
       throw new Exception("Invalid Token " + c);
   }
  Â
   @Override
   public String toString() {
       return root.toString();
   }  Â
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly