CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Similar documents
CMPSCI 187 / Spring 2015 Sorting Kata

CMPSCI 187 / Spring 2015 Hanoi

CMPSCI 187 / Spring 2015 Hangman

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

COMPSCI 220 Programming Methodology Project Assignment 06: Expression Evaluator

Lab 7 1 Due Thu., 6 Apr. 2017

Assignment 5. Introduction

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

CS 211 Programming Practicum Spring 2017

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 330 Homework Comma-Separated Expression

Tips from the experts: How to waste a lot of time on this assignment

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 116. Lab Assignment # 1 1

CS 211 Programming Practicum Spring 2018

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Chapter 4 Defining Classes I

Announcements. 1. Forms to return today after class:

CS 211 Programming Practicum Fall 2018

Advanced Java Concepts Unit 3: Stacks and Queues

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CS2383 Programming Assignment 3

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Full file at

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

4. Java Project Design, Input Methods

1.00 Lecture 4. Promotion

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Lesson 10: Quiz #1 and Getting User Input (W03D2)

CSIS 10B Lab 2 Bags and Stacks

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Full file at

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

CmpSci 187: Programming with Data Structures Spring 2015

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

CS Introduction to Data Structures How to Parse Arithmetic Expressions

An Introduction to Trees

SFU CMPT 379 Compilers Spring 2018 Milestone 1. Milestone due Friday, January 26, by 11:59 pm.

Project 3: Implementing a List Map

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

1.00/1.001 Tutorial 1

ASSIGNMENT 5 Objects, Files, and More Garage Management

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

ASSIGNMENT 5 Objects, Files, and a Music Player

infix expressions (review)

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

In this lab we will practice creating, throwing and handling exceptions.

Computer Science II Fall 2009

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Fun facts about recursion

CS W3134: Data Structures in Java

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

Tips from the experts: How to waste a lot of time on this assignment

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CS111: PROGRAMMING LANGUAGE II

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

13 th Windsor Regional Secondary School Computer Programming Competition

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Prefix/Infix/Postfix Notation

Example Program. public class ComputeArea {

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CS664 Compiler Theory and Design LIU 1 of 16 ANTLR. Christopher League* 17 February Figure 1: ANTLR plugin installer

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Tips from the experts: How to waste a lot of time on this assignment

Formal Languages and Automata Theory, SS Project (due Week 14)

CS 231 Data Structures and Algorithms, Fall 2016

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

XC Total Max Score Grader

3. Java - Language Constructs I

CSCI 135 Programming Exam #1 Fundamentals of Computer Science I Fall 2014

Comp Assignment 2: Object-Oriented Scanning for Numbers, Words, and Quoted Strings

CmpSci 187: Programming with Data Structures Spring 2015

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

Project 3: RPN Calculator

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Compilers Project 3: Semantic Analyzer

St. Edmund Preparatory High School Brooklyn, NY

CISC-235. At this point we fnally turned our atention to a data structure: the stack

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

Laboratory 1: Eclipse and Karel the Robot

Transcription:

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Due on Thursday, 05 March, 8:30 a.m. Marc Liberatore and John Ridgway Morrill I N375 Section 01 @ 10:00 Section 02 @ 08:30 1

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Contents Overview 3 Learning Goals................................................. 3 General Information 3 Policies..................................................... 3 Test Files.................................................... 3 Problem 1 4 Import Project into Eclipse........................................... 4 Files to Complete................................................ 5 Implementing LinkedStack......................................... 5 Postfix Expressions and Evaluators....................................... 5 Implement Arithmetic Operators........................................ 6 Implement a Postfix Arithmetic Evaluator................................... 6 Export and Submit............................................... 7 Additional Notes................................................ 7 Page 2 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Overview For this assignment, you will implement an evaluator for postfix expressions. Your evaluator will be stack-based, and capable of evaluating correctly-formed but otherwise arbitrary arithmetic expressions on ints. You will implement the stack using linked lists. Learning Goals Demonstrate understanding of a linked-list-based stack implementation. Reinforce the concept of dynamic dispatch. Understand and implement the core of an object-oriented, stack-based postfix expression evaluator. Implement JUnit tests and drivers for a nontrivial code base. General Information Read this entire document. If, after a careful reading, something seems ambiguous or unclear to you, then email cs187help@cs.umass.edu immediately. Start this assignment as soon as possible. Do not wait until 5pm the night before the assignment is due to tell us you don t understand something, as our ability to help you will be minimal. Reminder: Copying partial or whole solutions, obtained from other students or elsewhere, is academic dishonesty. Do not share your code with your classmates, and do not use your classmates code. You are responsible for submitting project assignments that compile and are configured correctly. If your project submission does not follow these policies exactly you may receive a grade of zero for this assignment. Policies For some assignments, it will useful for you to write additional class files. Any class file you write that is used by your solution MUST be in the provided src directory you export. The TAs and instructors are here to help you figure out errors, but we won t do so for you after you submit your solution. When you submit your solution, be sure to remove all compilation errors from your project. Any compilation errors in your project will cause the autograder to fail, and you will receive a zero for your submission. Test Files In the test directory, we provide several JUnit test cases that will help you keep on track while completing the assignment. We recommend you run the tests often and use them to help create a checklist of things to do next. But you should be aware that we deliberately don t provide you the full test suite we use when grading. Page 3 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator We recommend that you think about possible cases and add new @Test cases to these files as part of your programming discipline. Simple tests to add will consider questions such as: Do your methods taking integers as arguments handle positives, negatives, and zeroes (when those values are valid as input)? Does your code handle unusual cases, such as empty or maximally-sized data structures? More complex tests will be assignment-specific. To build good test cases, think about ways to exercise methods. Work out the correct result for a call of a method with a given set of parameters by hand, then add it as a test case. Note that we will not be looking at your test cases, they are just for your use. Before submitting, make sure that your program compiles with and passes all of the original tests. If you have errors in these files, it means the structure of the files found in the src directory have been altered in a way that will cause your submission to lose some (or all) points. Note: You may not use any of the classes from the Java Platform API that implement the Collection interface (or its sub-interfaces, etc.). The list of these classes is included in the API documentation, available at: http: //docs.oracle.com/javase/7/docs/api/java/util/collection.html. Doing so will be viewed as an attempt to cheat. Problem 1 Import Project into Eclipse Begin by downloading the starter project and importing it into your workspace. It is very important that you do not rename this project as its name is used during the autograding process. If the project is renamed, your assignment will not be graded, and you will receive a zero. The imported project may have some errors, but these should not prevent you from getting started. Specifically, we may provide JUnit tests for classes that do not yet exist in your code. You can still run the other JUnit tests. The project should normally contain the following root items: src This is the source folder where all code you are submitting must go. You can change anything you want in this folder (unless otherwise specified in the problem description and in the code we provide), you can add new files, etc. support This folder contains support code that we encourage you to use (and must be used to pass certain tests). You must not change or add anything in this folder. To help ensure that, we suggest that you set the support folder to be read-only. You can do this by right-clicking on it in the package explorer, choosing Properties from the menu, choosing Resource from the list on the left of the pop-up Properties window, unchecking the Permissions check-box for Owner-Write, and clicking the OK button. A dialog box will show with the title Confirm recursive changes, and you should click on the Yes button. test The test folder where all of the public unit tests are available. Problem 1 continued on next page... Page 4 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Problem 1 (continued) JUnit 4 A library that is used to run the test programs. JRE System Library This is what allows Java to run; it is the location of the Java System Libraries. If you are missing any of the above or if errors are present in the project (other than as specifically described below), seek help immediately so you can get started on the project right away. Files to Complete Each of the files listed here have unit tests associated with them. For full credit, you must correctly implement each class according to the specifications given in this assignment description and in the comments of the code. Keep in mind we have tried to provide you with helper classes that should make your job easier. stack.linkedstack A stack data structure that MUST use a generic LLNode<T> linked-list structure to allow for unbounded stack size. Remember, do not use any classes from the Java Platform API that implement the Collection interface. If in doubt check with us. language.arith.suboperator A binary operator for performing subtraction on two integers. language.arith.multoperator A binary operator for performing multiplication on two integers. language.arith.divoperator A binary operator for performing division on two integers. language.arith.negateoperator A unary operator for performing negation on a single integer. evaluator.arith.arithpostfixevaluator An evaluator for simple arithmetic using postfix notation. Implementing LinkedStack You need to implement a basic stack data structure using a linked list data type internally to allow for an unbounded structure. Start by reading the comments in the StackInterface interface. It will provide you with some direction on what each method needs to do. Also, it will be helpful to review DJW Section 3.7 and the Discussion 05 assignment. The tests associated with the LinkedStack class are in stack.linkedstacktest in the test folder. You want to make sure you pass all of the tests provided. We strongly encourage you to try and think of additional tests that might trip you up. Did you meet all of the requirements specified by the interface? Postfix Expressions and Evaluators As discussed in lecture and in DJW, a postfix expression evaluator takes as input an arithmetic expression, written in postfix notation, and computes and returns the result. Typically arithmetic is written in infix notation, where the Problem 1 continued on next page... Page 5 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Problem 1 (continued) operator, such as +, is written between the two operands, such as 1 + 5. Postfix notation (sometimes called reverse polish notation) places the operator after the operands. Postfix notation is unambiguous about the order of operations when evaluating an expression, unlike infix notation, which requires rules of operator precedence and parentheses to be unambiguous. For example, the postfix expression 4 5 7 2 + is equivalent to the infix expression 4 (5 (7 + 2)). Implement Arithmetic Operators Before you can create a postfix evaluator, you will need to define what each of the possible postfix operators do. For this assignment, you are required to support addition, subtraction, multiplication, division, and negation of integers. Unlike DJW s implementation, which decides how to evaluate by using the value of a string (see page 224), you will use the more idiomatic Java approach of dynamic dispatch. Practically, this means you ll define each operator as a class implementing the Operator<T> interface, and call the performoperation() method on a reference to an Operator<T> Java will look at the class of the object to choose the correct method implementation. To help facilitate this, you have been provided with an Operator<T> interface. Take a moment to review the interface. You might also want to review the BinaryOperator<T> class. It implements the Operator<T> interface, is subclassed by PlusOperator and other binary operators, and provides a common place for the functionality shared by all of them to reside. Now run the operator.arith.plusoperatortest test. All of the tests pass! Lucky you! Go ahead and open up the PlusOperator class and you will see an implementation. Review this implementation, then complete the SubOperator, DivOperator, and MultOperator classes. Each time you implement something, be sure to run the associated tests to see how you are doing. Finally, you will need to implement the unary NegateOperator class. Although it is not required, it is recommended that you create an abstract class UnaryOperator first, similar to the BinaryOperator<T>, and then extend it in NegateOperator. Note that for this evaluator, unary negation is assigned the operator "!". The ArithPostfixEvaluator class does not require it, but consider adding a tostring() method to each of the arithmetic operator classes. It may aid in your debugging, particularly if you use println() and a driver in addition to unit tests and the debugger. Implement a Postfix Arithmetic Evaluator Now that you have a stack and operators defined, it is time to create an evaluator. Open up the evaluator. arith.arithpostfixevaluator class and you will see several TODO comments. Before starting, check out the evaluator.arith.arithpostfixevaluatortest class (in the test directory) to see an example of how the evaluator is expected to be called and the results that are expected to be returned. First, you want to initialize the stack you will be using with your implementation. Second, determine what you will do when you see an Operand. Third, determine what you will do when you see an Operator. Finally, determine what you will return. This code will be similar to the sketch we gave in lecture and to DJW s implementation. The biggest difference to DJW s evaluator is that yours will have Operands and Operators rather than primitive types and strings, and it will call performoperation() rather than hardcoding the evaluation as DJW does. Problem 1 continued on next page... Page 6 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Problem 1 (continued) Export and Submit When you have completed the changes to your code, you should export an archive file containing the entire Java project. To do this, click on the postfix-student project in the package explorer. Then choose File Export from the menu. In the window that appears, under General choose Archive File. Then choose Next and enter a destination for the output file. Be sure that the project is named postfix-student. Save the exported file with the zip extension (any name is fine). Log into Moodle and submit the exported zip file. Additional Notes Using the ArithPostfixParser You have been provided with a class for parsing arithmetic postfix expressions. It is not important that you understand how it is implemented 1 but it is important that you understand what the interface provides for you. In particular, note that it implements the Iterable interface over a sequence of Tokens. You can use this interface to iterate over the input, as shown in parser.arith.arithpostfixparserexample. Material on Stacks Stacks have been covered in both lecture and in the book. If you are having trouble, you should review the lecture materials and the book. For stacks in this assignment you really want to focus on sections 3.1 and 3.7. Material on Exceptions For this assignment, you will need to signal exceptional situations. For a quick reference on how to throw an exception, check out language.binaryoperator. This is an abstract class that meets many of the requirements for the language.operator interface. You will notice that its setoperand method has several exceptional states and throws the exceptions detailed in the language.operator interface. Also, there is material available in the book in chapter 3 (focus on section 3.3 and 3.4). Where is the Driver Class? If you scan through the provided files, you will notice none of them contain a main method. This means that out of the box you cannot run your code. Instead, we highly recommend you create your own JUnit tests and standalone drivers for testing out your elements. For example, when you implement the MultOperator, you might write a driver somewhere with the following: 1 public static void main(string[] args){ 2 Operator<Integer> multop = new MultOperator(); 3 Operand<Integer> operand0 = new Operand<Integer>(5); 4 Operand<Integer> operand1 = new Operand<Integer>(6); 5 multop.setoperand(0, operand0); 6 multop.setoperand(1, operand1); 7 Operand<Integer> result = multop.performoperation(); 8 System.out.println(result.getValue()); 9 } Or add to the existing tests if you don t see how to convert the above into one or more @Tests, check in with a TA or instructor. 1 If you re curious, the TokenIterator class is an inner class used to provide the Iterator that the Iterable interface of PostfixEvaluator requires. Inner classes are a language feature we may cover later (or not at all). Problem 1 continued on next page... Page 7 of 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Problem 1 (continued) We also suggest you write a driver that reads in postfix expressions from the user and calculates them. It might look something like this: 1 public static void main(string[] args){ 2 Scanner s = new Scanner(System.in); 3 PostfixEvaluator<Integer> evaluator = new ArithPostfixEvaluator(); 4 System.out.println("Welcome to the Postfix Evaluator"); 5 System.out.println("Please enter a postfix expression to be evaluated:"); 6 String expr = s.nextline(); 7 // Sometimes I get an exception. Maybe I should use a try/catch block. 8 Integer result = evaluator.evaluate(expr); 9 System.out.println("The expression evaluated to: " + result); 10 // Maybe I should ask user if they want to enter another expression and loop 11 } What is this public static enum Type? In the ArithPostfixEvaluator code we provided for you we wrote a switch statement that has two cases: OPERAND and OPERATOR. If you decide to dig to see what these are, you will find the following: 1 /** 2 * A {@link PostfixParser} can produce different types. 3 * @author jcollard 4 * 5 */ 6 public static enum Type { 7 8 /** 9 * Indicates that the value being parsed is an {@link Operand} 10 */ 11 OPERAND, 12 13 /** 14 * Indicates that the value being parsed is an {@link Operator} 15 */ 16 OPERATOR; 17 } enums are a way to define your own types; a variable of a given enum type can hold only the values defined in the enum (or null). The PostfixParser can produce two different kinds of things, operators and operands. This enum, called Type, formalizes that information in a clearer way than, for example, a boolean isoperator would. The Token object representing the next token returned by the parser contains one of these two Types, indicating which getter of the object you should call next in your evaluator. Note that null is technically a valid value for an enum; we use the default branch of the switch statement to guard against this possibility. If you are interested in knowing more about enumerated types in Java, we recommend you check out Java s tutorial on enums. Page 8 of 8