Hour 4: Integer and Number Variables

This lesson is to teach you about integer and number values.

Overview

Imagine you are a musician and play the cello. While you might play an important role in a piece of music, if you were part of an orchestra, you might be accompanied by other instruments. As a whole, these other instruments work together to make a piece of music work. While an imperfect metaphor, computers have their own accompaniment: libraries. Libraries are often code that someone else wrote and can allow you to do very advanced programming far more easily than inventing it all yourself. In this lesson, you are going to learn about libraries and objects.

Goals

You have the following goals for this lesson:

  • Learn how to use libraries
  • Create object variables
  • Learn how to tell objects to take actions

Warm up

Some types of applications are harder to invent than others. For example, a variable is a relatively simple thing, whereas a self-driving car might take a generation or two to invent with many engineers working on it. For something you are interested in inventing, what kind of libraries do you think you might need that you do not immediately know how to invent yourself?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
ClassA custom data type. Classes are a plan for constructing these custom data types in memory
ObjectA class is a plan, whereas an object is one copy of that plan. 
LibraryA library is a collection of code that defines a type of Object. Using a library lets you use the type of Object it describes.
PackageA package is a categorized group of libraries.
ActionActions are tasks that can be performed by variables. Different Object types have different Actions available.
ParameterA parameter is a single piece of information that is input into an action when it executes.
Standard LibraryA set of libraries that are standard in a particular programming language. Most languages have invented their own standard library.

Code

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCodeExplanation
use LIBRARYuse Libraries.Compute.MathA use statement lets us use a type of Object in your program.
TYPE NAMEMath mathA variable declaration that makes a new Object. Unlike primitive variables, Object variables do not need the “=” sign.
NAME:ACTION()math:Action() [ex. math:AbsoluteValue(-5)]Instructs the variable to perform a specific, pre-programmed task.

CSTA Standards

This lesson covers the following standards:

  • 1B-AP-14: Observe intellectual property rights and give appropriate attribution when creating or remixing programs.
  • 3A-AP-15: Justify the selection of specific control structures when tradeoffs involve implementation, readability, and program performance, and explain the benefits and drawbacks of choices made.
  • 2-AP-16: Incorporate existing code, media, and libraries into original programs, and give attribution.
  • 3A-AP-20: Evaluate licenses that limit or restrict use of computational artifacts when using resources such as libraries.

Explore

Sharing code can save time and effort. Many computer scientists have worked hard to create functionality and, if that code is under a license where others can use it, may be useful to others. In order to share code, programmers organize their work into libraries.

A library is a collection of code that is organized to handle a specific kind of task. You can bring libraries into any program to help solve problems using pre-written code. To give an example from math, while addition and subtraction are easy, many more complex mathematical operations exist (e.g., logarithms, trig functions). Hypothetically, you could reinvent all of these systems, but they are difficult to write and understand. Further, if another person has already solved the problem, it can make sense to reuse an existing solution.

Quorum has many different libraries available, all of which are under an open software license called BSD-3. This roughly means they can be used for commercial or non-commercial purposes. Many other programming languages and environments exist with many being open source under a relatively open license.

While it would not make sense to use for light reading, the Quorum Standard Library index page has a list of all libraries. It contains all pre-written code functions that can be used to make your programs easier to write. Here you will see a list of libraries on the left side of the page. The libraries are organized into groups called packages. Packages are named in a way that sort of resembles a web address, using words separated by periods.

One example of a library most people would not want to write on their own is the Math class in the package named Libraries.Compute. Mathematical equations are important, no doubt, but are both boring and complex. How to calculate mathematical operations correctly and accurately is often no easy task. The center of the page shows documentation for the Math library, including a brief summary, an example, and a list of variables and actions.

A screenshot of the Quorum Standard Library index. The Math class is selected from the Libraries.Compute package. The center of the page includes details on the library, headed by a title that says Libraries.Compute.Math Documentation, and includes a summary, example, and a list of variables.

Programming languages often have quite a few libraries to choose from. Specifically in Quorum, there are libraries for graphics, accessibility, math, data science, gaming, robots, and much more. Normally, in a course on computer science, it would be unrealistic to learn them all and this is okay. Focusing on bigger picture concepts, while having fun in a domain of interest to you, is typical.

Math Library

Now imagine using the Math library. In Quorum, there are multiple ways that libraries can be used and what is available depends on the platform. Specifically, if you are running Quorum on a desktop computer inside of the Quorum Studio development environment, there are slightly different libraries available than if you put Quorum on a robot or run it on the web. While these differences can be important, a vast number of the libraries work similarly or the same everywhere. The point though is that because robots, web browsers, desktop computers, and mobile devices all work a bit differently, it is normal to expect some subtle differences in the available libraries. Any version of Quorum Blocks will come with the Math libraries built in.

To use a library in a program, you need to tell the programming language which library you want to connect to. In order to add a library to your program, you need a new block, the 'use' block.

This shows the development with a single block in it that says use myLibrary. The error in this image specifies that the main starting point of the program is wrong, which is correct, but curious.

Inside the editable area of the block, you might have a proper name, but this example intentionally uses an incorrect one: myLibrary. This is a placeholder value that would be replaced with the name of the library you want. Notice that for the online editor, if you were to try and run this, it may not give the error that you would expect. It says:

I did not understand:
Line 1, Column 1: I noticed that the class Main.quorum was specified as the starting point for this program, but it does not have an action named Main.

For Math, the full name of the library is Libraries.Compute.Math. You might think that changing the line of code to this would fix the problem, but it will not. The problem is that programming languages have many mathematical constraints inside of them and, because of these, sometimes they are not very good guessers as to what is wrong in code. For example, in this case, this code has a single line, or block, and this line is telling the programming language you want access to myLibrary, which does not exist. Even if it did, asking the computer to access a library is not really an action. It would not do anything. As such, in English this error is telling you that this code would do nothing. Since it would make no sense to run code that does nothing, it is an error.

Breaking the error itself down, all computer programs have to start somewhere. Most programming languages use the name Main, for nothing more than convention, to represent where to start. In Quorum specifically, in order to help novices, this is hidden when you start out and written for you behind the scenes. Thus, when the error says that there is no main, it is saying in a roundabout and confusing way that the program would do nothing.

Now consider a second example, where you have an output statement, but the code would do something, like output a value. In this case, if the use statement is wrong, Quorum can give a slightly easier to understand error:

In this image, there are now two lines of code, including use myLibrary and output with the word hi in double quotes. Notably, because the output line is there, the error message instead focuses on the word myLibrary not being a real Quorum library.
I did not understand:
Line 1, Column 0: I could not locate the package named myLibrary. Are you sure you spelled it correctly?

In this case, it is saying that myLibrary does not exist, which is more direct. Switching back again to the correct code for the use statement, if you want to actually use math, you need to create an object. To do this, you can make a new blank block beneath the use statement or an output statement. A blank block is exactly, and literally, equivalent to writing an empty line of code, blurring the line between blocks and text.

In this blank block, to use math, you need to write a line of code to make an object. This is similar to the variables in the last lesson, but this time, you are going to be using a special type. The variable type is Math. The name can be anything that follows the variable naming rules, but in the example below, the name is “calculator.”

In this example, the important lines are use Libraries.Compute.Math and Math calculator. The crucial point here is that this does correctly load up the math library, but then immediately does nothing with that.

Although it looks different, this new block is making a new variable, just like you saw when you were working with the primitive types. Previously, you put simple values into your variables, like numbers or text. This time, you will put a complex value inside your container, called an Object. In this case, the object is a Math type, which is kind of like a scientific calculator you can use.

One approach to learning a library would be to read all of the documentation, which is about as fun as watching paint dry. Another approach is to have the computer help us and learn on the fly. In the offline editor, Quorum Studio, the environment can automatically detect the calculator object and inform you what operations it can take. In the online editor, the Parsons problems isolate operations for learning. However, the downside is that it does not automatically tell you all of the options that are available. Put another way, the online environment is more curated.

The most important aspect of objects to understand is that they can take actions. Actions are special tasks that Objects can perform. For example, the Math type object knows how to calculate absolute values or many of the other operations built into a scientific calculator.

Actions have a specific structure when written as code. An action always begins with the name of the Object that is doing the task, followed by a colon. After the colon comes the name of the action that is being used, followed by a pair of parentheses. For example, with finding the Sine value of your Math object, the block of code would look like: calculator:AbsoluteValue(value) where value is placeholder for any numerical value.

Depending on the action, different tasks require a different amount of information inside the parenthesis. For example, calculating the absolute value of a number requires the number you want the absolute value of. The RaiseToPower action needs two pieces of information, the base number and the exponent. These pieces of information are called parameters.

Some actions do not need any parameters at all. Even if the action doesn’t need any extra information, though, the parenthesis are always added to the end of an action. The following is an example of using calculator:SquareRoot(value), which has an error in it.

In this example, the important lines are use Libraries.Compute.Math and Math calculator. The next line is output calculator:SquareRoot(9), which will return 3.

Just like when you put the use statement in, your new block might have a placeholder in the text, the part that says value. This placeholder is a parameter to the action is the name value, which does not exist. You might replace that word with a number, like 0. You can also put the value out to a variable or output it, like so in text:

number root = calculator:SquareRoot(4)
output root

Engage

In this activity, you are going to go on a scavenger hunt to look for libraries that might interest you. You may be interested in learning to invent, or teaching others to invent, many different kinds of applications. This can vary in a seemingly endless number of ways. The goal here is to familiarize yourself with all of the options available in the current version of the standard library. When your scavenger hunt is finished, complete one set of Parsons problems related to objects and calling actions.

Directions

Pick a partner to do a scavenger hunt with. You are going to explore as a group different libraries for various kinds of applications. Your broad goal is to find two libraries in each of the following categories:

  • User Interfaces
  • Data Science
  • Computer Graphics
  • Robotics
  • Accessibility
  • Two more categories of your choosing

As you go on your scavenger hunt, write down which libraries you think might be the most interesting and most useful. For the two you found the most interesting, look at the actions that the library can take and identify three of each you think might perform an interesting behavior.

You may refer to the following links on the Quorum website to get you started in finding these libraries:

  1. Standard Library Index
  2. Quorum Language References

Once you have completed your scavenger hunt, there is only one set of Parsons problems for this lesson. In this case, you will practice calling a few math actions. As a reminder, you do not need to memorize all of the actions inside of every object. That is impractical. As such, what you want to practice with these problems is not really about math, but instead is about learning to use libraries, objects, and actions.

  1. Learn about loading and using the Math library

Wrap up

Once you are done, if time allows, each group can share aloud their top two most interesting libraries. Consider as a group what might be good targets for using in specifically computer science education.

Next Tutorial

In the next tutorial, we will discuss Boo Boo Management Online, which describes how to manage errors.