GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 03: Introduction to Numeric Variables


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




Shift in course format

We are going to start picking up the pace in both lectures and labs at this point in the semester. Some items to be aware of:




Why are numbers and numeric types useful in computation?

Now, let's talk about how Java (and other programming languages store the most basic type of values: numbers.

But first, an analogy

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




Integer variables

Consider the following program:

Activity 1:Let's trace through this code together to make sure you understand the following concepts about creating and using integers in Java:

Some notes on style:




Integer operators


Binary Operators

First, let's examine the familiar binary operators (you've already seen the addition operator above):

Most of these work the way you would expect them to on integers. However, integer division is a bit tricky, since it's possible to divide two integers and not have a whole number as the result (such as 5 / 2). In Java, however, the compiler sees that that only integers are being used, and will perform integer division. That is, the result is rounded down to the nearest integer. Thus, 1/4=0 and 21/6=3. Later, when we work with real numbers, we will see that, to compute a real-valued expression, at least one of the numbers needs to be real-valued (i.e. 5.0 / 2.0 = 2.5)

The modulus (%) operator is probably new for you, and it is used to return the remainder of a division. For example, 5 % 2 evaluates to 1, while 6 % 2 evaluates to 0, because there is no remainder.

When evaluating complex arithmetial expressions, such as 5 + 3 * 2, Java uses the same order of operations as you're used to, and evaluates left-to-right. To be sure, I recommend you use parentheses explicitly to make this order of evaluation and precedence obvious or as needed: (5 + 3) * 2

There are also some binary operators to check for equality:

These work the way you would expect.


Unary Operators

Next, let's consider some unary operators:

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.




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 3: Identify the compiler error:




Real-valued variables

About real numbers:

Just like we did with integers, we can declare variables and assign values to them, using the reserved word double

The short form int corresponds nicely with the English word integer. But double? There is an explanation (below).

One can also use the Java reserved word float as a substitute for double, as in:

float x = 3.14159;
Here, float means English term floating point number.

Think about this:

Back to double:

Hardly anyone uses float. anymore. We'll use double.




Homework 1: due Monday at midnight

Let's go through the first Homework 1 problems together now; there are a few additional details to how homework problems are scored that we should discuss together now, before you learn those pieces later this semester.

There are only five problems in the homework, but problems 2, 4, and 5 can be tricky! You should budget up to two hours for this assignment if you haven't programmed before. When you're working on the problems, stop after 20 minutes on a problem where you haven't made any progress, and create a post on Ed -- we answer these very quickly (usually less than an hour). While you're waiting, you can see if other people got stuck on the same problem (this will almost certainly happen!) and see if any of the answers there help you with your questions. Ed is the quickest and easiest way to receive help fast, and we love to answer your questions there!

Although this homework is graded, you are allowed (and encouraged!) to work in groups. You are also encouraged to help each other out on Ed, which includes getting class participation credit for answering someone else's question. We ask that if you are helping another student, you avoid just giving them your answer, as that doesn't help them learn as much as guiding them toward the correct answer does!

Activity 4: Let's solve Problem 0 on Homework 1 together, so we all understand how to run and use the driver.




Casting

Consider the following program:

Activity 5: What you suppose will be printed? Try it in an open visualizer window. Then, change the program to try the assignment the other way around: initially assign the value of 1 to i and then assign i to x.

An assignment from an int to a double works fine:

About casting:

Activity 6: What do you get when you cast the real value 0.5 to an int? Write code in your Visualizer window to find out.




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!




Next class:

Now that we understand basic numeric types, we'll learn how to tell a program to make a decision using conditional statements.
We will also continue with HW1 problems in lab this Thursday/Monday -- it is due Monday at 11:59pm.

Assignments for next lecture:

Complete all of the homework assignments on variables and numeric types before the next class (HW1 due Monday at 11:59pm), and prepare for the in-lecture quiz on lecture_04 notes on Tuesday.