Introduction to Software Development
GWU Computer Science
And, once we've worked with integers, we'll also do some "number crunching".
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:
Now, let's talk about how Java (and other programming languages store the most basic type of values: numbers.
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:
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.
Next, let's consider some unary operators:
Activity 2: Let's trace through this code in the Visualizer.
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:
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.
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:
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.Our current toolbox by this lecture