Lab 2.1: Types
Types
Goals
The goal of this lab is to learn the following
- Declaring variables
- Naming rules for variable declarations
- How to concatenate text using the + operator
Computer Science Principles Curriculum
- Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1
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.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8
Vocabulary
- Projects window
- Quorum Studio Output window
- Editor
- Declare
- Initialize
- Variable type
- Integer
- Text
- Boolean
- Number
- Concatenation
Hotkeys
- Build Project
- Editor Window
- Output Window
- Projects Window
- Run Project
Overview
This lab will guide you through declaring variables with the appropriate name, primitive data type and matching value. In addition to declaring variables you will also be learning the rules to name a variable and how to initialize them. Then, you will learn to use the say and output commands and how to concatenate using the "+" operator. This lesson can be done online in the editors or offline in Quorum Studio. References to both are included.
Goal 1: Declaring Variables
Start by opening Quorum Studio and creating a new project. In the New Project dialog, make sure "Quorum" is selected in the Categories list. Then select the "Blank Application" project in the Projects list and press Enter. On the next step, name the new project "Lab2_1" in the Project Name text field. Press Enter or click the Finish button to create a new project. Before you begin writing any code, be sure to check the new project is the main project.
For this lab, you will use the following windows in Quorum Studio: 1) the Projects window, 2) the Quorum Studio Output window, and 3) the Editor. The Projects window holds a list of folders and files that allows you to organize and navigate the project. This is where source files and other files will be found. The Sodbeans Output window displays any compiler errors you may have and it displays any output statements executed during the programs runtime. Finally, the Editor window is where you will write your code.
There are four types of primitive data types that you can use in Quorum:
integer
: a positive or negative number (including 0) that does not have a decimal. Examples: 1, 0, -10number
: a numerical value with a decimal. Examples: 1.0, 0.0, -10.35boolean
: a true/false value. Example: true, falsetext
: a literal string/text value enclosed in quotes. Examples: "Hello World", "5", "true"
To initialize a variable, the format of the code is: type variableName = value
.
Example: Declare a variable with the integer type called myInteger
In the example below we declare a variable called myInteger with the integer
type and assign the variable the value of 4.
integer myInteger = 4
Whenever we initialize a variable we are doing three things:
- reserving a location in memory of the type declared
- assigning a name to the memory location that we can use in our program
- storing a value in the memory location
Goal 2: Naming rules for variable declarations
Each variable has to have an unique name. If you want more than one variable to have similar names you should insert something to differentiate from the other.
Example: Declare two variables with similar names
integer myInteger = 4
integer myInteger2 = 6
Notice that the names are similar but we add the 2 in the end to differentiate from the first one. The name of the variable must start with a letter, with can be upper or lower case. It can have more than one word in the name but it cannot have space between them. Camel case is a common format used for easy readability. Although it is not required, by convention, variable names usually begin with a lower case letter and class names usually start with a capital letter. The following is an example of this convention.
Example: Declare variables using camel case
text myFirstText = "Hi!?"
boolean whatIsBoolean = true
There are some characters that you cannot use to name a variable. You cannot use a number or a symbol as the beginning of the name. In addition, you cannot use certain words that are reserved for specific functions in Quorum.
Example: Declaring variables that break the naming rules
// This code generates an ERROR
number 2number = 4.6
boolean boolean = false
The message in the Output window should give you a clue as to what went wrong. In addition, Quorum will automatically display a line under the code that has an error so that you will know where something went wrong. Once all of the compiler errors are gone, run the program by going to Run -> Run Main Project or by pressing F6. When the project runs, nothing will be in the output window because all you have done is declared the variable.
Goal 3: How to concatenate text using the + operator
Add more variables and types to the program. Do the following steps to add types:
- Define an
integer
type and assign it the name firstInteger. Assign 4 to firstInteger. - Define a
number
type and assign it the name secondNumber. Assign 8.9 to secondNumber. - Define a
boolean
type and assign it the name checkResult. Assign true to checkResult. - Define a
text
type and assign it the name quorumText. Assign " You just wrote your first program. " to quorumText.
Now that you have defined these four new variables, have Quorum output and say each variable to the user.
Example: Output and say the variable firstInteger to the user.
Use the following code as an example to output and say each of the variables that you just initialized.
output firstInteger
say firstInteger
Notice that each of the commands need a separate line, so for 4 variables we need 8 lines of code to output and say everything.
Example: Concatenation
You can use concatenation to combine two or more variables or phrases using the operator. The following code shows how to construct an output statement using concatenation:
output secondNumber + " is greater than " + firstInteger
say secondNumber + " is greater than " + firstInteger
When using text make sure you put space inside the quotes, otherwise there will not be the space between the last word and the variable or between the variable and the first word.
Now have your program output the following sentence using the variable names above:If the second number is 8.9 and the first number is 4, then checkResult is true.
To check your output statement, change the value assigned to firstInteger to 10
and checkResult to false
and then run your program again. If you wrote your output statement correctly, your program should display this line:If the second number is 8.9 and the first number is 10, then checkResult is false.
Once you have completed this, you are done! Show your work to the instructor.
Next Tutorial
In the next tutorial, we will discuss Lab 2.3, which describes how work music in Quorum..