Gettysburg College

CS 216
Data Structures and Algorithms

Fall 2024

Assignment 1

Due: Mon, Sep 2, by 11:59pmscreenshot of Calculator with
buttons '+' and '=' as 3rd column
and 1+2+3 shown on the screen
Due: Thu, Sep 5, by 11:59pmcomplete assignment

References

Stack API

TextField API

JavaFX Setup (Dr. Presser)

Description

This assignment will serve as an introduction to the topic of algorithms and data structures, and as a review of CS112. The goal is to implement an algorithm for evaluating an (infix) arithmetic expression. The algorithm uses the Stack data structure to keep track of intermediate results.

The final application that you turn in should work similar to s typical Calculator application. The calculator should work with decimal numbers and support the following operations: addition, subtraction, multiplication, division, exponentiation. In addition, the user should be allowed to use parentheses to group sub-expressions. The GUI should include the = button to indicate the end of expression and should have a clear button.

Calculator Image

Preliminaries

Create a JavaFX project named Calculator.

For the CS Lab computers, here are instructions from CS112 on setting up Java 8 for JavaFX projects:

Java 8 for JavaFX

You may copy and modify the following starter code for your JavaFX application:

Calculator.java

Put each class in a separate file. Javadoc and formal testing is not required for this assignment.

The Algorithm

Here is the algorithm for evaluating arithmetic expressions:
1. create two stacks:
  
   * operand stack -- stores the numbers in the expression and intermediate results
   * operator stack -- stores operators that have not been evaluated

2. scan the input for individual tokens (either a number or an operator):

   2.1. if current token is a number:

              store it on operand stack

   2.2. otherwise (current token is an operator):

              as long as operator on top of the operator stack has
                         higher or equal precedence to current operator:
           
                    pop an operator and two operands
                    evaluate the popped operator
                    push the result back on the operand stack

              push current operator on top of operator stack

[ Parentheses and = operators require extra consideration. ]

The Calculator

Note the following about the pseudocode given above:

The Operators

Implement the following operator hierarchy discussed in class:

                    Operator
                  ^    ^    ^
             ____/     |     \____________
            /          |                  \ 
           /           |                   \
      AddOperator  MulOperator  ...  RightParenOperator
For this assignment one data member that represents precedence level will be sufficient. The data member(s) should be private in the final submission (could be declared protected initially).

The operator classes will have a single constructor and two methods:

Workflow

Here is possible workflow that starts with a basic version of the application and progresses toward the final version:

Follow good programming practices.

What to turn in

Make sure that your cs216 workspace has a project with the following structure (check the spelling and capitalization):
Calculator/
    |
    +----- src/the .java files
    |
    +----- bin/

Upload the .java files in the Moodle dropbox.