Gettysburg College

CS 216
Data Structures and Algorithms

Spring 2026

Assignment 1

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

References

Stack API

TextField API

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

Follow the instructions on Moodle to download the IDE that we will use this semester. The video shows instructions for CS111, but the steps are the same. You will mostly use the buttons:

Create a project named Calculator.

You may modify/adapt 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

Upload all .java and .log files in the Moodle dropbox.