Implementing Methods for a Calculator
Hey all. I'm new to java and have very little experience with it. I know the basic structures of it but not much more. I just transferred schools and was using Python in my last class. I was given this project and have no clue how to implement the methods asked. I will list the methods I'm supposed to implement and then list what code I have. I apologize ahead of time for the length, but believe all the code is necessary to see how the program runs.

1) This calculator does not support %, the modulo operator. Please modify evalTerm so that when the user types 10%4*6, 12.0 will be printed. 

2)The current implementation does not enforce the definition of variables completely. For instance, x$ or $x can be variable names. Please write the following method 
      static boolean isVariable(String id) 

which returns true if and only if id is a variable by the above definition. (a variable is a element that starts with a $ or letter and followed by a mix of letters and digits) Please use this method in getToken (or getNextToken) of the class EvalTokenizer. If an identifier does not qualify for being a variable, throw an exception of ArithmeticException("bad variable name");

3)This calculator uses the class Memory to maintain the variables and the results of each line. This class is implemented using a "wrapper" class of mapping. Please provide another implementation of Memory using the Binary Search Tree. That is, you are aksed to implement the three methods of Memory using Binary Search Tree: 
    public Memory(); // Constructor of Memory
    
    public void store(String name, double value); 
         // insert the pair  into the binary search tree, where name is the key 
         // and value is its value. 
         // If name is already in the binary search tree, only the value is updated (no duplicated keys).
     
    public double fetch(String name);
        // check if name is in the binary search tree; if not, return 0.0;
        // otherwise, return the value associated with name.

4) We want this calculator to use most functions defined in the java Math class. The Math functions can be added into the calculator by extending the definition of factor 
<factor> ::= <unary func> ( <expression> )
           | <binary func> ( <expression> , <expression> )
<unary func> ::= abs | sqrt | exp | log 
<binary func> ::= min | max | pow

Please modify evalFactor according to the above rules. 

5) Boolean expressions can also be defined and evaluated similarly. For instance, we may extend the definition of statement to include boolean expressions as follows: 
<statement> ::= <variable> = <expression> 
              | <expression>
              | test <boolExpr>
<boolExpr> ::= <boolTerm>
               | <boolExpr> "|" <boolTerm>
<boolTerm> ::= <boolFactor>
         | <boolTerm> & <boolFactor>
<boolFactor> ::= ( <boolExpr> )
           | ! <boolFactor>
           | <expression> <compare> <expression> 
           | true 
           | false
<compare> ::= < | > | >= | <=

Please write an evaluator for the boolean expressions as defined above. 
To test your program, we like to see how the following expressions are evaluated: 

x = log(2010%4*100)
y = sqrt(abs(150-x))+sqrt(x)
test x - y >= min(x, y)
test (x < y) | (y > 0) & (log(y) > 0)

Here is the code I have to start...


public class Token {
        // Data:
        private String type = "";
        private String name = "";
        private double value = 0;
        
        // Constructors:
        public Token() { this( "" ); }
        public Token( String t ) { this( t, 0 ); }
        public Token( String t, double v ) { this( t, t, v ); }
        public Token( String t, String n, double v ) { 
         type = t; name = n; value = v; 
        }

        // Methods:
        public String getType( ) { return type;}
        public String getName( ) { return name;}
        public double getValue( ) { return value; }
        public String toString( ) { return (" type="+type+" name="+name+" value="+value);}
}   

import java.util.*;

public class Memory {

    private HashMap<String, Double> map;
    
    /** Creates new Memory */
    public Memory() {
        map = new HashMap<String, Double>();
    }
    
    public void store(String name, double value) {
        map.put(name, new Double(value));
    }
    
    public double fetch(String name) {
        Double x = (Double)map.get(name);
        if (x == null) return 0;
        return x.doubleValue();
    } 
}


public class Evaluator {
 private double value;
 private static EvalTokenizer str; 
 private static Memory mem = new Memory();
 
 public Evaluator( String s ) {
     str = new EvalTokenizer( s );
     value = evaluate();    
 }
 
 public double getValue() {
  return value;
 }
 public void storeCurrentValue(int line) {
  mem.store("$"+line, value);
 }
 private double evaluate() {
  return evalStatement(str.getToken());
 }
 
 private double evalStatement(Token firstToken) {
  /* <statement> ::= <var> = <expression> 
                   | <expression> 
                   */
  String s = firstToken.getType();
  // if (s.equals("goto")) return 0;
  if (s.equals("_id") && (str.peekToken().getType()).equals("=")) {
   // <statement> ::= <var> = <expression> 
   str.getToken();
   double x = evalExpression(str.getToken());
   mem.store(firstToken.getName(), x);
   return x;
  }
  return evalExpression(firstToken);
 }
 
 private double evalExpression(Token firstToken) {
  /* <expression> ::= <term> 
                    | <expression> + <term>
                    | <expression> - <term>
        */
  double x = evalTerm(firstToken);
  while (true) {
   String op = str.peekToken().getType();
   if (op.equals("+") || op.equals("-")) {
    str.getToken(); // skip "+" or "-"
    double y = evalTerm(str.getToken());
    if (op.equals("+")) x += y;
    else x -= y;
   } else
    return x;
  }
 }
 
 private double evalTerm(Token firstToken) {
  /* <term> ::= <factor>
              | <term> * <factor> 
              | <term> / <factor> 
        */
  double x = evalFactor(firstToken);
  while (true) {
   String op = str.peekToken().getType();
   if (op.equals("*") || op.equals("/") || op.equals("%")) {
    str.getToken(); // skip "*" or "/"
    double y = evalFactor(str.getToken());
    if (op.equals("*")) x = x*y;
    if (op.equals("/")) x= x/y;
    else x = x%y;
   } else return x;
  
  
  }
 }
 
 private double evalFactor(Token firstToken) {
  /* <factor> ::= ( <expression> )
              | + <factor>
              | - <factor>
              | <variable>  
              | ++ <variable>  
              | -- <variable>
              | <number> 
        */
  String t = firstToken.getType();
  
  if (t.equals("(")) { 
   // <factor> ::= ( <expression> )
   double x = evalExpression(str.getToken());
   String nex = str.getToken().getType();
   if (!nex.equals(")")) throw new ArithmeticException("bad expression" );
   return x;
  }
  
  // <factor> ::= + <factor> | - <factor>
  if (t.equals("+")) return (evalFactor(str.getToken()));
  if (t.equals("-")) return (-evalFactor(str.getToken()));
  
  // <factor> ::= <number> 
  if (t.equals("_num")) return firstToken.getValue(); 

  // <factor> ::= <variable> 
  if (t.equals("_id")) return mem.fetch(firstToken.getName());
  
  if (t.equals("++") || t.equals("--")) { 
   // <factor> ::= ++ <variable> | -- <variable> 
   firstToken = str.getToken();
   if ((firstToken.getType()).equals("_id")) {
    double x = mem.fetch(firstToken.getName());
    if (t.equals("++")) {
     mem.store(firstToken.getName(), x+1);
     return x+1;
    } else {
     mem.store(firstToken.getName(), x-1);
     return x-1;
    }
   }
  }
  
  throw new ArithmeticException("bad expression" );
 }
}

import java.util.StringTokenizer;

public class EvalTokenizer {
    // Data:
    private StringTokenizer str;
    private Token nextToken = new Token();
    
    public EvalTokenizer( String line ) { 
     str = new StringTokenizer(line, "+*-/().<>!=%,|& ", true);
     getToken();
    }
    
    public Token peekToken() { return nextToken; }
    
    /**
     * A token is either a number literal (integer or double), an identifier, or special symbols 
     * getToken finds the next token, skipping blanks, and return it.
     * For _int or _double token, place the processed value in value.
     * For _id, place the name of the identifier in name.
     * Throw ArithmeticException if input is unrecognized.
     */
    public Token getToken( ) throws ArithmeticException {
     
      static boolean isVariable(String id) {
      
      }
        Token result = nextToken;
        nextToken = getNextToken();
        String t1 = result.getType();
        String t2 = nextToken.getType();
          
        // handle floating numbers
        if (t1.equals("_num") && t2.equals(".")) {
         nextToken = getNextToken();
         t2 = nextToken.getType();
         if (!t2.equals("_num")) throw new ArithmeticException("number format error" );
         t2 = nextToken.getName();
         double x = result.getValue()+nextToken.getValue()*Math.pow(10.0, -t2.length());
         nextToken = getNextToken();
         return new Token("_num", "", x);
        }
        
        if (t1.equals("$")) {
         if (t2.equals("_int")) {       
          double x = nextToken.getValue();
          nextToken = getNextToken();
          return new Token("_dollar", x);
         } else 
          throw new ArithmeticException("dollar variable format error" );
        }
        
        if (t1.equals("+")) {
         if (t2.equals("+")) { nextToken = getNextToken(); return new Token("++"); }
        }
        
        if (t1.equals("-")) {
         if (t2.equals("-")) { nextToken = getNextToken(); return new Token("--"); }
        }
        
        if (t2.equals("=")) { 
         if (t1.equals(">")) { nextToken = getNextToken(); return new Token(">="); }
            if (t1.equals("<")) { nextToken = getNextToken(); return new Token("<="); }
            if (t1.equals("=")) { nextToken = getNextToken(); return new Token("=="); }
        }
        System.out.println("token = "+result);
        return result;
    }
    
    private Token getNextToken() throws ArithmeticException {
        long theValue;    
        // Return a default empty token when no more tokens in str.
        if( !str.hasMoreTokens()) return new Token();  

        String s = str.nextToken( );
        if( s.equals( " " ) ) return getNextToken();

        char c = s.charAt(0);
        if ('0' <= c && c <= '9') {  // numbers
         try { theValue = Long.parseLong( s ); }
         catch( NumberFormatException e ) {
          throw new ArithmeticException("number format error" );
         }
         return new Token( "_num", s, theValue);
        } 
        
        if ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '$') // identifiers
         return new Token( "_id", s, 0);
        
        return new Token( s, s, 0 );
    }

}

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Calculator {
    /**
     * Simple main to exercise Evaluator class.
     */
    public static void main( String [ ] args )
    {
        String str;
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        int line = 1;
        System.out.print( "Enter expressions, one per line:\nline 1: " );
        
        while (true) {
         try {
          str = in.readLine( );
          if (str == null) continue; 
          if (str.equals("quit")) break;
          Evaluator ev = new Evaluator( str );
          System.out.println( "line " + line + " result >>> " + ev.getValue( ) );
          ev.storeCurrentValue(line);
          
                line++;
                System.out.print( "line "+line+": " );
            }
         catch( IOException e ) { 
          System.err.println( e );
          System.out.print( "line "+line+": " );
         }
         catch (ArithmeticException e) { 
          System.err.println( e );
          System.out.print( "line "+line+": " );
         }
        } // end while
        
        System.out.println("Goodbye.");
    } // end main
}


Again I aplogogize for the length of this post, but as I said, I was thrusted into this class mid-way and this project is worth 10% of the grade, and probably even more for me since I have missed other assignments. I can pick up on languages without too much trouble, but am just starting java. Any help anyone is willing offer is greatly appreciated. Thanks.
Implementing Methods for a Calculator
===

Hey all. I'm new to java and have very little experience with it. I know the basic structures of it but not much more. I just transferred schools and was using Python in my last class. I was given this project and have no clue how to implement the methods asked. I will list the methods I'm supposed to implement and then list what code I have. I apologize ahead of time for the length, but believe all the code is necessary to see how the program runs.

1) This calculator does not support %, the modulo operator. Please modify evalTerm so that when the user types 10%4*6, 12.0 will be printed. 

2)The current implementation does not enforce the definition of variables completely. For instance, x$ or $x can be variable names. Please write the following method 
      static boolean isVariable(String id) 

which returns true if and only if id is a variable by the above definition. (a variable is a element that starts with a $ or letter and followed by a mix of letters and digits) Please use this method in getToken (or getNextToken) of the class EvalTokenizer. If an identifier does not qualify for being a variable, throw an exception of ArithmeticException("bad variable name");

3)This calculator uses the class Memory to maintain the variables and the results of each line. This class is implemented using a "wrapper" class of mapping. Please provide another implementation of Memory using the Binary Search Tree. That is, you are aksed to implement the three methods of Memory using Binary Search Tree: 
    public Memory(); // Constructor of Memory
    
    public void store(String name, double value); 
         // insert the pair  into the binary search tree, where name is the key 
         // and value is its value. 
         // If name is already in the binary search tree, only the value is updated (no duplicated keys).
     
    public double fetch(String name);
        // check if name is in the binary search tree; if not, return 0.0;
        // otherwise, return the value associated with name.

4) We want this calculator to use most functions defined in the java Math class. The Math functions can be added into the calculator by extending the definition of factor 
<factor> ::= <unary func> ( <expression> )
           | <binary func> ( <expression> , <expression> )
<unary func> ::= abs | sqrt | exp | log 
<binary func> ::= min | max | pow

Please modify evalFactor according to the above rules. 

5) Boolean expressions can also be defined and evaluated similarly. For instance, we may extend the definition of statement to include boolean expressions as follows: 
<statement> ::= <variable> = <expression> 
              | <expression>
              | test <boolExpr>
<boolExpr> ::= <boolTerm>
               | <boolExpr> "|" <boolTerm>
<boolTerm> ::= <boolFactor>
         | <boolTerm> & <boolFactor>
<boolFactor> ::= ( <boolExpr> )
           | ! <boolFactor>
           | <expression> <compare> <expression> 
           | true 
           | false
<compare> ::= < | > | >= | <=

Please write an evaluator for the boolean expressions as defined above. 
To test your program, we like to see how the following expressions are evaluated: 

x = log(2010%4*100)
y = sqrt(abs(150-x))+sqrt(x)
test x - y >= min(x, y)
test (x < y) | (y > 0) & (log(y) > 0)

Here is the code I have to start...


public class Token {
        // Data:
        private String type = "";
        private String name = "";
        private double value = 0;
        
        // Constructors:
        public Token() { this( "" ); }
        public Token( String t ) { this( t, 0 ); }
        public Token( String t, double v ) { this( t, t, v ); }
        public Token( String t, String n, double v ) { 
         type = t; name = n; value = v; 
        }

        // Methods:
        public String getType( ) { return type;}
        public String getName( ) { return name;}
        public double getValue( ) { return value; }
        public String toString( ) { return (" type="+type+" name="+name+" value="+value);}
}   

import java.util.*;

public class Memory {

    private HashMap<String, Double> map;
    
    /** Creates new Memory */
    public Memory() {
        map = new HashMap<String, Double>();
    }
    
    public void store(String name, double value) {
        map.put(name, new Double(value));
    }
    
    public double fetch(String name) {
        Double x = (Double)map.get(name);
        if (x == null) return 0;
        return x.doubleValue();
    } 
}


public class Evaluator {
 private double value;
 private static EvalTokenizer str; 
 private static Memory mem = new Memory();
 
 public Evaluator( String s ) {
     str = new EvalTokenizer( s );
     value = evaluate();    
 }
 
 public double getValue() {
  return value;
 }
 public void storeCurrentValue(int line) {
  mem.store("$"+line, value);
 }
 private double evaluate() {
  return evalStatement(str.getToken());
 }
 
 private double evalStatement(Token firstToken) {
  /* <statement> ::= <var> = <expression> 
                   | <expression> 
                   */
  String s = firstToken.getType();
  // if (s.equals("goto")) return 0;
  if (s.equals("_id") && (str.peekToken().getType()).equals("=")) {
   // <statement> ::= <var> = <expression> 
   str.getToken();
   double x = evalExpression(str.getToken());
   mem.store(firstToken.getName(), x);
   return x;
  }
  return evalExpression(firstToken);
 }
 
 private double evalExpression(Token firstToken) {
  /* <expression> ::= <term> 
                    | <expression> + <term>
                    | <expression> - <term>
        */
  double x = evalTerm(firstToken);
  while (true) {
   String op = str.peekToken().getType();
   if (op.equals("+") || op.equals("-")) {
    str.getToken(); // skip "+" or "-"
    double y = evalTerm(str.getToken());
    if (op.equals("+")) x += y;
    else x -= y;
   } else
    return x;
  }
 }
 
 private double evalTerm(Token firstToken) {
  /* <term> ::= <factor>
              | <term> * <factor> 
              | <term> / <factor> 
        */
  double x = evalFactor(firstToken);
  while (true) {
   String op = str.peekToken().getType();
   if (op.equals("*") || op.equals("/") || op.equals("%")) {
    str.getToken(); // skip "*" or "/"
    double y = evalFactor(str.getToken());
    if (op.equals("*")) x = x*y;
    if (op.equals("/")) x= x/y;
    else x = x%y;
   } else return x;
  
  
  }
 }
 
 private double evalFactor(Token firstToken) {
  /* <factor> ::= ( <expression> )
              | + <factor>
              | - <factor>
              | <variable>  
              | ++ <variable>  
              | -- <variable>
              | <number> 
        */
  String t = firstToken.getType();
  
  if (t.equals("(")) { 
   // <factor> ::= ( <expression> )
   double x = evalExpression(str.getToken());
   String nex = str.getToken().getType();
   if (!nex.equals(")")) throw new ArithmeticException("bad expression" );
   return x;
  }
  
  // <factor> ::= + <factor> | - <factor>
  if (t.equals("+")) return (evalFactor(str.getToken()));
  if (t.equals("-")) return (-evalFactor(str.getToken()));
  
  // <factor> ::= <number> 
  if (t.equals("_num")) return firstToken.getValue(); 

  // <factor> ::= <variable> 
  if (t.equals("_id")) return mem.fetch(firstToken.getName());
  
  if (t.equals("++") || t.equals("--")) { 
   // <factor> ::= ++ <variable> | -- <variable> 
   firstToken = str.getToken();
   if ((firstToken.getType()).equals("_id")) {
    double x = mem.fetch(firstToken.getName());
    if (t.equals("++")) {
     mem.store(firstToken.getName(), x+1);
     return x+1;
    } else {
     mem.store(firstToken.getName(), x-1);
     return x-1;
    }
   }
  }
  
  throw new ArithmeticException("bad expression" );
 }
}

import java.util.StringTokenizer;

public class EvalTokenizer {
    // Data:
    private StringTokenizer str;
    private Token nextToken = new Token();
    
    public EvalTokenizer( String line ) { 
     str = new StringTokenizer(line, "+*-/().<>!=%,|& ", true);
     getToken();
    }
    
    public Token peekToken() { return nextToken; }
    
    /**
     * A token is either a number literal (integer or double), an identifier, or special symbols 
     * getToken finds the next token, skipping blanks, and return it.
     * For _int or _double token, place the processed value in value.
     * For _id, place the name of the identifier in name.
     * Throw ArithmeticException if input is unrecognized.
     */
    public Token getToken( ) throws ArithmeticException {
     
      static boolean isVariable(String id) {
      
      }
        Token result = nextToken;
        nextToken = getNextToken();
        String t1 = result.getType();
        String t2 = nextToken.getType();
          
        // handle floating numbers
        if (t1.equals("_num") && t2.equals(".")) {
         nextToken = getNextToken();
         t2 = nextToken.getType();
         if (!t2.equals("_num")) throw new ArithmeticException("number format error" );
         t2 = nextToken.getName();
         double x = result.getValue()+nextToken.getValue()*Math.pow(10.0, -t2.length());
         nextToken = getNextToken();
         return new Token("_num", "", x);
        }
        
        if (t1.equals("$")) {
         if (t2.equals("_int")) {       
          double x = nextToken.getValue();
          nextToken = getNextToken();
          return new Token("_dollar", x);
         } else 
          throw new ArithmeticException("dollar variable format error" );
        }
        
        if (t1.equals("+")) {
         if (t2.equals("+")) { nextToken = getNextToken(); return new Token("++"); }
        }
        
        if (t1.equals("-")) {
         if (t2.equals("-")) { nextToken = getNextToken(); return new Token("--"); }
        }
        
        if (t2.equals("=")) { 
         if (t1.equals(">")) { nextToken = getNextToken(); return new Token(">="); }
            if (t1.equals("<")) { nextToken = getNextToken(); return new Token("<="); }
            if (t1.equals("=")) { nextToken = getNextToken(); return new Token("=="); }
        }
        System.out.println("token = "+result);
        return result;
    }
    
    private Token getNextToken() throws ArithmeticException {
        long theValue;    
        // Return a default empty token when no more tokens in str.
        if( !str.hasMoreTokens()) return new Token();  

        String s = str.nextToken( );
        if( s.equals( " " ) ) return getNextToken();

        char c = s.charAt(0);
        if ('0' <= c && c <= '9') {  // numbers
         try { theValue = Long.parseLong( s ); }
         catch( NumberFormatException e ) {
          throw new ArithmeticException("number format error" );
         }
         return new Token( "_num", s, theValue);
        } 
        
        if ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '$') // identifiers
         return new Token( "_id", s, 0);
        
        return new Token( s, s, 0 );
    }

}

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Calculator {
    /**
     * Simple main to exercise Evaluator class.
     */
    public static void main( String [ ] args )
    {
        String str;
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        int line = 1;
        System.out.print( "Enter expressions, one per line:\nline 1: " );
        
        while (true) {
         try {
          str = in.readLine( );
          if (str == null) continue; 
          if (str.equals("quit")) break;
          Evaluator ev = new Evaluator( str );
          System.out.println( "line " + line + " result >>> " + ev.getValue( ) );
          ev.storeCurrentValue(line);
          
                line++;
                System.out.print( "line "+line+": " );
            }
         catch( IOException e ) { 
          System.err.println( e );
          System.out.print( "line "+line+": " );
         }
         catch (ArithmeticException e) { 
          System.err.println( e );
          System.out.print( "line "+line+": " );
         }
        } // end while
        
        System.out.println("Goodbye.");
    } // end main
}


Again I aplogogize for the length of this post, but as I said, I was thrusted into this class mid-way and this project is worth 10% of the grade, and probably even more for me since I have missed other assignments. I can pick up on languages without too much trouble, but am just starting java. Any help anyone is willing offer is greatly appreciated. Thanks.