Complete addition of print statements to each method and constructor, to the sup
ID: 3624156 • Letter: C
Question
Complete addition of print statements to each method and constructor, to the supplied code.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");
}
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();
}
}
Explanation / Answer
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()
{
System.out.println("Constructor called");
displayValue = 0;
previousOperator = ' ';
leftOperand = 0;
}
/**
* Return the value currently displayed
* on the calculator.
*/
public int getDisplayValue()
{
System.out.println("getDisplayValue called");
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()
{
System.out.println("Minus function called");
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()
{
System.out.println("getTitle function called");
return "Super Calculator";
}
/**
* Return the author of this engine.
*/
public String getAuthor()
{
System.out.println("getAuthor function called");
return "Hacker T. Largebrain";
}
/**
* Return the version number of this engine.
*/
public String getVersion()
{
System.out.println("getVersion function called");
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("reportState called");
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");
}
public class CalcEngineTester
{
// The engine to be tested.
private CalcEngine engine;
/**
* Constructor for objects of class CalcEngineTester
*/
public CalcEngineTester()
{
System.out.println("CalcEngineTester class constructor called");
engine = new CalcEngine();
}
/**
* Test everything.
*/
public void testAll()
{
System.out.println("testAll function called");
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()
{
System.out.println("testPlus function called");
// 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()
{
System.out.println("testMinus function called");
// 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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.