Page 230 - Learn To Program With Scratch
P. 230

First, the user is asked to enter an answer in fraction form (for example,
                         3/5 or –7/8). The script then extracts the numerator and the denominator
                         of the answer string (which are separated by the division sign) and assigns
                         them to the num3 and den3 variables, respectively. For example, if the user
                         enters –23/15, num3 will be set to –23 and den3 will be set to 15. After that,
                         the script broadcasts a GotAnswer message to tell the Digit sprite to show the
                         user’s answer on the Stage. When the Digit sprite receives this message, it
                         stamps the digits of num3 and den3 at the correct positions on the Stage
                         in the same way it displayed the numerators and denominators of the two
                         operands. You can check the file FractionTutor.sb2 for the details.
                             After entering an answer, the user can click the Check button to see if
                         the answer is correct. The script for the Check sprite broadcasts a CheckAnswer
                         message to inform the other sprites of the user’s request. This message is
                         trapped and processed by the Teacher sprite, which will execute the script
                         shown in Figure 8-34.









                                                                    Compute the correct answer based
                                                                    on the current operation, which is
                                                                    determined by the costume number

                                                                   of the Operation sprite. The four
                                                                    procedures (Add, Subtract, Multiply,
                                                                    and Divide) compute the numerator
                                                                    (ansNum) and the denominator
                                                                    (ansDen) of the correct answer.


                                                                    Find the greatest common divisor
                                                                    (GCD) of ansNum and ansDen in
                                                                   order to put the answer in its
                                                                    simplest form.


                                                                   Reduce ansNum and ansDen by
                                                                    dividing both of them by the GCD.
                                                                   Give feedback to the user.
                         Figure 8-34: The CheckAnswer script

                             The current costume of the Operation sprite tells which operation pro-
                         cedure (Add, Subtract, Multiply, or Divide) to execute u. The operations
                         take num1, den1, num2, and den2 as inputs and set the values of ansNum and
                         ansDen, which represent the numerator and denominator of the correct
                         answer, respectively. The four procedures are shown in Figure 8-35.
                             After finding the answer, CheckAnswer needs to put it in its simplest
                         form. For example, 2/4 should be simplified to 1/2. To perform this reduc-
                         tion, the script first finds the greatest common divisor (GCD), also known
                         as the greatest common factor, of the numerator and denominator v.
                         (We’ll look at this procedure in a moment.)


           208   Chapter 8
                                           www.it-ebooks.info
   225   226   227   228   229   230   231   232   233   234   235