KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > examples > calculator > Calculator


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.examples.calculator;
4
5 public class Calculator
6 {
7     private Parser parser;
8     private Environment environment;
9
10     public Calculator() {
11         environment = new SimpleEnvironment();
12         parser = new InfixParser();
13     }
14
15     public Calculator( Parser parser, Environment environment ) {
16         this.parser = parser;
17         this.environment = environment;
18     }
19
20     public double calculate( String JavaDoc expressionString ) throws CalculatorException {
21         Expression expression = parser.parse(expressionString);
22         return expression.evaluate(environment);
23     }
24
25     public void setVariable( String JavaDoc name, String JavaDoc valueString ) throws CalculatorException {
26         environment.setVariable(name, parser.parse(valueString));
27     }
28 }
29
Popular Tags