Example: air traffic controller

CHAPTER 8 Presidents Quiz - Appinventor

Figure 8 Presidents QuizThe Presidents quiz is a trivia game aboutformer leaders of the United States. Though thisquiz is about Presidents , you can use it as atemplate to build quizzes or study guides on the previous chapters, you ve been introducedto some fundamental programming , you re ready for something more ll find that this CHAPTER requires a conceptualleap in terms of programming skills and abstract thinking. In particular, you ll use two listvariables to store the data in this case, the quiz questions and answers and you ll usean index variable to track where the user is in the quiz . When you finish, you ll be armedwith the knowledge to create quiz apps and many other apps that require list CHAPTER assumes that you re familiar with the basics of App Inventor: using theComponent Designer to build the user interface, and using the Blocks Editor tospecify event handlers and program the component behavior.

Figure 8-1. CHAPTER 8 Presidents Quiz The Presidents Quiz is a trivia game about former leaders of the United States. Though this quiz is …

Tags:

  Chapter, Quiz, President, Chapter 8 presidents quiz

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of CHAPTER 8 Presidents Quiz - Appinventor

1 Figure 8 Presidents QuizThe Presidents quiz is a trivia game aboutformer leaders of the United States. Though thisquiz is about Presidents , you can use it as atemplate to build quizzes or study guides on the previous chapters, you ve been introducedto some fundamental programming , you re ready for something more ll find that this CHAPTER requires a conceptualleap in terms of programming skills and abstract thinking. In particular, you ll use two listvariables to store the data in this case, the quiz questions and answers and you ll usean index variable to track where the user is in the quiz . When you finish, you ll be armedwith the knowledge to create quiz apps and many other apps that require list CHAPTER assumes that you re familiar with the basics of App Inventor: using theComponent Designer to build the user interface, and using the Blocks Editor tospecify event handlers and program the component behavior.

2 If you are not familiarwith these fundamentals, be sure to review the previous chapters before ll design the quiz so that the user proceeds from question to question byclicking a Next button and receives feedback about the 8-2. The Presidents quiz inactionWhat You ll LearnThis app, shown in Figure 8-1, covers: Defining list variables for storing thequestions and answers in lists. Sequencing through a list using an index;each time the user clicks Next, you ll displaythe next question. Using conditional (if) behaviors: performingcertain operations only under specificconditions. You ll use an if block to handlethe app s behavior when the user reaches theend of the quiz . Switching an image to show a differentpicture for each quiz StartedNavigate to the App Inventor website and start a new project. Give the project thename PresidentsQuiz and set the screen s title to Presidents quiz . Connect yourdevice or emulator for live ll need to download the following pictures for the quiz from website onto your computer: ll load these images into your project in the next the ComponentsThe Presidents quiz app has a simple interface for displaying the questions andallowing the user to answer.

3 You can build the components from the snapshot of theComponent Designer shown in Figure 8: Presidents QuizChapter 8, Presidents QuizFigure 8-3. The Presidents quiz in the DesignerTo create this interface, first load the images you downloaded into the project. Inthe Media area, click Add and select one of the downloaded files ( , ).Do the same for the other three images. Then, add the components listed in Table 8-1. Components for the Presidents quiz appComponent typePalette groupWhat you ll name itPurposeImageUser InterfaceImage1 The picture displayed with InterfaceQuestionLabelDisplay the current the answer text and button ina InterfaceAnswerTextThe user will enter his InterfaceAnswerButtonThe user clicks this to submit InterfaceRightWrongLabelDisplay correct! or incorrect! ButtonUser InterfaceNextButtonThe user clicks this to proceed tothe next the ComponentsDesigning the to the image file , the first picture that shouldappear.

4 Set its Width to Fill parent and its Height to to (you ll input the first question in theBlocks Editor). to Enter an answer . Set its Text property to blank. Moveit into to Submit and move it to Next . to blank. Adding Behaviors to the Components You ll need to program the following behaviors: When the app starts, the first question appears, including its correspondingimage. When the user clicks the NextButton, the second question appears. When it sclicked again, the third question appears, and so on. When the user reaches the last question and clicks the NextButton, the firstquestion should appear again. When the user answers a question, the app will report whether it is ll code these behaviors one by one, testing as you THE QUESTION AND ANSWER LISTSTo begin, define two list variables based on the items in Table 8-2: QuestionList tohold the list of questions, and AnswerList to hold the list of corresponding 8-3 shows the two lists you ll create in the Blocks 8-2.

5 Variables for holding question and answer listsBlock typeDrawerPurposeinitialize global to ( QuestionList )VariablesStore the list of questions (rename it QuestionList).initialize global to( AnswerList )VariablesStore the list of answers (rename it AnswerList).make a list ListsInsert the items of the 8: Presidents QuizChapter 8, Presidents QuizBlock typeDrawerPurposetext (three of them)TextThe a list ListsInsert the items of the (three of them)TextThe 8-4. The lists for the quizDEFINING THE INDEX VARIABLEThe app needs to keep track of the current question as the user clicks the NextButtonto proceed through the quiz . You ll define a variable named currentQuestionIndex forthis, and the variable will serve as the index into both the QuestionList andAnswerList. Table 8-3 lists the blocks you ll need to do this, and Figure 8-4 shows whatthat variable will look 8-3. Creating the indexBlock typeDrawerPurposeinitialize global to( currentQuestionIndex )VariablesHold the index (position) of the current (1)MathSet the initial value of currentQuestionIndex to 1 (the firstquestion).

6 Figure 8-5. Initiating the index variable with a value of 1 DISPLAYING THE FIRST QUESTIONNow that you ve defined the variables you need, you can specify the app s interactivebehavior. As with any app, it s important to work incrementally and define onebehavior at a time. To begin, think only about the questions and, specifically,displaying the first question on the list when the app launches. We ll come back anddeal with the images and answers a bit Behaviors to the ComponentsAdding Behaviors to the ComponentsYou want your code blocks to work regardless of the specific questions that are onthe list. That way, if you decide to change the questions or create a new quiz bycopying and modifying this app, you ll only need to change the actual questions inthe list definitions, and you won t need to change any event , for this first behavior, you don t want to refer directly to the first question, Which president implemented the New Deal during the Great Depression?

7 Instead,you want to refer, abstractly, to the first socket in the QuestionList (regardless of thespecific question there). That way, the blocks will still work even if you modify thequestion in that first select particular items in a list with the select list item block. The block asksyou to specify the list and an index (a position in the list). If a list has three items, youcan enter 1, 2, or 3 as the this first behavior, when the app launches, you want to select the first item inQuestionList and place it in the QuestionLabel. Recall from the Android, Where s MyCar? app, if you want something to happen when your app launches, you programthat behavior in the event handler. You can use the blocks listedin Table 8: Presidents QuizChapter 8, Presidents QuizTable 8-4. Blocks to load the initial question when the app startsBlock Screen1 Event handler triggered when the app to QuestionLabelPut the first question in list item ListsSelect the first question from global QuestionList VariablesThe list to select questions (1)MathSelect the first question by using an index of the blocks workThe event is triggered when the app starts.

8 As shown inFigure 8-5, the first item of the variable QuestionList is selected and placed So, when the app launches, the user will see the first 8-6. Selecting the first questionTest your app Click Connect and connect to your device or theemulator for live testing. When your app loads, do you see thefirst item of QuestionList, Which president implemented the New Deal during the Great Depression? ITERATING THROUGH THE QUESTIONSNow, program the behavior of the NextButton. You ve already defined thecurrentQuestionIndex to remember the current question. When the user clicks theNextButton, the app needs to increment the currentQuestionIndex ( , change itfrom 1 to 2 or from 2 to 3, and so on). You ll then use the resulting value ofcurrentQuestionIndex to select the new question to display. As a challenge, see if youcan build these blocks on your own. When you re finished, compare your resultsagainst Figure Behaviors to the ComponentsAdding Behaviors to the ComponentsFigure 8-7.

9 Moving to the next questionHow the blocks workThe first row of blocks increments the variable currentQuestionIndex. IfcurrentQuestionIndex has a 1 in it, it is changed to 2. If it has a 2, it is changed to 3,and so on. After the currentQuestionIndex variable has been changed, the app uses itto select the new question to display. When the user clicks NextButton for the firsttime, the increment blocks will change currentQuestionIndex from 1 to 2, so the appwill select the second item from QuestionList, Which president granted communistChina formal recognition in 1979? The second time NextButton is clicked,currentQuestionIndex will be set from 2 to 3, and the app will select the thirdquestion on the list, Which president resigned due to the Watergate scandal? Note Take a minute to compare the blocks of tothose in the event handler. In blocks, the app used select list item with aconcrete number (1) to select the list item.

10 In these blocks, you reselecting the list item by using a variable as the index. The appdoesn t choose the first item in the list, or the second, or the third; itchooses the currentQuestionIndexth item, and thus a different itemwill be selected each time the user clicks NextButton. This is a verycommon use for an index incrementing its value to find and displayor process items in a 8: Presidents QuizChapter 8, Presidents QuizTest your app Test the behavior of the NextButton to see if theapp is working correctly. Click the NextButton on the the phone display the second question, Which presidentgranted communist China formal recognition in 1979? Itshould, and the third question should appear when you clickthe NextButton again. But if you click again, you should see anerror: Attempting to get item 4 of a list of length 3. The apphas a bug! Do you know what the problem is? Try figuring itout before moving problem with the code thus far is that it simply increments to the nextquestion each time without any concern for the end of the quiz .


Related search queries