Question

(JAVA) I have "cannot be resolved or is not a field" error on node.right = y;...

(JAVA)

I have "cannot be resolved or is not a field" error on

node.right = y; and node.left = x;

//BuildExpressionTree.java

import java.util.*;
import javax.swing.*;
import javax.xml.soap.Node;

public class BuildExpressionTree {
   private Stack stack = new Stack();
   private Node node;
  
   public static boolean isOperator(String token){
       if(token == "+" || token == "-" || token == "*" || token == "/"){
           return true;
       }
       JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token Error", JOptionPane.WARNING_MESSAGE);
       return false;
   }
  
   public static boolean isInteger(String token){
       try{
           if(Integer.parseInt(token) == 0 ||
           Integer.parseInt(token) == 1 ||
           Integer.parseInt(token) == 2 ||
           Integer.parseInt(token) == 3 ||
           Integer.parseInt(token) == 4 ||
           Integer.parseInt(token) == 5 ||
           Integer.parseInt(token) == 6 ||
           Integer.parseInt(token) == 7 ||
           Integer.parseInt(token) == 8 ||
           Integer.parseInt(token) == 9){
           return true;
           }
       }catch(NumberFormatException nfe){
           JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token Error", JOptionPane.WARNING_MESSAGE);
           return false;
       }
       return true;
   }
  
   public String[] postfixExpression(String postfixExp){
       String expression = postfixExp;
       String[] expressionArray = expression.split("\\s+");
       String splitExpression = "";
      
       for(int i = 0; i < expressionArray.length; i++){
           splitExpression += expressionArray[i];
       }
       StringTokenizer tokenizedExpression = new StringTokenizer(splitExpression);
       splitExpression = "";
      
       while(tokenizedExpression.hasMoreTokens()){
           String nextToken = tokenizedExpression.nextToken();
           splitExpression += nextToken + " ";
       }String[] tokens = splitExpression.split("\\s+");
       return tokens;
   }
  
   public String constructTree(String expression) throws RuntimeException{
       stack = new Stack();
       String result;
       String[] expressionRead = postfixExpression(expression);
       for(int i = 0; i < expressionRead.length; i++){
           String nextToken = expressionRead[i];
           if(isInteger(nextToken)){
               node = (Node) new OperandNode(nextToken);
               stack.push(node);
           }else if(isOperator(nextToken)){
               node = (Node) new OperatorNode(nextToken);
               Node y;
               Node x;
              
               y = (Node) stack.pop();
               x = (Node) stack.pop();
              
               node.right = y;
               node.left = x;

               stack.push(node);
           }else{
               throw new RuntimeException();
           }
       }
       result = stack.peek().toString();
       return result;
   }
}

//Node.java


public abstract class Node {
   protected String data;
   protected Node left;
   protected Node right;
  
   public void setData(String data){
       this.data = data;
   }
  
   public String getData(){
       return data;
   }
}

class OperandNode extends Node{
   public OperandNode(String operand){
       this.data = operand;
       this.left = null;
       this.right = null;
   }
   public String toString(){
       return data + "";
   }
}

class OperatorNode extends Node{
   public OperatorNode(String operator){
       this.data = operator;
       this.left = null;
       this.right = null;
   }
  
   public void setLeft(Node left){
       this.left = left;
   }
   public Node getLeft(){
       return left;
   }
   public void setRight(Node right){
       this.right = right;
   }
   public Node getRight(){
       return right;
   }
   public String toString(){
       return "( " + this.left + " " + data + this.right + " )";
   }
}

Homework Answers

Answer #1

/**
*
* @author Rashmi Tiwari
*/

import java.util.*;
import javax.swing.*;
import javax.xml.soap.Node;
public class BuildExpressionTree {
private Stack stack = new Stack();
private Node node;
  
public static boolean isOperator(String token){
if(token == "+" || token == "-" || token == "*" || token == "/"){
return true;
}
JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token Error", JOptionPane.WARNING_MESSAGE);
return false;
}
  
public static boolean isInteger(String token){
try{
if(Integer.parseInt(token) == 0 ||
Integer.parseInt(token) == 1 ||
Integer.parseInt(token) == 2 ||
Integer.parseInt(token) == 3 ||
Integer.parseInt(token) == 4 ||
Integer.parseInt(token) == 5 ||
Integer.parseInt(token) == 6 ||
Integer.parseInt(token) == 7 ||
Integer.parseInt(token) == 8 ||
Integer.parseInt(token) == 9){
return true;
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token Error", JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
  
public String[] postfixExpression(String postfixExp){
String expression = postfixExp;
String[] expressionArray = expression.split("\\s+");
String splitExpression = "";
  
for(int i = 0; i < expressionArray.length; i++){
splitExpression += expressionArray[i];
}
StringTokenizer tokenizedExpression = new StringTokenizer(splitExpression);
splitExpression = "";
  
while(tokenizedExpression.hasMoreTokens()){
String nextToken = tokenizedExpression.nextToken();
splitExpression += nextToken + " ";
}String[] tokens = splitExpression.split("\\s+");
return tokens;
}
  
public String constructTree(String expression) throws RuntimeException{
stack = new Stack();
String result;
String[] expressionRead = postfixExpression(expression);
for(int i = 0; i < expressionRead.length; i++){
String nextToken = expressionRead[i];
if(isInteger(nextToken)){
node = (Node) new OperandNode(nextToken);
stack.push(node);
}else if(isOperator(nextToken)){
node = (Node) new OperatorNode(nextToken);
Node y;
Node x;
  
y = (Node)stack.peek();
stack.pop();
x = (Node)stack.peek();
stack.pop();

node.right = y;

node.left = x;   
stack.push(node);
}else{
throw new RuntimeException();
}
}
result = stack.peek().toString();
return result;
}
}
//Node.java

public abstract class Node {
protected String data;
protected Node left;
protected Node right;
  
public void setData(String data){
this.data = data;
}
  
public String getData(){
return data;
}
}
class OperandNode extends Node{
public OperandNode(String operand){
this.data = operand;
this.left = null;
this.right = null;
}
public String toString(){
return data + "";
}
}
class OperatorNode extends Node{
public OperatorNode(String operator){
this.data = operator;
this.left = null;
this.right = null;
}
  
public void setLeft(Node left){
this.left = left;
}
public Node getLeft(){
return left;
}
public void setRight(Node right){
this.right = right;
}
public Node getRight(){
return right;
}
public String toString(){
return "( " + this.left + " " + data + this.right + " )";
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...