<< Chapter < Page Chapter >> Page >

Various numeric representations

The block labeled sqrt will deliver about a dozen different representations of numeric values, depending on what is selected from thepull-down list. This includes the square root, trigonometric functions, absolute value, etc.

Rounding a number

Finally, the block labeled round will round a decimal number to the nearest whole number.

Drag and drop the plus and minus operators

Image 4 shows the result of dragging the top two green blocks from the left panel and dropping them into the white boxes in the bottomtwo orange variable blocks in the center panel of Image 3 .

Image 4. result of dropping addition and subtraction operators into variable blocks.

Missing image.
Image 4. Result of dropping addition and subtraction operators into variable blocks.

A few more steps are required

If you compare Image 4 with Image 2 , you will see that Image 4 is close to what we need but we aren't quite there yet. In order to causethe bottom two blocks in the center panel in Image 4 to match the bottom two blocks in Image 2 , we need to do the following:

  • Click the orange Variables button in the upper left of Image 4 to expose the variables as shown in Image 1 .
  • Drag the blocks for the variables named LeftOperand and RightOperand from the left panel and drop them into the white boxes in the bottom two blocks in Image 4 to make them match the bottom two blocks in Image 2 .

That's the solution. The upper left portion of your Stage area should now look similar to Image 5 . Note however that you may need to use the mouse to arrange the four variables and the button to get your Stage arrangedlike Image 5 .

Image 5. stage area of the finished program.

Missing image.
Image 5. Stage area of the finished program.

Operation of the program

Once you reach this point, you can click the green flag to initialize all four variables to zero.

Then you can move the sliders back and forth to manually set the values for the variables named LeftOperand and RightOperand .

Then when you click the button at the bottom of Image 5 , the variable named Sum will display the sum of the values of the top two variables, and the variable named Diff will display the value of LeftOperand minus the value of RightOperand .

An online version of this program is available

A copy of this program has been posted online for your review (see Resources for the URL) . If you don't find the program using that URL, search the Scratch site for the usernamed dbal.

Run the program

I encourage you to use the information provided above to write this program. Experiment with the code, making changes, and observing the results of yourchanges. Make certain that you can explain why your changes behave as they do.

Just for fun, move the button a little to the right and cause it to say Ouch for about five seconds each time you click it as shown in Image 6 . Hint: See the purple button labeled Looks in Image 3 .

Image 6. making the button say ouch.

Missing image.
Image 6. Making the button say ouch.

I also encourage you to write the program described below.

Student programming project

Write a Scratch program named Arithmetic02 that produces the output shown in Image 7 when the user adjusts the sliders to the values shown and clicks the button. Make the word Ouch! appear and then go away after about five seconds.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask