Next we’ll continue introducing the basic building blocks of our computer programs. We’ll show how you can manipulate the data stored by your variables through simple mathematical operations.
In the last lesson we introduced variables, which allow us to store changing values as our program runs. We looked at how to declare and initialize those variables. The declaration tells Java the name and type of the variable we are going to use, and the initialization sets the variable’s first value:
If you’re having any trouble understanding variables, please review the previous lesson, or attend one of our concept review sessions.
Before we continue, we need to introduce one new piece of Java syntax: comments. Comments allow us to include descriptive text in our programs. Comments are completely ignored by the computer! They are entirely for the humans who write, read, and maintain the code.
Java supports two types of comment:
Single-line comments are good when you have something short to say about something in your code.
Multi-line comments that start with /*
and end with */
are better when you have more to say and need to write one or more paragraphs.
Even though comments are ignored by the computer, they are essential to writing good computer code. We’ll use comments frequently in our examples, and show you how you can use them to outline a plan for completing more complex programming tasks.
Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful.
Your friend wrote a simple function intended to save the area of a room in a variable called area given its width and height stored in int variables width and height. Unfortunately, their code has a small error. Go ahead and fix it for them! (Note that you do not need to declare or modify width and height. But you should declare and set area.)
Now that we can store data in our programs we can harness the next fundamental computer superpower: math. Computers can do math, quickly and with (near) perfect accuracy. Many of the operations that we can perform on variables are mathematical in nature:
Variables can also be set based on the value of other variables:
Some of you, particularly those with a mathematical bent, may be alarmed by certain lines of Java code. Like this one:
In math, the statement on the second line is non-sensical.
Regardless of the value of i
, it can never equal itself plus one—nonsense!
The source of the confusion here is the =
symbol.
In Java, a single =
does not indicate or test equality.
It indicates assignment.
We use it when we want to assign a value to a variable.
To further the confusion, assignments in Java are evaluated from right to left. So they aren’t math nor English! That’s why the example above works.
Let’s examine this assignment carefully together:
Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful. Particularly when computing something important, like your grade!
Your grade in CS 124 is based on three components, which are already stored in double
variables: quizScore
,
homeworkScore
, and mpScore
, each as numbers between 0 and 1.
Quizzes, homework, and the MP are worth 40%, 30%, and 30% of your grade, respectively.
A course staff member wrote the following method to calculate your grade as a percentage between 0 and 100.
However, it has a small mistake which you will want to correct!
It’s fairly common in our programs to want to modify the value of a variable but start with its current value. For example:
These types of operations are common enough that there are a few shortcuts that you can use:
Similarly you can use -=
, *=
, and /=
.
Finally, adding and subtracting by one are so common that they even have an additional set of shortcuts:
Similarly you can use ++
and --
.
What we’ve been covering in this lesson are known as operators. (We’ve covered the first half of the linked table. We’ll get to the rest soon!) Almost all of them are for working with numbers. Since, to a computer, everything is a number.
But there is one operator that we use to work with boolean
values:
Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful. Particularly when computing something important, like your score!
Your grade in CS 124 is based on three components, which are already stored in double
variables: quizScore
,
homeworkScore
, and mpScore
, each as numbers between 0 and 1.
We also add a few points of extra credit, stored in an int
variable extraCredit
.
Quizzes, homework, and the MP are worth 40%, 30%, and 30% of your grade, respectively.
A course staff member wrote the following method to calculate your grade as a percentage between 0 and 100.
However, it has a few small mistakes which you will want to correct!
(Don't forget to include the extra credit!)
As a reminder, CS 124 now allows students to collaborate freely on the homework problems. Please see the syllabus for more details.
Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful. Particularly when computing something important, like your grade!
Your grade in CS 124 is based on four components, which are already stored in double
variables: quizScore
,
homeworkScore
, mpScore
, and projectScore
, each as numbers between 0 and 1.
(Don't worry: It's really a mini project.)
Quizzes, homework, the MP, and the project are worth 40%, 30%, 22.5%, and 7.5% of your grade, respectively.
A course staff member wrote the following method to calculate your grade as a percentage between 0 and 100.
However, it has a few small mistakes which you will want to correct!
As we teach you the basics of computer science and programming, we’re also going introduce you to computer science people—the humans behind technology’s remarkable achievements. Note that we include a few questions on each quiz on these short videos, so don’t skip them!
The name Turing comes up a lot in computer science. There’s the Turing machine, an abstract model of a machine that, despite its simplicity, can compute any algorithm(1) There’s the Turing test to determine whether a machine can be distinguished from a human. And there’s the Turing Award—computer science’s highest honor, and its equivalent of the Nobel Prize.
But who was Alan Turing, and why did his life end tragically at the age of only 41? Watch the video below to find out more.
Note that the British Government formally apologized to Alan Turing—but not until 2009, 55 years after his death.
Business Council is a professional organization on campus that welcomes all majors that focuses on developing well-rounded individuals through professional, social, and service events to explore their passions in any given career. Their information night is Monday 1/23/2023 at 7 pm at Foellinger Auditorium!
Need more practice? Head over to the practice page.