Actions

The first group of problems that allow you to practice building an accessible user interface.

The Main Action

The main action is where every program begins. Add the output blocks to the matching action. Before running the program, predict what the program will output. Did your prediction match the outcome?

Defining an Action

Define an action named PrintMenu in the existing program. Then add the output statements to this action so that the program outputs the following dinner menu.

Dinner Menu
Chicken Pot Pie.....$15.95
Chicken Parmesan.....$17.50
Vegetarian Chili.....$11.00
Hamburger and Fries.....$18.95
Chef Salad.....$13.50

Calling an Action

Calling an action tells the program to run the code in the action that is called. Call the PrintMenu action twice by typing the action name followed by a left and right paren. When you ran the program did the program output the menu twice?

Actions with a Parameter

In this task, revisit the letter grade program. This time, the program should use an action, GetLetterGrade, with one parameter: an integer score. Program the action so that it converts a score to a letter grade. Then call the action and pass it a argument value. Use the existing blocks to build this program.

Actions with Multiple Parameters

In this task, all three call blocks for the CompareNumbers action are called in the Main action. Build the CompareNumbers action so that it compares the a and b parameters. A finished program will output the following.

14 is smaller than 21
22 is equal to 22
73 is larger than 54

Actions with a Return Value

Building off the previous program, switch the action to return the message as text. Instead of outputting the text right away, the action will return the text so that the values are output in the Main action. You should expect the same output.

14 is smaller than 21
22 is equal to 22
73 is larger than 54

Calling Actions with a Return Value

In this task, you will call an action with a return value and store that value in a variable. The action you will call is the IceCreamConeRating. Read the action signature, action IceCreamConeRating(text flavor, integer scoops) returns text, to help choose what parameter values to send the action. Do the same thing to help choose which variable to assign the returned value to. Finally, add the output block and change it so it ouputs the value returned from the action.

Programmers often name their variables poorly. In this case, parameter1 gives you no info about the value to send the action. Use types to help decode this action.

Write an Action

Bring together all the parts of programming actions to create a zombie mood meter. This action will take an integer value representing the number of hours since the zombie has eaten. Then, it will return text describing the zombie's mood. The action will have a signature like the following: action ZombieMood(integer hoursSinceEaten) returns text.

Inside the action scope, if the number of hours since eating is less than three, return the text "Calm". If the number of hours is three to six hours, return the text "Hungry". If it's been more than six hours, return "Raging".

Once you have written the action, call the action three times in Main with the hours set to 2, 6, and 9. Then, output your zombie's mood results. A working program will have an action defined, and the output will match the following program output.

Zombie Mood: Calm
Zombie Mood: Hungry
Zombie Mood: Raging

Math Actions: Add

For this task and the next five, build up a math library with a collection of actions. For this problem, build an Add action that has two integer parameters and an integer return value, action Add( integer a, integer b ) return integer. Then call the action, integer result = Add(3, -9), with the arguments 3 and -9. Finally, output the result. When run, it should output -6.

Math Actions: Subtract and Multiply

For this problem, write an action for Subtract and Multiply. Start by adding two action blocks and rename each Subtract and Multiply. Each has two integer parameters and an integer return value. Once the actions are built, call each action with the arguments 3 and 5(e.g. result = Subtract(3, 5)). Then, output the results. When run, the program should output -6, -2, and 15.

Math Actions: Divide

For this problem, add a Divide action with two number parameters and a number return value for this problem. In addition, you will need to do some error checking to check if the divisor is zero. If it is zero, output a message Error, cannot divide by zero. and return 0.0. Otherwise, return the division result.

Once you have completed the action, call the action with the arguments 34.0 and 2.0(e.g., otherResult = Divide(34.0, 2.0)). Then, output the results. Call the action a second time with arguments 34.0, 0.0 and output the result. When run, the program should output the following.

-6
-2
15
17.0
Error, cannot divide by zero.
0.0.

Math Actions: IsEven

For this problem, add an IsEven action with one integer parameter and a boolean return value for this problem. Use mod to determine if the parameter is even or odd. If the parameter is even, return true and if it's odd, return false.

Once you have completed the action, call the action with the argument 1902(e.g., booleanResult = IsEven(1902)). Then, output the result. When run, the program should output the following.

-6
-2
15
17.0
true

Math Actions: AbsoluteValue

For this problem, add an AbsoluteValue action with one integer parameter and a integer return value. Once you have completed the action, call the action with the argument -78 (e.g., result = AbsoluteValue(-78)). Then, output the result. When run, the program should output the following.

-6
-2
15
17.0
true
78

Math Actions: Power

For this problem, add a Power action with two integer parameters and an integer return value. Once you have completed the action, call the action with the arguments 3, 2(e.g., result = Power(3, 2)). Then, output the result. When run, the program should output the following.

-6
-2
15
17.0
true
78
9