CS 1 Labs: Lab 9

CS 1 Labs: Lab 9

To accompany Chapter 9 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

Primitive chars and Classy Strings

Read all of these instructions before beginning the exercises.
There are 9 checkpoints in this lab. If you need help with any exercise, raise your hand.

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.


You will use the same program for this lab exercise, so please get each checkpoint checked before proceeding to the next step.

Converting Characters

Go into the subdirectory Calculator and edit the class Calculator. This program is not yet complete, and your job in this lab is to complete it.

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.

Going through Strings char by char

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+ 22
with 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 + 22
where 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.

Checking for Legal Expressions

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+5
and observe that the +5 part of the expression is ignored. Why?

Now run your program with the expression

a+b
and 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.

Reformatting the Expression

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    + 33
your output should look like
11+33=44
Modify 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.

Exploiting Our Work

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

  1. assigning formatExpression(expression) to expression immediately after getting it from the JTextField, and
  2. simplifying the LEGAL_EXPRESSION pattern as described above.
In your output box, you will not need to call the formatExpression() method again; just use the already-formatted value of expression.

7 Show us your modified code and your working program.

Quitting Your Program with q

Right now, when you enter the expression "q" into your calculator, you get the Error in expression message. Yet the code (see statement under "Ignore until Checkpoint 7") is intended to check the input expression and to exit the program if it is "q". The program is not working as intended. Why? Fix this problem.

8 Show us your code and your working program. Be prepared to explain why the original code did not work.

After the Lab

9 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.
Susan Haller and Timothy Fossum, University of Wisconsin-Parkside