App Inventor: Lab 2 – Simple Calculator
App Inventor: Lab 2 – Simple Calculator
App Inventor: Lab 2 – Simple Calculator
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>App</strong> <strong>Inventor</strong>: <strong>Lab</strong> 2 <strong>–</strong> <strong>Simple</strong> <strong>Calculator</strong><br />
A. Notes<br />
Developers often need to modify both text and numeric data.<br />
Math operations <strong>–</strong> <strong>App</strong> <strong>Inventor</strong><br />
Every programming language provides a base set of math operators. The basic math operations can be<br />
found in the Math drawer in the Blocks Editor. Many other math operations are available in the Math<br />
drawer as well.<br />
Text operations <strong>–</strong> <strong>App</strong> <strong>Inventor</strong><br />
The <strong>App</strong> <strong>Inventor</strong> text (strings) methods are located in the Text drawer in the Blocks Editor. The text<br />
method allows the programmer to add specific text to the program. There are many other text methods<br />
such as join or make text that will allow you to manipulate text strings.<br />
B. Learning Objectives<br />
In this lab you will learn how to use:<br />
• Components<br />
o Basic<br />
TextBox <strong>–</strong> obtain input from users and display output to others<br />
<strong>Lab</strong>el <strong>–</strong> identifies items on the screen<br />
Button <strong>–</strong> will be used to allow the user to input what math operation they wish to<br />
use<br />
Math Operators <strong>–</strong> used to calculate mathematical calculations<br />
o Screen Arrangements<br />
HorizontalArrangement - position GUI components horizontally<br />
o Other Stuff<br />
TextToSpeech <strong>–</strong> convert text to speech for output to the user<br />
• Blocks/Methods<br />
o Math library<br />
addition / subtraction / multiplication / division<br />
o Text library<br />
join <strong>–</strong> used to concatenate or joining 2 text values to create a new text string<br />
make text <strong>–</strong> alternative block for joining any number of text strings<br />
Page 1 of 3
C. <strong>App</strong>lication Description<br />
This application is a simple calculator. The user should be able to initially Add and Subtract two numbers.<br />
The application should then be extended for more operations and features (see extensions at the end of the<br />
assignment).<br />
A sample screen for the initial operations of the simple Addition and Subtraction <strong>Calculator</strong> is provided:<br />
Additional Instructions<br />
• There should be an event processing sequence for each button. This means that when each button<br />
is done, a series of events will take place. The sample block sequence for the Addition button<br />
event processing is shown below.<br />
• In this example, the application not only displays the answer in text field, but also tells the<br />
computer to speak the answer to the user using the TextToSpeech library. To use the<br />
TextToSpeech feature, go to the Component Designer, look under the “Other Stuff” palette and<br />
drag it onto your application <strong>–</strong> it will be a Non-visible component.<br />
In the btnAdd block above you may notice that the Text property of a text box named lblResults is<br />
being modified using a math operation. You will find these Math operations within the Blocks Editor.<br />
Page 2 of 3
Your first step should be to design your user interface and decide on the application logic.<br />
Here is a sample user interface design. Textboxes are used for<br />
user input and to show the result of the calculation. In the<br />
example to the right you will notice that there are 2 different<br />
textboxes for the 2 numbers to be inputted by the user as well as<br />
a textbox to show the output which is the result of the<br />
calculation. There are also two buttons to allow the user to<br />
choose whether they wish to add or subtract.<br />
The labels and textbox placement can be controlled using the<br />
Screen Alignment components (HorizontalArrangement,<br />
VerticalArrangement or TableArrangement). To place<br />
components in fixed locations you should:<br />
• drag arrangement components onto the viewer<br />
• place the GUI components the desired location<br />
D. Tasks<br />
Development typically follows many cycles of design, code, and<br />
test. Complete each of these phases.<br />
Phase 1<br />
Implement the Add/Subtract operations using 2 different<br />
textboxes for input values and test your application.<br />
Phase 2<br />
Add the features for Multiplication and Division and a Clear button to erase all of the textboxes to start<br />
inputting new numbers.<br />
Phase 3<br />
Add a feature to have the result spoken to the user.<br />
Page 3 of 3