Global edition

Starting Out with App Inventor for Android Tony Gaddis • Rebecca Halsey This page intentionally left blank. 208 Chapter 3 Input, Variables, and Calculations

5. In the Blocks Editor, create blocks that initialize a variable named Age to the value of 25.

6. In the Blocks Editor, create blocks that initialize a variable named MiddleName to the value of Suzanne. 7. Create an app with a slider that displays values in the range of 100 to 200. 8. In the Blocks Editor, use math operator blocks to create an expression that gives the result of 43 3 25 3 6. 9. Create an app that enters three numbers and finds their maximum and minimum values. VideoNote The Average 10. Create an app that lets the user enter an angle, measured in degrees. The app of Three Test Scores App should display the sine, cosine, and tangent of the angle.

Programming Projects 1. Distance Traveled Assuming there are no accidents or delays, the distance that a car travels down an interstate highway can be calculated with the following formula: Distance  Speed  Time Create an app that allows the user to enter a car’s speed in miles per hour. The application should have buttons that display the following: ● The distance the car will travel in 5 hours ● The distance the car will travel in 8 hours ● The distance the car will travel in 12 hours

2. Circle Dimensions Create an app that allows the user to enter a radius r of a circle. The program should then calculate the area and perimeter of the circle. The program should display the radius, the surface area, and the perimeter of the circle (the surface area is equivalent to p  r2 and the perimeter is equivalent to 2  p  r). 3. Celsius and Fahrenheit Temperature Converter Assuming that is a Celsius temperature, the following formula converts the temperature to Fahrenheit: F  5/9C  32 Assuming that F is a Fahrenheit temperature, the following formula converts the temperature to Celsius: C  5/9(F  32) Create an app that allows the user to enter a temperature. The app should have Button components described as follows: ● A button that reads Convert to Fahrenheit. If the user clicks this button, the app should treat the temperature that is entered as a Celsius temperature and convert it to Fahrenheit. Programming Projects 209

● A button that reads Convert to Celsius. If the user clicks this button, the app should treat the temperature that is entered as a Fahrenheit temperature, and convert it to Celsius. 4. Body Mass Index Create an app that lets the user enter his or her weight (in pounds) and height (in inches). The app should display the user’s body mass index (BMI). The BMI is often used to determine whether a person is overweight or under- weight for his or her height. A person’s BMI is calculated with the following formula: BMI  weight  703  height 2 5. Cookie Calories A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a serving equals 300 calories. Create an app that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed. 6. Calorie Counter Create an app with a screen that resembles Figure 3-99. The screen displays the images of four fruits (a banana, an apple, an orange, and a pear) and each fruit’s calories. You can find these images in the Fruit Symbols folder of the book’s media , available for download at www.pearsonglobaleditions.com/Gaddis. When the application starts, the total calories displayed should be zero. Each time the user clicks one of the fruit images, the calories for that fruit should be added to the total calories, and the total calories should be displayed. When the user clicks the Reset button, the total calories should be reset to zero.

Figure 3-99 Calorie Counter App (Source: MIT App Inventor 2) 210 Chapter 3 Input, Variables, and Calculations

7. Calories from Fat and Carbohydrates A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day. Then, she calculates the number of calories that result from the fat, using the following formula: calories from fat  fat grams  9 Next, she calculates the number of calories that result from the carbohydrates, using the following formula: calories from carbs  carb grams  4 The nutritionist asks you to create an app that will make these calculations. 8. Measurement Converter Create an app that will convert miles to kilometers and pounds to kilograms. To get the most correct exchange rates, search the Internet using the term measurement conversions. If you cannot find the correct conversion rates, use the following: 1 Mile  1.852 Kilometer 1 Pound  0.45 Kilogram Display your output amounts rounded to two decimal places. Decision Blocks

CHAPTER 4 and Boolean Logic

TOPICS

4.1 Introduction to Decision Blocks 4.7 The if then else if Block 4.2 Relational Operators and the if Block 4.8 Working with Random Numbers 4.3 The if then else Block 4.9 The Screen’s Initialize Event 4.4 A First Look At Comparing Strings 4.10 The ListPicker Component 4.5 Logical Operators 4.11 The CheckBox Component 4.6 Nested Decision Blocks

4.1 Introduction to Decision Blocks

CONCEPT: Sometimes a program needs to “decide” whether or not to execute certain instructions. App Inventor provides three blocks for making decisions.

Computer programs work with many kinds of data. You’ve already created apps that work with values such as 1, 2, and 0.25. These values are numbers. You’ve also created apps that work with values such as Hello and Enter the distance. These are text values, which are also known as strings.

Programs can also work with the values true and false. These two values, true and false, are known as Boolean values, in honor of the English mathematician George Boole. In the 1800s Boole invented a system of mathematics in which the abstract ­concepts of true and false can be used in computations. Today, languages allow you to store the values true and false in memory and use those values in algorithms.

In a computer program, the values true and false are commonly used in decision ­making. Quite often, you will test a Boolean expression (an expression that gives either true or false as its value) and you will perform one set of instructions if the expression is true, or another set of instructions if the expression is false. 211 212 Chapter 4 Decision Blocks and Boolean Logic

The if then Block App Inventor provides the if then block for making decisions. In the Blocks column it is found in the Built-in section, in the Control drawer, as shown in Figure 4-1.

Figure 4-1 The Decision Blocks (Source: MIT App Inventor 2)

The if then block is shown in Figure 4-2. Notice that the if then block has two sockets: one for the if part, and one for the then part. The if socket holds a Boolean expression. If the Boolean expression is true, the instructions that appear in the then socket will be executed. If the Boolean expression is false, nothing happens (the instructions that appear in the then socket will be skipped). Figure 4-3 shows a way that might be helpful to think about the if then block.

Figure 4-2 The if then Block (Source: MIT App Inventor 2)

Plug a Boolean expression here. The blocks that you plug here will be executed ony if the Boolean test expression is true.