Copy the lab materials to your account.
cp -r /home/Classes/Cs1/Labs/Lab09 .Everything you need for the lab exercises today is contained in this new directory.
Change directory to Lab09.
Compile and run the calculator. It is a very simple one. You can enter single digit operands and perform only one operation (+, - , *, /). There should be no spaces in the text that you enter. For example, you can enter "3+4". Try this. What do you get? Obviously something is wrong.
For the first checkpoint, figure out what is wrong.
Hint: Find out what getOperand1() and
getOperand2() are returning.
1
Explain to us what is wrong with the calculator.
For the second checkpoint, fix the problem. Hint: Look at the ASCII code table in Chapter 9.1 of your text and observe that the ASCII code for '8' (for example) is 8 greater than the ASCII code for '0'.
2
When you have corrected the problem, show us your code and working program.
Go into subdirectory Calculator2 and edit the class Calculator. This program looks similar to the previous version except that the getOperand... methods have been replaced by a single getOperand() method that returns the next operand in the input expression. This code is similar to the code for extracting words in section 9.2 of your text, except that we are extracting numbers instead.
Compile and run this program. You should be able to enter expressions such as
15/2 42* 55 843- 511 11+ 22with multi-digit operands instead of the single-digit operands required in the previous version. Try these expressions, entering them exactly as they appear, including spaces.
Observe that these expressions all result in correct values.
Now see what happens when you try the following expression:
11 + 22where there is a space before and after the +.
3
Explain exactly what situations can produce this error
and why the error occurs.
Fix your program so that this error is eliminated. Change only the getOperator() method. Remember that all you need to do is skip spaces in the expression until you get to the operator.
4
Show us your code and your working program.
Right now, your program should work if you enter an expression that is in the format number operator number, where there can be any number of spaces around the numbers.
Run your program with the expression
1+2+5and observe that the +5 part of the expression is ignored. Why?
Now run your program with the expression
a+band observe that your program fails. Why?
We want to check the validity of our input expression before we even try to evaluate its operands and get the operator. To do this, we want to use the LEGAL_EXPRESSION pattern to check for correct input. Right now, LEGAL_EXPRESION will match any input line, since the regular expression .* matches any string.
Redefine LEGAL_EXPRESSION so that it matches only strings of the form number operator number where number is any sequence of one or more decimal digits preceded or followed by any number of spaces, and operator is one of the symbols - + * /. Hint: make sure the - symbol comes first in the part of your regular expression that checks the operator.
Make this change to your program. You should now be able to enter illegal expressions and have the text box display an error message. Legal expressions should evaluate as usual.
Compile and run your program.
5
Show us your definition of LEGAL_EXPRESSION
and your working program.
So far, your formatExpression() method simply returns the expression string unmodified. We want to clean the input expression so that, when it is output, all the spaces have been removed. For example, with input
11 + 33your output should look like
11+33=44Modify your formatExpression() method to return a string that is the same as the original expression with all the spaces removed. Create an empty StringBuffer, and go through the exp string, character by character, appending non-blanks to the StringBuffer object and ignoring blanks. Use the toString() method to return a String result.
Compile and run your program and test it with several input strings (including ones that are in error).
6
Show us your code and your working program.
If you use formatExpression() to remove blanks in expression before checking to see if the expression is legal, you can simplify the regular expression LEGAL_EXPRESSION by removing all the \s* patterns. Make this change by
7
Show us your modified code and your working program.
8
Show us your code and your working program. Be prepared to explain
why the original code did not work.