Homework #7 CMSC 131
Due March 30th, 2003 Object-Oriented Programming I
  Spring 2004

 

Calculator

This homework will give you an opportunity to design some more complicated logic, and to get practice writing testing code. You will be graded as follows:

You will write a program that implements the logic of an interactive calculator along with testing code that tests your logic.  A visual interface is supplied to you, so you can also hook up your logic to the interface and have an interactive calculator (see figure below).

The calculator must function according the following rules:

  1. The initial value of the display (when the calculator starts) must be 0.
  2. Entering digits should modify the displayed number by appending the digit to the right side of the current number.
  3. Entering a number followed by an arithmetic operator (+, -, * or /) should store both items so that pressing a sequence of numbers followed by an operator followed by a number followed by the equals key should calculate and display the result.
  4. Pressing an alternating sequence of numbers and operators should calculate results as expected without requiring that the equal key be pressed after each calculation.
  5. Pressing a number key immediately after the '=' key should clear the displayed number, and start entering a new number.
  6. Pressing the "C" button should set the display to 0 and reset the internal state
  7. If the user attempts to divide by zero, you must display "Divide by zero" on the display and reset the internal calculator state.
  8. You must support negative results, but you do not need to explicitly support entry of negative numbers.
  9. You do not have to handle pressing of operator keys multiple times in succession.  That is, we will never test your code with a sequence of keys such as (8, +, 5, +, +, 7).
  10. Note that you do not need to handle overflow.  This means that if you calculate very large numbers, you will get strange and incorrect results (and this is ok).
  11. Added 3/18: Pressing a digit when 0 is displayed should display only the new digit, and the initial 0 is not displayed.

The following table shows button clicks and what must be shown in the display:

Button Display
2 2
+ 2
5 5
= 7
- 7
3 3
- 4
3 3
= 1
/ 1
0 0
= Divide by zero
5 5
7 57
+ 57
2 2
3 23
= 80
C 0

Getting the Code Distribution

We will be using CVS with AutoCVS as we have for previous assignments.  You can access the assignment code by checking it out from CVS with Eclipse (it is called 'hw7'). You can read the API documentation here

Your Assignment

Your solution must create the Calculator class which implements the CalculatorMVC interface.  CalculatorMVC requires you to follow a simple model-view-controller pattern as in the previous project.  You are free to add instance variables and methods to the Calculator class as you see fit.

In addition, you must modify the supplied class called CalculatorTester to test your implementation.  The testing program supplied tests the example shown in the table above, but you must write at least 10 more tests to evaluate all features of your calculator.

The constructor for your Calculator class must have the signature:

public Calculator();

Calculator also must implement these two methods (as defined in CalculatorMVC):

public String getDisplayString();

getDisplayString() gets called by CalculatorFrame immediately after keyClicked() is called and the returned string is shown in the calculator GUI.  In addition, getDisplayString() is called once on startup after setCalculatorMVC() is called.

public void keyClicked(char key);

keyClicked() is called by CalculatorFrame immediately after any button in the calculatorGUI is pressed.

You should invoke your calculator from a main method as follows.  By doing it this way rather than including the CalculatorFrame code in the Calculator constructor, you can write the CalculatorTester class which tests your calculator without using the GUI.

static public void main(String[] args) {
    Calculator calc = new Calculator();
    CalculatorFrame frame = new CalculatorFrame("Calc");
    frame.setCalculatorMVC(calc);
}

Submitting Your Assignment

Submit your program by right-clicking on your project and select "Submit Project". Note that this option will only appear if you have successfully installed AutoCVS as described previously.  If you decide that you would like to make a change after you have submitted your project, you can resubmit as many times as you like before the extended deadline.  We will grade the last project submitted.

/* Name
 * Student ID
 * Homework #7
 */

<your code goes here>

Open Assignment

This is an "open" assignment.  You are free to get whatever help you want to get this assignment done.  You may look on the web, and talk to friends.  You can even look at other student's solutions or have other students help you debug your code.  But there are two conditions:

  1. You must document all outside help you get in your timelog.  If you have any non-trivial interaction with anyone about this homework, you must describe it (i.e., "<student xxx> helped me debug my program for 2 hours").  You will not be penalized for this help, but we want to know how much help people are getting to do this work - and we want you to be explicitly aware of how much help you are getting.

  2. You are responsible for understanding the code you turn in.  We expect you to know what you did as if you did it entirely privately.  You need to be able to do this level of work at this point in the semester if you expect to succeed in following projects and in exams.

Challenge Problem

Change your getDisplayString() method to display all numbers with US formatting where every 3rd digit (counting from the right) is separated by a comma.  So, instead of displaying "12345", you should display "12,345".  You should use the java.lang.StringBuffer class to construct the string.  You should read the Java API docs to learn about this class.