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.

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 arithmetical 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:




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.




Homework 1: due Tuesday at before 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!

Finally, please do not use ChatGPT (or another LLM) on homework assignments. You are not learning much from doing that, and worse, you're not learning to debug at all (which is what you'll spend most of your career doing as a developer). You will have a tough time on the quizzes this semester if you don't get enough individual practice writing and debugging your own code (this goes for groupwork as well).

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




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.


What's in our toolbox at this point:

Our current toolbox by this lecture


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.