For your second project, you will create a simple calculator.
Your final project should look like:
The calculator should function as a normal calculator. The output
display can be either centered (as above, which is the easiest thing to
do), or right-justified (as in a standard calcualtor display)
Grading
You will be graded on several criteria:
Program decomposition: 10
Each method should do only one thing: you should have private/protected
helper methods so that no one method is too long, or tries to do too
many things. You should only need to write two different button
handlers (that is, classes that implement ActionListener) -- one
for numbers, and one for operators. You will of course need a
total 16 different instances of these button handlers, one for each
button. Remember that you can pass parameters to the constructor
of the button handler.
Also, the constructor for your main calculator panel needs to do a
whole lot of work (set up subpanels, buttons, etc) -- you should
probably have helper methods instead of one larger constructor that
Coding standards 10
Be sure to do proper indentation & commenting
See the
Coding Standards
for more information
Working
Window with output display and buttons in the correct locations 20
Can enter numbers 10
Can enter multiple-digit numbers 20
Can calculate a simple expression with two operands and an equals (20 +
15 = would display 35, for instance) 20
All operators and C (clear) work correctly 5
Can chain together operatons (10 + 15 + 20 + 30 = would display 75, for
instace) 5
Submission
All file(s) required for your project should be in the folder
https://www.cs.usfca.edu/svn/<username>/cs112/Project2/
Hints
There is no one right way to do this assignment. Here are some
hints to get you started (you do
not
need to follow these hints)
- Do the project in stages. First, put together the panels
and buttons, then get number inputs correct, then do operators on just
2 numbers, then work on strings of operations.
- Consider storing (at least) the following 3 values as
instance variables: operand1, operand2, lastOperator. As a
number is enterred, modify operand2, When a operator is first
pressed, copy operand2 to operand1, and save the operator. When
the next number is enterred, save it to operand2. When an equals
(or second operator) is pressed, set operand1 = operand1
<savedOp> operand2
- Clever program decomposition can save you time.
- Start early! Ask for help as necessary
Extra Credit
If you want a greater challange, you can get up to 20 points of extra
credit by creating a calculator that can handle floating point numbers
and unary (that is, takes a single argument) operators. An
extra-credit solution would look something like:
You have some lattitude as to what extra buttons you can add for the
extra credit. See me for more information.