|
Due by midnight on Sunday, February 22nd. Hw2b CalculatorThis project is a more specific, enhanced version of the calculator project 4.20 on page 116 in the Drake text. - (1 point) Create a Java project called yourname-hw2. Add a test folder to the project.
- (1 point) Within both folders, src and test, create a Java package called cs271.hw2.yourname.
- (1 point) Create a (checked) CalculatorException class that extends Exception (not RuntimeException).
- (3 points) Create a Calculator interface with the following methods:
- number: pushes a new double number on top of the evaluation stack; returns no result.
- plus: adds the two numbers on top of the stack, removes both of them, and pushes the result of the addition on top of the stack; returns no result, but throws a CalculatorException if fewer than two numbers are on top of the stack.
- minus: ditto for subtraction.
- times: ditto for multiplication.
- div: ditto for division.
- top: returns a list with up to two numbers on top of the stack; that is, if there is only one number on the stack, the method returns a list with that number as its only item; if the stack is empty, the method returns an empty list.
- (5 points) Create a CalculatorImpl class that implements the Calculator interface by using an instance of (java.util.)Stack as its evaluation stack.
- (5 points) Create a CalculatorTest JUnit 4.x test case that thoroughly tests all methods in the Calculator interface. For example, ensure that the operator methods behave as expected in the various cases (at least two numbers on the stack, fewer than two numbers on the stack); ensure that the most recently pushed number shows up on top of the stack; etc.
- (2 points) Create a Main class with a main method that you can run as a Java application. This method should behave similarly to the example in figure 4.39 on page 117. In particular:
- The method prints up to two numbers on top of the stack before the prompt (colon). The topmost number should appear in the rightmost position. (Do not print the line numbers provided in bold in the figure for easier reference!)
- The method should handle properly formatted floating-point numbers, not just single digits.
- You can use a loop of the form:
final BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = in.readLine())!= null) {
// process input
}- (2 points) Provide appropriate javadoc comments for all non-test classes and methods. Run javadoc through Eclipse (or outside Eclipse). Also provide sufficient inline comments explaining your implementation to others (such as myself).
|