The next two projects combine working with trees with producing a graphical user iterface (GUI). These pull together lots of threads from this course, and you get something fun to show your friends.
You will write a program that operates as a graphing calculator. It will have a window that allows a user to enter in a function and set min and max values for x and y (to specify the viewing range); it will draw a graph of the function within the specified range.
The program Dr. VanDrunen wrote to solve this problem looks like this:
You are not required to mimic this window exactly, but your program should have a window with the basic features/elements found here.
This program has three distinct parts:
You will turn in the first part as project 8, because it is independent of the other portions. The other two parts are described in project 9. Do not delay getting started.
Clone the starting files from the course repository:
$ hg clone /cslab/class/csci235/projects/project8
The first phase of the program is very similar to what you did for lab 12.
The graphing calculator will display the graph of a function whose body is defined by the following language:
expression --> numeral |x
|(
expression op expression)
op -->+
|-
|/
|*
|^
Note there are three changes to the language from lab 12:
double
rather than int
.
Recall that we require that the expression be fully parenthesized, and we do not allow no unary negation (i.e., negative sign) in the language, so instead of
-3x^2 + 5
one must write
((0 - (3 * (x ^ 2))) + 5)
or
(((0 - 3) * (x ^ 2)) + 5)
(This might seem like an undue burden on the user, but
believe me that implementing things like order of operation,
implicit multiplication (ie, 3x
),
and unary minus would make this project severely more difficult.)
You must model expressions in this language using trees just as in lab 12 and interpret strings in the language by building appropriate trees.
The ExprNode
interface has been provided.
There is one difference between this and the version from lab 12:
the signature of the evaluate method is now
double evaluate(double x);
It returns a double (everything in this project will be double-valued, not int-valued), but more importantly, it also has a double parameter. This parameter indicates the value of the variable.
Your task is to write classes Number
,
Variable
, and Operation
(or
similar classes; you don't have to use these exact names)
which will implement ExprNode
.
They will be similar to those your wrote in lab 12.
The interesting part is figuring out how the evaluate()
method will differ.
(Hint: What will the various classes do with the
formal parameter x
?
The Variable
class is the only one that will
use it, the Number
class will ignore it,
and the Operation
class will pass it along
in the recursive calls.)
You are provided the class ExprStringSlicer
just as with lab 12; refer to that lab description to
see how it works.
The class Interpreter
has a static method
parse()
which will be used to convert strings to ExprNode trees.
Your task is to fill-in the method
parse()
.
Hints:
parse()
to build the subexpressions.
x
, (
,
or something else?
You can test it using the main()
method of Interpreter
, giving
the expression (in quotes) and the x-value on
the command line, for example
$ java Interpreter "((0 - (x ^ 2)) + 5)" 3.5
Don't worry yet about handling erroneous input. At this point it is ok if your program crashes on unparsable text.
Be sure that you have committed all of your work to your Mercurial repository.
Make a typescript in which you compile and run
Interpreter
for an interesting set of expressions and
x values. Be sure to include evaluating the same expression for more
than one x.
Turn in your typescript along with the .java
source
files for your node classes plus any others you wrote or modified for
this phase. You could turn in all of the Java sources with a command like
$ /cslab/class/csci235/bin/handin project8 typescript *.java
DUE: Wednesday, April 20, at 5:00 pm.