CSE 142 Winter 2002

Homework #3

Electronic Calculator

Warmup due: Electronically, 11:00pm, Sunday, February 3

Final project due: Electronically, 11:00pm, Sun., Feb. 10;
written report due in lecture, Monday February 11.


Purpose

The purpose of this assignment is to

You will be given a BlueJ project that implements part of the calculator, and your job is to complete it.

Getting Started

Download the following three files: Calculator.java, UserInterface.java, and CalcEngine.java.  Place them in a folder (none of the folders in the path name to these files should have a space or any special character (such as '#') in them) and create a BlueJ project from them.  Here is a brief description of the three classes.

Calculator

This is the main class.  It creates a calculator engine (CalcEngine) and a user interface and starts the application.  That is all it does.  You should not modify this class, and don't be alarmed if you don't understand some or all of the code in this class.  It uses some Java libraries that we haven't discussed.

UserInterface

This class provides the user interface of the calculator: the window on the screen, the buttons, etc.  You should not modify this class.  Again, don't worry if you don't understand the code in this class.

CalcEngine

This is where all the real work is done.  The calculation engine is responsible for implementing the logic of the calculator (described below).  It is informed of button pushes and does the calculations.  This class is (currently) improperly implemented.  It is your job to write this class.

You will need to define the instance variables and implement the methods.  Notice that the three downloaded files do not even compile (why?).  The method declarations are already there, but the body of each method is missing (they don't do anything).  You may also add methods for private use within CalcEngine if you wish.

IMPORTANT: Do not remove or modify the names, number of parameters, their types, or the return types of the methods that are already there.  These methods are called from the other classes.  If you change them, your project will not compile.

The Goal

Your finished product will look and behave like a fully working calculator with addition and subtraction functionality.  Here is an example session:

key typed display notes
0 initial display
4 4
2 42 user has typed in numbers
7 427
+ 427 displays current number after user clicks the + button
5 5 switches to the new value display
+ 432 displays the interim result when starting the next operation
2 2
= 434 result display
C 0 C clears the display and reset the internal state of the calculator

How the CalcEngine Methods are Called

Whenever the user clicks on one of the buttons, one of the methods in CalcEngine is called.  For example, if the 'C' button is clicked, then the clear method is invoked.  The clear method should then change the internal state of the calculator appropriately.  If the user clicks on a digit, then the buttonPressed method is called, and the argument passed to the method is an integer that is the digit that was pressed.  If the user clicks the plus sign, the minus sign, or the equals sign, then the corresponding method in CalcEngine is invoked.  

The internal state of the calculator (which is kept track in the instance variables of the CalcEngine object) must be updated correctly each time the user clicks on one of the buttons.  After the appropriate method in CalcEngine is invoked, the getDisplayValue method is invoked, and the result returned by that method will be displayed in the calculator's display area.  

Warmup

As a preliminary step towards building the complete calculator described above, you should implement enough of it so it will display a one-digit number whenever a digit key is clicked, and will reset the display to 0 when the C key is clicked.  To do this, you will need to complete enough of the implementation of the constructor and the getDisplayValue, buttonPressed, clear, getTitle, getAuthor, and getVersion methods to get this part of the assignment to work.  You may leave the other methods (plus, minus, equal) unimplemented.  (Hint: You will need at least one instance variable to keep track of the value that should be displayed.)

Here is a sample session for what you should build for the warmup part of the assignment:

key typed display notes
0 initial display
4 4 user typed a 4.  Display it.
2 2 user typed a 2.  Display it.
7 7 user typed a 7.  Display it.
+ 7 do nothing
5 5
+ 5 do nothing
2 2
= 2 do nothing
C 0 C clears the display and reset the internal state of the calculator

When the user clicks the C button, the display should show 0, and the calculator should behave as though it had just started.  Notice that this is a pretty useless calculator at this point.  In the second part of the assignment, you will implement the methods that actually do the calculating.

You must decide what instance variable(s) you will need to implement this part of the functionality of the calculator.

Hints and Suggestions

Turning in the warmup exercise

Electronically turn in your modified CalcEngine.java.  At the top of your .java file, put your name and your section in a comment.  Go to the Turn-in form.

End of Warmup

Now that we have implemented a small (but important) part of the calculator, it's time to complete it, by making the CalcEngine do the right thing when the user clicks the plus, minus, or equal sign.

What Should Happen When the User Clicks +, -, or =

The calculator that you build should behave as you would expect.  In case there are any doubts, here is a detailed description.  When the user clicks numbers, the value increases just as in a calculator.  For example, if the user has already pressed 9 and 8, then 98 should be in the display area.  Then if the user clicks a 0, then 980 will appear in the display area.

Suppose that the user has entered two values.  If the user then clicks the plus sign, then the result of computing the last two values entered should be displayed.  For example, if the user clicked 9, 8, 0, plus, 4, 2, plus (in that order), then 1022 (the result of 980+42) should be the final number that appears in the display area.  1022 will then be added to the next value entered.  Similarly, if the user clicks a minus sign, then the result value is computed and the next value entered is subtracted from the result value.  For example, if the user clicked 9, 8, 0, plus, 4, 2, minus, then 1022 would appear in the display area, and the next number entered would be subtracted from 1022.  If the user clicks the equals sign, then the result of the last operation is calculated and displayed.  If the user then enters another number, then the old result is lost forever and a new calculation begins.  For example, if the user clicks 9, 8, 0, minus, 4, 2, equal, 7, 3, plus, 2, equal, then 75 should be the final number that is displayed.  

If the user has just clicked on the plus or minus sign, and then clicks on either a plus or minus sign, then the first plus or minus sign is ignored.  For example, the sequence 9, 8, 0, minus, plus, 1, 0, equal should produce 990.  9, 8, 0, minus, plus, plus, minus, 1, 0 should produce 970 (because minus was the last operator clicked) in the display area.  If the user clicks on the equal sign immediately after a plus or minus, then the plus or minus that they clicked is ignored for future calculations.  For example, the sequence 9, 8, 0, minus, 1, 0, plus, equal, 7, equal should produce 7 in the display area.

If the user has just clicked on the equal sign, then additional clicks on the equal sign should not change the display.  If a number is entered after the equal sign, then a new calculation begins (as described above).  However, if an operator is clicked after an equal sign, then the calculations continue.  For example, the click sequence 9, 8, 0, minus, 4, 2, equal, 7, 3, plus, 2, equal, minus, 3, 4, equal should result in 41 in the display area.

Hints and Suggestions

Turning in the Complete Project

The turn-in page will accept a CalcEngine.java file.

You will be graded on two things: the correctness of your program (i.e., how well you followed directions, and how well your program does what we asked), and on the clarity of your program (i.e., how readable it is, and how well it communicates the intent of what you were trying to accomplish to the human reader).  Clarity will benefit from good explanatory comments, good choice of variable names, good use of local names (if needed), and good use of indentation to group things.

Electronically turn in your CalcEngine.java file.  Turn-in form.

Written Report

In lecture on Monday, February 11, you must turn in a short (no more than one page) written report about your project.  Include your name, section, homework number and date at the top.  Briefly describe the instance variables you defined for the CalcEngine class.  Did the calculator work correctly for all possible sequences of button clicks?  If not, describe what did not work.  Describe the most important things you learned from this project, which could be new ideas that you have never seen before, or misconceptions that you had that the project helped you straighten out.

End of Complete Project