Chapter 3.4 - Paint Pot

Time Estimate: 90 minutes

3.4.1. Introduction and Goals

This lesson suggests several small programming projects for enhancing the Paint Pot app. Hints and suggestions are provided.

Objectives: The objectives of this lesson are:

  • to continue learning how to navigate the Thunkable online programming platform.

  • to deepen your understanding of event-driven programming.

  • program additional events into an existing mobile app;

  • learn to solve simple programming problems.

Getting Ready

Open your PaintPot project in Thunkablearrow-up-right from the previous tutorial.

Paint Pot Projects

Here are some creative projects. Work through the following enhancement ideas.

You are encouraged to discuss your ideas for how to solve these problems with the instructor and your classmates.

  1. Add a button to support a 4th color option for the app.

  2. Add a custom image to the app instead of using the cat image that is provided. Use the image as the Canvas Stage background image. Make sure the image doesn’t have any copyright restrictions.

  3. The app currently has a ButtonPlus and ButtonMinus. But, we only completed the code for ButtonPlus. Now that you understand how to increment variables by 1, implement an algorithm for ButtonMinus that will subtract 1 from dotsize.

  4. If/Else Exercise: In computer programming, a bug is an error or defect that prevents the app from working the way it is supposed to. In this app, if the user continues to press ButtonMinus, the value of dotsize will eventually become negative. If dotsize is negative, what will be drawn when the user touches the screen? Try fixing this bug by adding an if/then algorithm to the ButtonMinus block. (Hint: The If/Then block is found under Control in the Toolbox. If you’ve done the I Have A Dream Part 2 lesson, you’ve already seen how to use an if/else block.)

  5. Add a block to your When ButtonPlus Click and When ButtonMinus Click events to change the Stage’s Drawing Width. You will want to set it to a multiple of the dotsize. (Hint: I have found 8 times the dotsize to be an appropriate size that works for me. You may need to experiment to find the multiplier that works for you.)

  6. Optional: Currently, if a user wants to use a larger dot and then use a smaller dot (or vice versus), they have to continually press the ButtonMinus (or ButtonPlus). If the current value of dotsize is 25, getting to 4 would be a pain. For easier use, add a button that resets the size of the dot (circle) back to 5. (HINT: You’ll need a second variable here to remember the original value of the dotsize.)

  7. Optional: Create one or more of your own enhancements for your app. Remember to write your ideas down in pseudocode before you begin programming.

AP CSP Pseudocode: If Statements

Selection with if statements is used in every programming language. The AP CS Principles Exam uses a text-based and a block-based pseudocode for questions that involve code. The AP CSP reference sheetarrow-up-right is provided during the exam describing this pseudocode. The table below compares AP CSP pseudocode to MIT App Inventor blocks for if statements and relational operators. Note that the curly brackets { } are used in AP text pseudocode (and in many text-based programming languages) to indicate the start and end of a block of code.

The relational operators (=, ≠, <, >,<=, >=) are used inside if statements to compare variables with values or mathematical expressions, and they evaluate to a Boolean (true, false) value. For example, a = b evaluates to true if a and b are equal; otherwise, it evaluates to false. The logical operators NOT, AND, and OR can be used to combine conditions inside an if statement and also evaluate to a true or false Boolean value.

If you have trouble telling < and > apart, think of a number line and think of < and > as arrows; < (less than) points towards 0 and smaller numbers on the number line and > (greater than) points towards the larger numbers on the number line.

Function

Text Style

Block Style

MIT App Inventor

Selection (else optional)

IF (condition)

{

block of statements

}

ELSE

{

block of statements

}

IF condition

block of statements

ELSE

block of statements

Relational Operators

a = b

a ≠ b

a < b

a > b

a <= b

a >= b

Logical Operators

NOT(condition)

(condition AND condition)

(condition OR condition)

We usually use if/else blocks to make a two way choice, but you can make a three way or even an unlimited number of choices with nested else if statements. In MIT App Inventor, use the blue gear sign at the top of the if block and drag in as many else-if's as you need and end with an else block. For example, the block below will print out "Excellent" if your score is greater than 10, "Good job!" if your score is greater than 5, or "Keep Trying". If the first condition is false (for example if score is 2), it will fall down to the next condition and so on until it reaches that last else.

In addition, some conditional statements can be written as equivalent Boolean expressions or vice versa. For example,

answer ← (x > 0 AND x < 10)

is equivalent to

IF (x > 0 AND x < 10)

{

answer ← true;

}

3.4.3. Summary

In this lesson, you learned how to:

Learning Objective CRD-2.J: Identify inputs and corresponding expected outputs or behaviors that can be used to check the correctness of an algorithm or program.

Learning Objective AAP-2.E.a: For relationships between two variables, expressions, or values: a. Write expressions using relational operators.

Learning Objective AAP-2.E.b: For relationships between two variables, expressions, or values: b. Evaluate expressions that use relational operators.

Learning Objective AAP-2.H.a: For selection: a. Write conditional statements.

Learning Objective AAP-2.H.b: For selection: b. Determine the result of conditional statements.

Learning Objective AAP-2.I.a: For nested selection: a. Write nested conditional statements.

Learning Objective AAP-2.I.b: For nested selection: b. Determine the result of nested conditional statements.

Learning Objective AAP-2.L: Compare multiple algorithms to determine if they yield the same side effect or result.

3.4.4. Self-Check

Check Your Understanding

Complete the following self-check exercises.

Q-1: When testing software, it is important to test critical values or boundary values–both valid and invalid values at the extremes of a range of legal values. In an app where the user enters a number for the month of the year, what would be appropriate boundary values to check?

A. 28, 29, 30, 31, and 32.

B. -99 and +99

C. 0 and 1, also 12 and 13

D. 2, 3, 4, 5, 6, 7, 8, 9, 10, 1

Q-2: What would the following code print out if the score was 6?

A. Going strong!

B. Getting better!

C. Not so good!

Q-3: What would the following code print out if the score was 10?

A. "Getting better!"

B. "Not so good!"

C. "Going strong!"

Q-4: AP 2021 Sample Question: In a certain country, a person must be at least 16 years old to drive a car and must be at least 18 years old to vote. The variable age represents the age of a person as an integer. Which of the following expressions evaluates to true if the person is old enough to drive but not old enough to vote, and evaluates to false otherwise?

I. (age ≥ 16) AND (age ≤ 18)

II. (age ≥ 16) AND (NOT(age ≥ 18))

III. (age < 18) AND (NOT(age < 16))

A. I and III only

B. II only

C. II and III only

D. I and II only

Q-5: AP 2021 Sample Question: The following code segment is intended to set max equal to the maximum value among the integer variables x, y, and z. The code segment does not work as intended in all cases.

Which of the following initial values for x, y, and z can be used to show that the code segment does not work as intended?

A. (D) x = 3, y = 2, z = 1

B. x = 1, y = 2, z = 3

C. (B) x = 1, y = 3, z = 2

D. (C) x = 2, y = 3, z = 1

Q-6: AP 2021 Sample Question: In the following statement, val1, val2, and result are Booleanvariables.

Which of the following code segments produce the same result as the statement above for all possible values of val1 and val2?

Select two answers.

A.

B.

C.

D.

3.4.5. Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Docarrow-up-right where you may use File > Make a Copy to make your own editable copy.

Last updated