Goals

The goal of this lab is to learn the following:

• Using basic mathematical operations in Quorum
• Getting input from the user

Computer Science Principles Curriculum

• Big Idea: Abstraction: EK 2.1.2C

Common Core Standards

• English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.10, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.11-12.4, CCSS.ELA-Literacy.RST.11-12.5
• Mathematics Content: High School Functions » Building Functions: CCSS.Math.Content.HSF.BF.A1
• Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP4, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8

Vocabulary

• Operations
• Subtraction
• Multiplication
• Division
• Modulus
• User Input

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.

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.

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) ``

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)``

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..