CS 10, Spring 1998
Review for Practical Exam

Use this discussion period to practice scripting in preparation for the practical exam on Thursday. Be sure to ask any questions you have about scripting in Hypertalk.

Work on any of the following exercises:

Labs

1. Complete any of the labs which you haven't finished. By now, most of these labs should seem easy. In particular, you should feel very comfortable using put, if, and repeat commands in Hypertalk. The bulk of last year's practical consisted of lab 4.3. You may want to try redoing this lab. All the labs can be found on the class web site: http://www.cs.swarthmore.edu/~mirarchi/spring98

Easy Exercises

1. Design a card to compute the circumference and area of a circle whose radius the user specifies. Create a field where the user can input a value for the radius. Then create a button which will display in another field the circumference and area of the circle with the given radius. (Recall that circumference = pi * diameter, and area = pi*radius2. Use 3.14 for pi) Use the graphics tools to draw a circle on your card and put the field where the user inputs the radius on the radius of the circle you drew.

2. Design a card that will convert from inches to centimeters. Create a field where the user can input a value in inches. Create a button which will display in a second field the value of the first field converted to centimeters. (1 inch = 2.54 cm)

Medium Exercises

1. Design a card which will calculate factorials (ex. 5! = 5*4*3*2*1). Create two fields on your card, a field for the user to enter a number, and the other to display the value of the factorial. Create a button which will compute and display the value of the factorial.

2. Design a card which will behave like the "Find/Replace" facility of word-processors. The card should contain one field in which the user can enter some text. It should contain two other small fields-- one in which the user can enter a word which she wants replaced, and another in which the user can enter a replacement word. Create a button which will replace each occurrence of the specified word with the specified replacement.

3. Design a card which will allow the user to write a sentence, and which will reverse the order of the words in the given sentence. This will require at least 2 fields-- one field in which the user can specify a sentence, and one field which will contain the new sentence. Write a button which will reverse the order of the words in the specified sentence. So if I type "you are kidding" in the input field, and click the button, then the results field should contain "kidding are you". (You can assume the input sentences won't contain punctuation.) Hint: Go through each word of the input field and put each word before what is already in the output field.

4. Recall that in mathematics, the intersection of two sets A and B consists of only those elements of A which are also elements of B. Design a card with three fields (we'll refer to them as field 1, field 2, and field 3). We will think of each field as representing a set (a set is just a collection of objects). The elements of the set are simply the words in the field. The user enters a bunch of words in field 1 and a bunch of words in field 2. Create a button which will display in field 3 only those words which appear both in field 1 and field 2 (i.e. this button computes the intersection of fields 1 and 2). Hint: this will require a repeat loop within a repeat loop.

Harder Exercise

The exam will consist of a programming exercise similar in difficulty to one below.

The goal is to create a card which translates text into Morse code.

  1. First, create a field which will contain all the letters of the alphabet, one letter per line. Each line will also contain the code corresponding to the given letter. Call this field "codes". Your "code" field should contain the following data:

    A	.-
    B	-...
    C	-.-.
    D	-..
    E	.
    F	..-.
    G	--.
    H	....
    I	..
    J	.---
    K	-.-
    L	.-..
    M	--
    N	-.
    O	---
    P	.--.
    Q	--.-
    R	.-.
    S	...
    T	-
    U	..-
    V	...-
    W	.--
    X	-..-
    Y	-.--
    Z	--..
    

    To save yourself retyping this, you can copy and paste from the electronic copy of this Review Sheet: http://www.cs.swarthmore.edu/~mirarchi/spring98/discussion/practical.html

  2. Create two new fields. Call the one field "input" and the other field "results". Create a button called "translate"

  3. Start by writing a script for the translate button that will translate a single letter to Morse Code. The user should be able to enter a single character in the input field, and the translate button should display the corresonding code in the results field. Hint: your script should go through each line of the codes field until it finds a line whose first letter matches the letter which the user specified. It should then display the second letter of this line in the "results" field.

  4. Modify the script of this button so that it translates a single word into morse code. For example, if the user enters the word "howdy" in the input field, the translate button should display ".... --- .-- -.. -.--" in the results field. Hint: you already have a script that translates a single letter to morse code. Use this script to go through each letter of the input word and translate that letter to morse code.

  5. Now create a message handler which will translate a word into morse code. This message handler will have one parameter, which will specify the word to be translated. So it will look like:
    	on translateWord wordToTranslate
    
    		<body>
    
    	end translateWord 
    
    The body of this script will be almost identical to the body of the translate button script, so start by copying and pasting the button script into the script for this new handler.

  6. Once you've written the script for the handler, modify the script of the "Translate" button so that instead of doing the translation itself, it uses the translateWord handler to translate the specified word. Hint: try the script below:
    on mouseUp
    
    	put word 1 of card field "input" into whichWord
    	translateWord whichWord
    
    end mouseUp
    

  7. Now modify the script of the translate button so that it translates an entire sentence (without punctuation) into Morse Code.

  8. As an exercise to illustrate the difference between a message handler and a function handler, rewrite translateWord so that it is a function. Recall that the major difference between a message handler and a function handler is that a function handler returns a value (think back to the makeDecimal function which we wrote to convert binary numbers to decimal numbers-- the value which this function returns is the decimal value of the binary number). In the case of the translateWord function, it should return the Morse Code version of the word. The skeleton of your function handler will look like:

    function translateWord whichWord
    
    	<body>
    	return <some variable which contains the Morse Code translation>
    
    end translateWord
    

    Hint: The body of this function will be almost identical to the old version of translateWord. The only difference is that instead of displaying the translation in the results field, the function translateWord will store the translation in some variable, and then return this variable.

  9. Once you've written the function, you can test it from the message box by typing
    translateWord("test")

    When you've got the function working, modify the script of the translate button so that it uses the function handler to translate each word of the input field into morse code. Hint: For each word in the input field, you'll want to put the value that translateWord returns after whatever is already in the results field.