Java Reference
In-Depth Information
case
'C':
displayText.setText("");
//Step 4d
numStr1 = "";
numStr2 = "";
firstInput =
true
;
}
}
The method
evaluate
performs an operation and returns the result of the operation
as a string. This method also handles various exceptions, such as division by zero and
number format error (which occurs if one of the number strings is empty). The
definition of this method is:
Method
evaluate
private
String evaluate()
{
final char
beep = '\u0007';
try
{
int
num1 = Integer.parseInt(numStr1);
int
num2 = Integer.parseInt(numStr2);
int
result = 0;
switch
(op)
{
case
'+':
result = num1 + num2;
break
;
case
'-':
result = num1 - num2;
break
;
case
'*':
result = num1 * num2;
break
;
1
1
case
'/':
result = num1 / num2;
}
return
String.valueOf(result);
}
catch
(ArithmeticException e)
{
System.out.print(beep);
return "E R R O R: " + e.getMessage();
}
catch
(NumberFormatException e)
{
System.out.print(beep);
Search WWH ::
Custom Search