SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 314 Weeks Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 20 Nov 2017 My Price 10.00

project description (P2GUI.pdf). I have most of it done;

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();
    }   
}




Answers

(5)
Status NEW Posted 20 Nov 2017 01:11 PM My Price 10.00

-----------  ----------- 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

Not Rated(0)