first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
#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=where
comment10.target=void\ reportState(java.lang.String)
comment10.text=\n\ Print\ the\ values\ of\ this\ object's\ fields.\n\ @param\ where\ Where\ this\ state\ occurs.\n
comment11.params=
comment11.target=void\ applyPreviousOperator()
comment11.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
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=12

View File

@@ -0,0 +1,151 @@
/**
* The main part of the calculator doing the calculations.
* This version incorporates debugging print statements.
* @author Hacker T. Largebrain (version 1.0)
* @version 1.1
*/
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.
*/
public void numberPressed(int number)
{
System.out.println("numberPressed called with: " +
number);
displayValue = displayValue * 10 + number;
reportState("end of numberPressed");
}
/**
* The '+' button was pressed.
*/
public void plus()
{
System.out.println("plus called");
applyPreviousOperator();
previousOperator = '+';
displayValue = 0;
reportState("end of plus");
}
/**
* The '-' button was pressed.
*/
public void minus()
{
applyPreviousOperator();
previousOperator = '-';
displayValue = 0;
}
/**
* The '=' button was pressed.
*/
public void equals()
{
System.out.println("equals called");
if(previousOperator == '+') {
displayValue = leftOperand + displayValue;
}
else {
displayValue = leftOperand - displayValue;
}
leftOperand = 0;
reportState("end of equals");
}
/**
* The 'C' (clear) button was pressed.
*/
public void clear()
{
System.out.println("clear called");
displayValue = 0;
reportState("end of clear");
}
/**
* 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";
}
/**
* Print the values of this object's fields.
* @param where Where this state occurs.
*/
public void reportState(String where)
{
System.out.println("displayValue: " + displayValue +
" leftOperand: " + leftOperand +
" previousOperator: " + previousOperator +
" at " + where);
}
/**
* 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()
{
System.out.println("applyPreviousOperator called");
if(previousOperator == '+') {
leftOperand += displayValue;
}
else if(previousOperator == '-') {
leftOperand -= displayValue;
}
else {
// There was no preceding operator.
leftOperand = displayValue;
}
reportState("end of applyPreviousOperator");
}
}

View File

@@ -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

View File

@@ -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 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();
}
}

View File

@@ -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.

View File

@@ -0,0 +1,45 @@
#BlueJ package file
dependency1.from=CalcEngineTester
dependency1.to=CalcEngine
dependency1.type=UsesDependency
objectbench.height=76
objectbench.width=700
package.editor.height=435
package.editor.width=592
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=515
readme.editor.width=853
readme.editor.x=30
readme.editor.y=23
target1.editor.height=753
target1.editor.width=885
target1.editor.x=50
target1.editor.y=47
target1.height=60
target1.name=CalcEngineTester
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=130
target1.x=130
target1.y=100
target2.editor.height=700
target2.editor.width=900
target2.editor.x=36
target2.editor.y=23
target2.height=60
target2.name=CalcEngine
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=130
target2.x=280
target2.y=200