KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Operations on Variables

int i = 0;
System.out.println(i);
i++;
System.out.println(i);
int j = 10;
i = i + j;
j = i - j;
System.out.println(i + " " + j);

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.

Reminder: Variables
Reminder: Variables

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:

// Dear Java: I'm going to use a variable named catsAge to store integer values
// And I'd like its initial value to be 6
int catsAge = 6;

If you’re having any trouble understanding variables, please review the previous lesson, or attend one of our concept review sessions.

Comments
Comments

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:

// This is a single line comment. It starts with //
/*
This is a multi-line comment. It starts with the two-character symbol on the line above.
And it ends with the two-character symbol on the line below.
*/

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.

Practice: Area Calculation

Created By: Geoffrey Challen
/ Version: 2020.6.0

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

int area = width + height;

Modifying Variables
Modifying Variables

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:

int i = 10 * 20;

Variables can also be set based on the value of other variables:

int i = 10;
int j = 20;
int k = i + j;

Understanding Variable Assignment
Understanding Variable Assignment

Some of you, particularly those with a mathematical bent, may be alarmed by certain lines of Java code. Like this one:

int i = 10;
i = i + 1;
System.out.println(i);

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:

int i = 1;
i = i + 1;
System.out.println(i);

Practice: Fall 2021 Grade Calculation

Created By: Geoffrey Challen
/ Version: 2021.8.0

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!

double grade = (30 * quizScore) + (30 * homeworkScore) + (30 * mpScore);

Self-Modification Shortcuts
Self-Modification Shortcuts

It’s fairly common in our programs to want to modify the value of a variable but start with its current value. For example:

int dollars = 0;
// Call parents
dollars = dollars + 100;
// Phew...
System.out.println(dollars);

These types of operations are common enough that there are a few shortcuts that you can use:

int dollars = 0;
// Call parents
dollars += 100;
// Phew...
System.out.println(dollars);

Similarly you can use -=, *=, and /=.

Finally, adding and subtracting by one are so common that they even have an additional set of shortcuts:

int dollars = 0;
// Call parents
dollars += 1;
// WTF?
System.out.println(dollars);
// Call parents again
dollars++;
System.out.println(dollars);
// ...

Similarly you can use ++ and --.

Binary Negation
Binary Negation

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:

boolean isCS124Awesome = false;
System.out.println(isCS124Awesome);
// Extra credit?
isCS124Awesome = !isCS124Awesome;
System.out.println(isCS124Awesome);

Practice: Fall 2022 Grade Calculation

Created By: Geoffrey Challen
/ Version: 2022.8.0

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!)

double grade = (30 * quizScore) + (30 * homeworkScore) + (30 * mpScore);

Course Design: Frequent Assessment
Course Design: Frequent Assessment

CS 124 is designed to give every student a chance to succeed. Below we explain one unusual aspect of the course. You can find more information on the syllabus.

CS 124 does not hold a final exam. Or a midterm. Or any high-stakes assessments.

High-stakes assessments like midterms and final exams encourage and reward cramming, which does not lead to knowledge retention. Infrequent assessment also allows students to go long periods of time without engaging with the content. We want you studying a small amount every day. So we assign a daily homework problem, and have you take a weekly quiz.

Frequent small assessment may be the most important component of student success in CS 124. We could fill pages about all of the benefits of this approach(1). When you study, you learn. Preparing for CS 124 quizzes will solidify your knowledge. Taking the quizzes will test your understanding, and provides important feedback for both you and us about what you know.

When you do poorly on a quiz, you have time to catch up, and should expect us to reach out to help as well. Many small quizzes means that each is less stressful, and allows you to recover from a bad assessment. It also allows us to implement learning-focused grading policies, including dropping low scores and catch-up grading.

Homework Problem
Homework Problem

As a reminder, CS 124 now allows students to collaborate freely on the homework problems. Please see the syllabus for more details.

Created By: Geoffrey Challen
/ Version: 2024.1.0

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 extraCredit. quizScore, homeworkScore, and mpScore will be between 0 and 1. extraCredit will be between 0 and 1. Quizzes, homework, and the MP are worth 50%, 20%, and 30% of your grade, respectively. Extra credit should be doubled (we're nice like that) and added directly to the result. A course staff member wrote the following method to calculate your grade as a percentage. However, it has a few small mistakes which you will want to correct!

double grade = (60 * quizScore) + (10 * homeworkScore) + (30 * mpScore) - extraCredit;

CS People: Alan Turing
CS People: Alan Turing

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(2) 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.

More Practice

Need more practice? Head over to the practice page.