Goals

The goal of this lab is to learn the following:

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

Overview

In this lab you will write a program in Quorum that performs basic mathematical operations and solves written math problems. You will demonstrate how to work with numbers by performing addition, subtraction, multiplication, division, and remainder operations in Quorum. You will also learn how to get input from the users.

Goal 1: Using basic mathematical operations in Quorum

You will concentrate on performing basic mathematical operations in Quorum. Then, you will move onto solving problem statements. Start by opening Sodbeans and creating a new blank Quorum project. Name the project Lab2_2.

Declare and initialize two integer variables: a to 7 and b to 4. Next, declare and initialize two number variables: c to 8.5 and d to 9.2. You will use these variables to perform mathematical operations and output the results to the Sodbeans Output window. We will start with addition, let's add a and b.

Example: Assign the result of adding a and b to the variable sum1.

integer a = 7
integer b = 4
integer sum1 = a + b
output sum1

After you run the example you should have in the output window the number 11.

Activity: Assign the result

Assign the result of adding a, b, c and d to the variable sum2. Do not forget to output your answer.

If you are going to perform a mathematical operation that includes at least one number variable, your answer should also be a number. You will apply the same concepts to the other mathematical operations.

To perform the subtraction you will use the minus sign (-) and for multiplication you should use the asterisk (*). Output all your answers.

Now move onto division by dividing a by b and assigning the result in the variable called divide1.

Example: Divide a by b and run the program.

integer divide1 = a/b
output divide1

Why was divide1 assigned the value of 1 when it should be 1.75? That happens because the variables are integers, so the result is going to be an integer. To see the difference, declare two new number variables called e and f with the same values of a and b, respectively.

Activity: Divide e by f.

You should have as output 1.75 if you declare the types correctly. Always run and output the operations.

The final mathematical operation available in Quorum is the modulus or remainder. This is written using the keyword mod. You will use the mod operation to get the remainder of a division.

number remainder = 10 mod 6
In this example, remainder would contain the value of 4 because 10 divide by 6 equals 1 with the remainder of 4.

Example: Using mod get the remainder of dividing c by b.

number remainder1 = c mod b 
output remainder1

You will have in the output window the value of 0.5. The example above shows that you can use mod with decimal numbers, but it is not usually done.

Let's wrap up what you have done with the mathematical operations. Write code that combines all four operations into one statement: addition, subtraction, multiplication and division. Name a variable result and perform the following operations:

(a + b) * (c - b) + (d / b) 

Activity: Output the answer to the operations above.

Compute this answer for yourself, given the values you have assigned to the variables a, b, c, and d.

Then, check your answer by adding another output statement for the result variable. The output for this operation should be 51.8.

Goal 2: Getting input from the user

When you write programs, you don't always merely perform computation on data you already know, as in the first part of this lab. Most of the time, you need to get input from the user to perform your calculations. As an example, a desktop calculator is a program taking input from the user (via the keypad), and providing output (the answer to the equation you entered)

You may get input from the user in Quorum using the input keyword. The code below asks the user for their name; inside the parenthesis, you tell the user what information you are requesting.

Example: Ask and output the user name.

text name = input("Please enter your first name:")
output name

When you run this program, "Please enter your name." will appear in the output window. Enter your name and press Enter, or click the "OK" button. Your name will appear in the output window.

It can do more than ask for input such as a name. You can also request numerical values. Let's create a second input statement that asks us to enter an integer value. The code should look something like the following:

text ageInput = input("How old are you?")

Notice, input will always be of type text. However, you can convert the text value of the ageInput variable to any other type you desire, such as integer or number. You do this using the cast statement, as below. Here, you desire to have the age as a whole number, so you will use the integer type.


integer age = cast(integer, ageInput)

Activity: Write the code to speak the age result.

It should say "Hi," followed by the name entered by the user.

Next, concatenate on the words, "you are " followed by the age variable and finally the words " years old."

Next Tutorial

In the next tutorial, we will discuss Lab 2.3, which describes how work music in Quorum..