GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 04: Introduction to Variables with Integers


Objectives

By the end of this module, you will be able to:

And, once we've worked with integers, we'll also do some "number crunching".




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Catching Up

Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:

  1. Tracing
  2. Debugging:
    1. Debug Prints
    2. Debugger (In Codio)
    3. Visualizer
  3. Clarify: What happens if we use the Run button for Compile & Run AND the Compile fails???




First, an analogy

Suppose we have boxes. Consider the following rules about "boxes":

Activity 1: Suppose z is a box that stores caps. What will the statement z = y result in? Draw a picture.




Integer variables

Consider the following program:

Activity 2: Edit, compile and execute the above program. What gets printed out to the terminal?

Now let's examine key parts of this program:

Next, let's look at assignment between variables:

Activity 3: What happens if you don't place a value in a variable? Find out by editing and compiling this program:

Activity 4: Identify the output of this program just by reading (mental execution).

Activity 5: Identify the output of this program just by reading:

Then, write up the program to confirm.

A rule: a variable must be declared before it is used (assigned a value or have a value copied from it).

Activity 6: What is the compiler error you get for this program?




Variations

The following variations are worth knowing:

Note about writing style:

Activity 7: Consider the following partially-complete program:

Write code where indicated to have the program first print 6, then 5. You may not change any of the existing code and you may not directly assign the numbers. Instead, find a way to swap: that is, whatever is in i should go into j and whatever is in j should go into i.




Integer operators


Unary Operators

First, let's consider some unary operators:

Activity 9: Mentally execute the program below - what does it print?

It is very important to know that a unary operator written like this:
num ++;
will increase the value of the variable num by 1 only AFTER it has been resolved and used in the expression it is in.
That means that a more complex operation, that involves the unary operators, might not result in what you expect.

Example:

Activity 10: Mentally execute the program below - what does it print?
Binary Operators

Next, let's examine the familiar binary operators +, -, *, /

Activity 11: Type up this program. What does it print? Change i to 21. What is the value of m?




Integer division:




Expressions and operator-precedence

Consider the following program:

Activity 12: Type up the above program in MyExpressionExample.java (renaming the class name to MyExpressionExample). What does it print?

About expressions:

Activity 13: What does the expression i*j - i+1*j-1 evaluate to when i=7 and j=3?




More about expressions and assignment

Here are two more operators, one unary (negative sign) and one binary (remainder or mod operator), that are commonly used:

Activity 14: What does it print? One way to know whether one number cleanly divides another is to apply the % (remainder) operator.

About the % operator:

Activity 15: Write code in MyExpressionExample3.java to take the loop in 5.19 above and print the quotient instead of remainder. For example, the first three lines of the output should be:
10
5 
3
    
(Thus, for example, when i is 1, the quotient is 10).

Now we'll look at a strange (initially) but very useful type of assignment:




Choosing variable names

Thus far, for simplicity, we have used short one-letter names for variables.

It is common to use longer, more meaningful names.

About variable names:




When things go wrong

As you might imagine, there are many ways to inadvertently create errors.

Let's start by identifying compilers errors by reading carefully.

Activity 16: Identify the compiler error:

Identify the compiler error:

Identify the compiler error:

Identify the compiler error:




Meta

Another in our series of occasional "meta" sections that will step back from the material to comment on how we can learn better.

This was a loooong module with lots of exercises and details. Let's review:

So, if you felt a bit overwhelmed, that's perfectly understandable. If you have to go back to some of the material to review or try some exercises again, that's fine. You're going to get better at this!