CS 10, Spring 1998
Lab 4.3 Control Structures in HyperTalk
The purpose of this lab is to give you an understanding of how to use the "if ... then" and "repeat" control structures to write more complex Hypertalk scripts.
Assignment for the Next Lab
- Reread pp. 144-158
- Read pp. 159-165
- Answer the Review Questions on page 158. You will not have to hand this in, but you should come to next lab prepared to discuss these questions and ask about anything you don't understand.
- For Tuesday's discussion, view video 3 of the Machine That Changed The World.
Lab 4.3 Instructions
- Begin by opening a new stack. This stack will consist of a single card. Create three new fields. Call these three fields "text," "input," and "results".
- Resize the "text" field so that it is large enough to hold about a paragraph of text.
- Above this "text" field, add a new field which will contain the following instructions to the user: "Enter some text in the field below:".
- Resize the "input" field so that it is only one line long. Above the "input" field, add a new field which will contain the following instructions to the user: "Enter a word in the field below:".
- Now create a button called "Count it!" Write a script for this button that will count and display the number of times the word in the "input" field appears in the "text" field. The user should be able to enter any amount of text in the text field, and any word in the input field. So for example, if the text field contains "Oh what a beautiful morning, oh what a beautiful day," and the input field contains "beautiful," then after the user clicks on "Count it!", the "results" field should say "The word beautiful appears 2 times in the text ." (Note: dealing with punctuation makes this problem more difficult. Don't worry about punctuation for now).
- Next, create a button called "Reverse it!" Write a script for this button that displays in the "results" field the reversal of the word in the "input" field. So if the user entered "swarthmore" in the "input" field, then after the user clicks the "Reverse it" button, the "results" field would contain "eromhtraws." (Hint: First clear the display field. Then go through each character of the input word, from left to right, and for each character, put that character before what is already in the display field.)
- Finally, create a third button called "Palindrome?" Write a script for this button that will display in the "results" field a sentence indicating whether or not the word in the "input" field is a palindrome (a word that is the same when you reverse it). So, for example, if the input word is "swarthmore," then clicking on the "Palindrome?" button should display "The word swarthmore is not a palindrome" in the "results" field. On the other hand, if the input word is "madam," then clicking on the "Palindrome?" button should display "The word madam is a palindrome" in the "results" field. One way of solving this problem is for your button script to perform the following steps:
- Start by reversing the word contained in the "input" field. For now, you can accomplish this by copying and pasting the script of the "Reverse it!" into the script of the "Palindrome?" button. (Later, we'll learn an easier way of doing this). This will display in the "results" field the reversal of the word contained in the "input" field.
- Now compare what's in the "input" field with what's in the "results" field.
- If the two are equal, display a message indicating that the word is a palindrome. Otherwise, display a message indicating that the word isn't a palindrome.
- When you have finished, call one of us over and show us your stack.