0% found this document useful (0 votes)
42 views6 pages

4 First Version

The document provides instructions for Assignment 4, which involves writing a program to evaluate mathematical expressions in Reverse Polish Notation (RPN) using a stack. Students are given a starter project and must demonstrate they can independently apply concepts from Chapter 14 and earlier to complete the programming challenge. The assignment includes example transcripts showing the expected input/output behavior of the program, including an 'explaining' mode that shows step-by-step evaluation and a 'brief' mode that only shows the result.

Uploaded by

veekaprogramming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

4 First Version

The document provides instructions for Assignment 4, which involves writing a program to evaluate mathematical expressions in Reverse Polish Notation (RPN) using a stack. Students are given a starter project and must demonstrate they can independently apply concepts from Chapter 14 and earlier to complete the programming challenge. The assignment includes example transcripts showing the expected input/output behavior of the program, including an 'explaining' mode that shows step-by-step evaluation and a 'brief' mode that only shows the result.

Uploaded by

veekaprogramming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 4 First Version

Start Assignment

Due Monday by 11:59pm Points 15 Submitting a file upload

Overview
Due Date is listed in the Assignment description here on Canvas and in your Canvas Calendar. (https://cascadia.instructure.com/calendar)

This assignment should be doable within the time-frame given. If you find yourself having difficulty, please seek out extra help from the
instructor.

You are not allowed to work in groups this assignment.

For this assignment, you should start, finish, and do all the work on your own. If you have questions, please contact the instructor.

Purpose:
Review what you've learned so far, and demonstrate that you have the ability to independently apply what you've learned to solve a
programming challenge.

For this assignment you will be specifically be focusing on Chapters 14 (and earlier) in order to write a program that uses a Stack to create a
program that evaluates mathematical expressions written in Reverse Polish Notation (https://en.wikipedia.org/wiki/Reverse_Polish_notation)
(which is commonly abbreviated as RPN).

Background:
When we write out a mathematical expression we normally we write the operators between the operands, like so: " 3 + 4 ". We call this an
'infix' notation (my personal mnemonic is that the operators are in-between the numbers).

In Reverse Polish Notation (https://en.wikipedia.org/wiki/Reverse_Polish_notation) the operators follow the operands, like so: " 3 4 + ". We
call this a 'postfix' notation.

One of the nice features of postfix notation is that the order of operations is determined by the expression itself, unlike infix notation which
forces you to memorize PEMDAS, etc. This is accomplished by immediately applying any operators that we see, as we read the expression
from left to right.

So how do we keep track of what we should be applying the operators to? By pushing operands (numbers) onto a stack as we read from left
to right. That way, when we encounter an operator we can pop as many operands off the stack as we need, apply the operator to the
numbers, and then push the result back onto the stack.

Here is some resources explaining Reverse Polish Notation:

Wolfram Mathworld entry on RPN (https://mathworld.wolfram.com/ReversePolishNotation.html)


Interesting historical info plus an emphasis on not needing PEMDAS for RPN
(https://www.calculator.org/articles/Reverse_Polish_Notation.html)
Wikipedia entry on RPN (https://en.wikipedia.org/wiki/Reverse_Polish_notation)
You can find RPN calculators for free online (such as this one (https://www.eeweb.com/tools/online-scientific-calculator/) ).
Be warned that they don't work exactly like ours will (they typically ask you to type a number, then press Enter, then type another number,
then press Enter, then you press the + key to do the addition.
Our program lets the user type in the entire expression on a single line and then evaluates the whole thing.
Still, these calculators may be useful for getting the overall idea of how RPN works.

Task:
This assignment will give you practice with the classes and interfaces that we've been examining in the Java Collections Framework,
specifically the Stack class.

Here's a link to the starter project for this assignment. (https://cascadia.instructure.com/courses/2392968/files/228006263?wrap=1)


(https://cascadia.instructure.com/courses/2392968/files/228006263/download?download_frd=1)
Here's a brief explanation of how to extract the files into a folder so you can work with them on your computer.
(https://cascadia.instructure.com/courses/2367969/pages/how-to-extract-zip-files)

You MUST build on the provided starter project (you will be penalized for starting a new Java file from scratch). You may find it useful to look
up 'labeled continue' statements before digging into the provided starter code.

The program has been started for you (so that you can focus more on the data structures and algorithms and less on the user interface code).
Your program needs to provide the RPN calculator functionality (as described in the 'Background' section) AND it needs to produce output that
is the same as what's listed in the 'Example Transcript' section, below.

Notice that the program can run in one of two modes: an 'explaining' mode where it shows the user exactly how it's evaluating the expression
at each step and a 'brief' mode where it just shows the end result (without showing any intermediate steps). Your program must be able to do
the same.

Note that there is now a video that walks you through the sample code, giving you a high level overview of what different parts of the
code are intended to do (https://cascadia.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=d2ecdce2-4ad2-493f-883d-adf00178657a) .

Example Transcript
User input is listed in large, bold underlined, highlighted text.
Note that the menu options for the main menu are case INsensitive. For example, you should be able to type in upper case A or lower case

a in order to choose the 'Add a new student' menu option.


IMPORTANT NOTE: If your program is given the following input exactly then your program should reproduce the following output exactly.
One way to test this would be to type in all the example inputs yourself. This is a great way to test specific scenarios (such as "Does 1 2 +
work correctly?") but is slow and error-prone for testing everything below. A much faster way is to type all the inputs into a separate text file,
and then copy and paste the entire file into your terminal window. For example, if you create a new text file (in VSCode you can do this using
Control+N (Command+N on a Mac)), and then typing the following into the file:

1 2 +
y
y
1 2 +
n
y
1 2 3 + +
n
y

Now, any time that you want to test these inputs you can copy the entire file, run your program, and paste it into the program (using Control-V
or Command-V, or using VSCode's Edit -> Paste menu option)

IMPORTANT NOTE #2: Your instructor will test your program by pasting all the inputs listed below into your program. For full credit your
program must reproduce the following output exactly. (And in addition, your program must of course also work correctly with any other
expressions even if they're not listed in the example transcript)

Here's the example transcript:

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 1 2 + Stack: []
Pushing 1.0 onto the stack of operands (numbers)
Remaining expression: 2 + Stack: [1.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: + Stack: [1.0, 2.0]
Popping 1.0 and 2.0 then applying + to them
Then pushing the result of 1.0 + 2.0 back onto the stack
END RESULT: 3.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
END RESULT: 3.0
Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 3 + +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
END RESULT: 6.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 3 + +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 1 2 3 + + Stack: []
Pushing 1.0 onto the stack of operands (numbers)
Remaining expression: 2 3 + + Stack: [1.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: 3 + + Stack: [1.0, 2.0]
Pushing 3.0 onto the stack of operands (numbers)
Remaining expression: + + Stack: [1.0, 2.0, 3.0]
Popping 2.0 and 3.0 then applying + to them
Then pushing the result of 2.0 + 3.0 back onto the stack
Remaining expression: + Stack: [1.0, 5.0]
Popping 1.0 and 5.0 then applying + to them
Then pushing the result of 1.0 + 5.0 back onto the stack
END RESULT: 6.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 3 4 - * /


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
END RESULT: -0.5

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 3 4 - * /


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 1 2 3 4 - * / Stack: []
Pushing 1.0 onto the stack of operands (numbers)
Remaining expression: 2 3 4 - * / Stack: [1.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: 3 4 - * / Stack: [1.0, 2.0]
Pushing 3.0 onto the stack of operands (numbers)
Remaining expression: 4 - * / Stack: [1.0, 2.0, 3.0]
Pushing 4.0 onto the stack of operands (numbers)
Remaining expression: - * / Stack: [1.0, 2.0, 3.0, 4.0]
Popping 3.0 and 4.0 then applying - to them
Then pushing the result of 3.0 - 4.0 back onto the stack
Remaining expression: * / Stack: [1.0, 2.0, -1.0]
Popping 2.0 and -1.0 then applying * to them
Then pushing the result of 2.0 * -1.0 back onto the stack
Remaining expression: / Stack: [1.0, -2.0]
Popping 1.0 and -2.0 then applying / to them
Then pushing the result of 1.0 / -2.0 back onto the stack
END RESULT: -0.5

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 + 2 - 2 * 4 + 2 /


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
END RESULT: 3.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 1 2 + 2 - 2 * 4 + 2 /


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 1 2 + 2 - 2 * 4 + 2 / Stack: []
Pushing 1.0 onto the stack of operands (numbers)
Remaining expression: 2 + 2 - 2 * 4 + 2 / Stack: [1.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: + 2 - 2 * 4 + 2 / Stack: [1.0, 2.0]
Popping 1.0 and 2.0 then applying + to them
Then pushing the result of 1.0 + 2.0 back onto the stack
Remaining expression: 2 - 2 * 4 + 2 / Stack: [3.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: - 2 * 4 + 2 / Stack: [3.0, 2.0]
Popping 3.0 and 2.0 then applying - to them
Then pushing the result of 3.0 - 2.0 back onto the stack
Remaining expression: 2 * 4 + 2 / Stack: [1.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: * 4 + 2 / Stack: [1.0, 2.0]
Popping 1.0 and 2.0 then applying * to them
Then pushing the result of 1.0 * 2.0 back onto the stack
Remaining expression: 4 + 2 / Stack: [2.0]
Pushing 4.0 onto the stack of operands (numbers)
Remaining expression: + 2 / Stack: [2.0, 4.0]
Popping 2.0 and 4.0 then applying + to them
Then pushing the result of 2.0 + 4.0 back onto the stack
Remaining expression: 2 / Stack: [6.0]
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: / Stack: [6.0, 2.0]
Popping 6.0 and 2.0 then applying / to them
Then pushing the result of 6.0 / 2.0 back onto the stack
END RESULT: 3.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 3 WordNotSymbol


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
ERROR! Operator (non-numeric input) contains more than 1 character: WordNotSymbol
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 3 WordNotSymbol


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 2 3 WordNotSymbol Stack: []
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: 3 WordNotSymbol Stack: [2.0]
Pushing 3.0 onto the stack of operands (numbers)
Remaining expression: WordNotSymbol Stack: [2.0, 3.0]
ERROR! Operator (non-numeric input) contains more than 1 character: WordNotSymbol
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 3 %


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
ERROR! Operator not recognized: %
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 3 %


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 2 3 % Stack: []
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: 3 % Stack: [2.0]
Pushing 3.0 onto the stack of operands (numbers)
Remaining expression: % Stack: [2.0, 3.0]
ERROR! Operator not recognized: %
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
ERROR! Expected to find 2 operands (numbers) but we don't have a second number on the stack!
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 2 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 2 + Stack: []
Pushing 2.0 onto the stack of operands (numbers)
Remaining expression: + Stack: [2.0]
ERROR! Expected to find 2 operands (numbers) but we don't have a second number on the stack!
Since we can't evaluate that expression we'll ask you for another one to evaluate instead

RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 3 4 5 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
ERROR! Ran out of operators before we used up all the operands (numbers):
9.0
3.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: 3 4 5 +


Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
Remaining expression: 3 4 5 + Stack: []
Pushing 3.0 onto the stack of operands (numbers)
Remaining expression: 4 5 + Stack: [3.0]
Pushing 4.0 onto the stack of operands (numbers)
Remaining expression: 5 + Stack: [3.0, 4.0]
Pushing 5.0 onto the stack of operands (numbers)
Remaining expression: + Stack: [3.0, 4.0, 5.0]
Popping 4.0 and 5.0 then applying + to them
Then pushing the result of 4.0 + 5.0 back onto the stack
ERROR! Ran out of operators before we used up all the operands (numbers):
9.0
3.0

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: // There's like 7 spaces here - everything star

ting at the // is NOT typed in

Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) n
ERROR! Ran out of operands (numbers)

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) y
RPN calculator
Supported operators: + - * /

Type your RPN expression in so that it can be evaluated: // There's like 7 spaces here - everything st

arting at the // is NOT typed in

Would you like me to explain how to expression is evaluated? (y or Y for yes, anything else means no) y
We WILL explain the evaluation, step by step
ERROR! Ran out of operands (numbers)

Would you like to evaluate another expression? (y or Y for yes, anything else to exit) byebye
Thank you for using RPN Calculator!

What to turn in:


Make sure that you hand in the source code for the entire program.
Please include all the .Java files, whether you've changed them or not, just on the off chance you forgot that you changed them.

Every file that you turn in should have a comment at the top of the file that contains the following:
Your name (first and last)
The name of this class (“BIT 143”)
The year and quarter
The assignment number, including the revision number, which starts at 0 (“A2.0”)
If you’re handing this in again for a regrade, make sure to increase the minor version number by one (from “A2.0”, to “A2.1").

How to write and organize your Java code


Please see the specific instructions for how to arrange your Java code here
(https://cascadia.instructure.com/courses/2392968/pages/instructions-for-writing-your-java-programs) .

How to hand in your homework


Instructions for how to hand in your homework are listed here; please follow this link for detailed instructions on how to hand in
your homework. (https://cascadia.instructure.com/courses/2392968/pages/how-to-hand-in-your-homework-using-canvas)

What to turn in:


You need to hand in any and all files that contain work that you've done, so that the instructor can grade your work. While there's a list of files
that the instructor expects to see listed here, please note that programming is an open-ended activity that allows you to make many different
decisions including which file(s) to put your work in. Because of that it's possible that you have done work in a file that's not listed here; you
must include all files that contain your work.

That said, your instructor expects that you'll hand in the following files:

The ReversePolishNotationCalculator.java file


A copy of the instructor feedback file (https://cascadia.instructure.com/courses/2392965/files/228002948?wrap=1) in your upload
Any other files that you contains your work

Grading Criteria:
Please examine the rubric that's attached to this assignment (it should be listed below)

A4_RPN_Reverse_Polish_Notation

Criteria Ratings Pts

Correct Behavior 5 pts 3 pts 0 pts


The program compiles, runs, and passes the tests that the instructor tried. Excellent Good Getting Started
All the of Most of Unable to run the
5 pts
the tests the tests program, or many of the
passed passed tests failed

Correct Implementation 5 pts 3 pts 0 pts


The algorithm is implemented well - good use of methods (as appropriate), efficient code, Excellent Good Getting Started
5 pts
and correctly expresses the algorithm in Java

Code Style 5 pts 3 pts 0 pts


Is the code clearly written, consistently and readably formatted, and generally easy to read? Excellent Good Getting Started

This includes the use of good variable names, comments on each class and each method, 5 pts
using local variables when possible, correct use of generics and the other standard style
guidelines.

Total Points: 15

You might also like