first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine\ instance.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ @return\ The\ value\ currently\ displayed\n\ on\ the\ calculator.\n
|
||||
comment10.params=
|
||||
comment10.target=void\ applyPreviousOperator()
|
||||
comment10.text=\n\ An\ operator\ button\ has\ been\ pressed.\n\ Apply\ the\ immediately\ preceding\ operator\ to\n\ calculate\ an\ intermediate\ result.\ This\ will\n\ form\ the\ left\ operand\ of\ the\ new\ operator.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n\ @param\ number\ The\ single\ digit.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ '+'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ '-'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ @return\ The\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ @return\ The\ author\ of\ this\ engine.\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\n
|
||||
numComments=11
|
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* The main part of the calculator performing the
|
||||
* arithmetic logic of the calculations.
|
||||
* @author Hacker T. Largebrain
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// The value in the display.
|
||||
private int displayValue;
|
||||
// The previous operator typed (+ or -).
|
||||
private char previousOperator;
|
||||
// The left operand to previousOperator.
|
||||
private int leftOperand;
|
||||
|
||||
/**
|
||||
* Create a CalcEngine instance.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
displayValue = 0;
|
||||
previousOperator = ' ';
|
||||
leftOperand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value currently displayed
|
||||
* on the calculator.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
* @param number The single digit.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
displayValue = displayValue * 10 + number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '+' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
applyPreviousOperator();
|
||||
previousOperator = '+';
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '-' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
applyPreviousOperator();
|
||||
previousOperator = '-';
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
if(previousOperator == '+') {
|
||||
displayValue = leftOperand + displayValue;
|
||||
}
|
||||
else {
|
||||
displayValue = leftOperand - displayValue;
|
||||
}
|
||||
leftOperand = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
displayValue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "Super Calculator";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The author of this engine.
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "Hacker T. Largebrain";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The version number of this engine.
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "version 0.2";
|
||||
}
|
||||
|
||||
/**
|
||||
* An operator button has been pressed.
|
||||
* Apply the immediately preceding operator to
|
||||
* calculate an intermediate result. This will
|
||||
* form the left operand of the new operator.
|
||||
*/
|
||||
private void applyPreviousOperator()
|
||||
{
|
||||
if(previousOperator == '+') {
|
||||
leftOperand += displayValue;
|
||||
}
|
||||
else if(previousOperator == '-') {
|
||||
leftOperand -= displayValue;
|
||||
}
|
||||
else {
|
||||
// There was no preceding operator.
|
||||
leftOperand = displayValue;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngineTester()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ CalcEngineTester\n
|
||||
comment1.params=
|
||||
comment1.target=void\ testAll()
|
||||
comment1.text=\n\ Test\ everything.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ testPlus()
|
||||
comment2.text=\n\ Test\ the\ plus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 3+4.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ testMinus()
|
||||
comment3.text=\n\ Test\ the\ minus\ operation\ of\ the\ engine.\n\ @return\ the\ result\ of\ calculating\ 9\ -\ 4.\n
|
||||
numComments=4
|
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Test the CalcEngine class.
|
||||
*
|
||||
* @author Hacker T. Largebrain
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CalcEngineTester
|
||||
{
|
||||
// The engine to be tested.
|
||||
private CalcEngine engine;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class CalcEngineTester
|
||||
*/
|
||||
public CalcEngineTester()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test everything.
|
||||
*/
|
||||
public void testAll()
|
||||
{
|
||||
System.out.println("Testing the addition operation.");
|
||||
System.out.println("The result is: " + testPlus());
|
||||
System.out.println("Testing the subtraction operation.");
|
||||
System.out.println("The result is: " + testMinus());
|
||||
System.out.println("All tests passed.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the plus operation of the engine.
|
||||
* @return the result of calculating 3+4.
|
||||
*/
|
||||
public int testPlus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the key presses: 3 + 4 =
|
||||
engine.numberPressed(3);
|
||||
engine.plus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 7.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the minus operation of the engine.
|
||||
* @return the result of calculating 9 - 4.
|
||||
*/
|
||||
public int testMinus()
|
||||
{
|
||||
// Make sure the engine is in a valid starting state.
|
||||
engine.clear();
|
||||
// Simulate the presses: 9 - 4 =
|
||||
engine.numberPressed(9);
|
||||
engine.minus();
|
||||
engine.numberPressed(4);
|
||||
engine.equals();
|
||||
// Return the result, which should be 5.
|
||||
return engine.getDisplayValue();
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
------------------------------------------------------------------------
|
||||
Super Calculator
|
||||
Author: Hacker T. Largebrain
|
||||
------------------------------------------------------------------------
|
||||
|
||||
This program implements a highly efficient calculating engine for
|
||||
a pocket calculator. It includes a complete test rig to demonstrate
|
||||
that the engine works as it should.
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=CalcEngineTester
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=694
|
||||
package.editor.height=435
|
||||
package.editor.width=586
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=564
|
||||
readme.editor.width=861
|
||||
readme.editor.x=33
|
||||
readme.editor.y=23
|
||||
target1.editor.height=706
|
||||
target1.editor.width=884
|
||||
target1.editor.x=36
|
||||
target1.editor.y=55
|
||||
target1.height=60
|
||||
target1.name=CalcEngineTester
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=140
|
||||
target1.y=80
|
||||
target2.editor.height=722
|
||||
target2.editor.width=904
|
||||
target2.editor.x=47
|
||||
target2.editor.y=30
|
||||
target2.height=60
|
||||
target2.name=CalcEngine
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=130
|
||||
target2.x=300
|
||||
target2.y=180
|
Reference in New Issue
Block a user