Magic Math Program - using the PUT command and the Label and Text fields
We use computers because they do complicated calculations and solve problems very quickly. Here we will get some practice using the computer to solve problems for us.
So far, we have written programs with pop-up Dialog boxes. You may have discovered that it gets tedious filling them in and always clicking to them.
We are going to learn an easier way by typing on the screen directly. We will be filling in "Text" fields on the screen. We will also be using "Label" fields to add labels to the screen
Then when the user presses the "Done" button, we will get that info from the screen and save it in our own variables using the "Put" command. We can then do our own calculations and then "Put" the answers back on the screen.
Here is a diagram of how we use variables and the "PUT" command to move them on and off the screen. Here we move the user's name, grade and score off the screen and Put them into variables for us to use. - the user's name goes into the variable tName
- the user's grade goes into the variable tGrade
- the user's score goes into the variable tScore
Note: we have to be careful in naming our variables. We can not call our variable "name" because that is already a property - the "name" of an object. To be safe, put a "t" in front of your variable name and you can be sure it will not be the same as any property
We add 20 to the variable - tScore and put it back onto the screen
Simple Calculations
Create a simple program that will do some math for us. The user will give us 2 numbers, we will save those numbers in variables and then do some math with them. Afterwards, you will write a program of your own.
1. Practice - This is what we are going to make:
A Program that will add and multiple any 2 numbers that the user types: (their sum and product)
In the graphic below, we get two numbers from the user - Number 1 (field "num 1") and Number 2 (field "num 2"): then we give them those numbers added together and multiplied together
The following code is on the "Done" button: (It gets the numbers that they typed in and saves them as "x" and "y". Then it puts the "sum" and the "product" of those numbers back on the screen
on mouseUp put field "num 1" into x // this takes the number on the screen and puts it into a variable called "x" put field "num 2" into y // this takes the number on the screen and puts it into a variable called "y" put x+y into field "sum" // this adds the 2 variables (x + y) and puts it on the screen into "sum" put x*y into field "product" // this multiplys the 2 variables (x * y) and puts it into "product" end mouseUp
When they press the "Done" button they get the answers
2. Instructions - Step by step - This is how to do it:
For a simple exercise, implement the above program
Create a new main stack and using save it as "Magic Math":
- Click on "File", "New Mainstack"
- Click on "Object" , "Stack Inspector" , in the "Name" field change "Untitled" to "Magic Math"
- Click o "File", "Save as" and save it. (to the "Desktop", your G-Drive or your flash drive)
Set up the Labels on the screen: - Drag over a "Label" field and a "Text Entry" field (they are side by side on the ("Tools" palette)
- Click on the "label" field, and open up it's Inspector (Double-click on it or right-click and pick Property Inspector)
- go to it's "Contents" and type - Number 1
- click anywhere else to have it take effect
- go to it's "Text Formatting" properties and make it bigger (you may have to resize the field by dragging a corner of it)
Now we will set up the text entry field the way we want it: - click on the Text" field and drag one corner to make it square and larger
- open it's Property Inspector and name it "num1"
- in the "text Formatting" set the size to be "24"
Now we will have those two set up like we want, we will make copies of them and use the copies for the rest of the labels and text fields: - hold down the Control-key ( the one with "Ctrl" on it) and drag off copies of each
- Click on the top label and open up it's Property Inspector go to the Contents properties
- change it's contents to "Magic Math"
- leave the Inspector up and click on each label field and change it's contents
- when done, it should look like this:
Do the almost same for the "text entry" fields. - click on the first box, it says the name is
- click on the next one and change it's name to "Num 2"
- change the name of the other 2 to "Sum" and "Product"
- you should see the following:
Now drag over a button, open its "Property Inspector and name it Done
Add the following code to your button' code/script, format it as you like
You can add more graphics and change the properties to make it look more professional.
3. Practice - Add the following extras to your program:Add a "Reset" Button You may want to add a "reset" button to clear out all the fields Now to see if you understand how to do it with the following code: put empty into field "num 1"
put empty into field "num 2"
put empty into field "sum"
put empty into field "product"
end mouseUp

Finally, add Multiply and Divide (product and division of the 2 numbers)
Now to see if you understand what we just did - add the result of subtracting and dividing the two numbers to the screen
|