Article
Application: Integer Calculators
Although a program may differ in appearance across platforms, the core logic of the
program remains the same. For instance, the same application looks entirely different
on a mobile device than on a desktop computer. Mobile devices have small screens and
may use any combination of arrow keys, number pads, QWERTY keyboards, and
touchscreens. Desktop computers, on the other hand, have much larger screens and
consistently use a full keyboard and a mouse. As a result, the layout of information and
buttons in the same application must differ to better accommodate different platforms.
In this assignment, you will write two integer calculators, one that uses a provided
graphical user interface (GUI), and another that uses a text-based interface. Although
the two interfaces will look different to the user, the code you write to perform
calculations is exactly the same.
As a general rule, methods should not both perform computations and also handle input
or output (I/O). I/O is more difficult to unit test, and tests should be streamlined to be
fast and easy to run. Keeping computation and I/O methods separate also allows the
model (the part of the program that performs the “significant” computation) to be
available for other, possibly unforeseen, uses.
A design pattern is a general-purpose organization tool for writing certain kinds of
programs. The Model-View-Controller (MVC) pattern is a commonly used design pattern
for programs with GUIs. The model is the nuts and bolts of the program. In the case of
this assignment, it is the code that carries out calculations after the user gives the
operands and operators. The view refers to how the information from the model is
displayed to the user. In this assignment, the view is the field that displays the
operands, operators, and the final answer. The controller is the code that allows the
user to tell the program what to do. In this assignment, the controller includes all of the
calculator “buttons” that the user must press to tell the model which operators and
operands to use. The MVC pattern strictly segregates, often in different classes, the
model, the view, and the controller. In small programs, the view and the controller are
often combined, but the model is always kept separate from them. In this assignment,
the GUI will provide the view and the controller; your task is to provide the model.
The code that implements the GUI for the Integer Calculator is in the file
Calculator.java. Perform the following steps to create a new NetBeans Project with
Calculator.java as the main class.
Download Calculator.java to your computer.
Create a new NetBeans Java Application Project
At Step 2. Name and Location of Project creation:
o Name the Project IntegerCalculator
o Make sure to uncheck the Create Main Class box
Once IntegerCalculator appears in the Project window of NetBeans, drag-and-
drop Calculator.java into IntegerCalculatorSource Packages within the
Projects pane of the NetBeans window.
Make Calculator the main class (You can do this from the Run node of the
Project Properties window. Right-click on the IntegerCalculator project and
select Properties to access this window).
Calculator.java will appear in the default package subdirectory of
IntegerCalculatorSource Packages. It will display with a red stop sign icon with an
exclamation point: This is because it refers to the
CalculatorLogic class, which doesn’t yet exist. You will create CalculatorLogic;
this class will implement all of the calculator logic and functions. Once this class is
defined, the red stop sign with the exclamation point will disappear.
Here is an example that demonstrates most of the required logic in the calculator.
Suppose the user starts with a blank display, and enters 16+2-5=.
'1' This character is only part of a number; save it in a String variable called
numberString.
'6' This is the next part of a number. Append it to numberString.
'+' This marks the end of the first number.
o Convert numberString to an int and save it in a variable called
operand.
o Save the '+' in a variable called pendingOperation.
o Reset numberString to the empty string.
'2' Append '2' to numberString.
'-' This marks the end of the second number.
o Convert numberString to an int.
o Because pendingOperation is '+', convert the second number to an
int and add it to operand.
o Save the '-' in a variable called pendingOperation.
o Reset numberString to the empty string.
'5' Append '5' to numberString.
'=' This marks the end of a number.
o Convert numberString to an int.
o Because pendingOperation is '-', convert the second number to an
int and subtract it from operand.
o Save the '=' in a variable called pendingOperation.
o Reset numberString to the empty string.
Notes:
Convert a string to an integer with Integer.parseInt(string). If the string
contains illegal characters, a NumberFormatException occurs; you must
either catch this exception or ensure that it never happens.
There is no shortcut way to convert a character representing an operation, such
as '+', to an actual operation. Use either a switch statement or a sequence of
if-else statements.
Do not perform any I/O within the CalculatorLogic class. Instead, the class
must contain a method String process(char input) that takes a char
representing a button press on the calculator, and returns a string to be
displayed.
The process method must return the string “Error” if the user attempts to
divide by zero. The calculator should do nothing until the C (clear) button is
pressed.
Thoroughly test the process method and any helper methods that it calls by
using the TDD approach of writing the tests first. It is better to write many small
tests, with meaningful names, than to write one huge test.
To simplify the assignment, perform operations in the order in which they occur.
For example, given 2+3*4=, do the addition before the multiplication. You do not
have to support parentheses for grouping.
The supplied Calculator class contains the main method and sets up the GUI
for you. It will use your class to create a CalculatorLogic object and call its
process method as appropriate.
Here are the characters the process method should expect from the GUI:
The digits 0 through 9.
+ - * / To add, subtract, multiply, and divide, respectively.
= To compute and display the current result.
c To clear the entire calculator (and start all over again).
e To clear an entry (reset the current operand to the empty string).
n To change the sign of the current operand (multiply by -1).
Think carefully about what to do for each new character (button press) that occurs. The
action will be different depending on whether you are building a new number (already
have some digits) or are between numbers. Avoid repeating code; if you have to do the
same thing in more than one place, put it in a method.
Sometimes you will want to use a program in a different environment. For example, a
calculator could be used on a web page or on a smartphone with a small screen. If your
model class does all of the significant computations and if it does no input/output (which
ties it to a particular environment), it will be much easier to reuse.
When you have the calculator GUI working, write a class called TextCalculator, also
containing a main method to provide a text-only interface to the CalculatorLogic
class. You can use a java.util.Scanner to read in lines from the user, pass legal
characters to the process method (skipping over blanks and any other improper
characters), and print the final result from each line. Provided you have properly isolated
your model class and kept it I/O free, this class will be very simple to write.
As part of your submission, include screenshots of the program running using the GUI
as well as using the text-based interface. For each interface, include screenshots
showing one calculation for each of the four operators and one calculation that uses all
of the operators. For one of the interfaces, include two screenshots showing how your
program handles a user placing two operators next to each other and two screenshots
showing how your program handles a user placing no operators in the calculation. Show
screenshots of your program being tested with JUnit.
Save your NetBeans Project and screen shots of the working program as a ".zip" file.