CS I · Wed Nov 11 · 15.3 · We Do

Before the bell

You are responsible for all of this before the end of class.

  1. The discussion form in the I Do deck. All three sections. If you are here, this should be done.
  2. Your classwork. This form on the right. Section 1 before the exit ticket pops up; Section 2 past the floor.
  3. Today's Bench Basics activity: Java Math. It opens later in class and lives on slide 4 of this deck.
  4. The exit ticket. It opens 15 minutes before the bell and lives on the last slide of this deck.
Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

Java Math: Whole Numbers, Remainders and Decimals

This reading explains how Java performs division with whole numbers and with decimals, and then it works through one complete program line by line.

Dividing in Java

On the B06 bench, 17 ÷ 5 was 3 remainder 2, and Java calculates the same two answers with two separate operators. When both numbers are int values, the / operator performs integer division, which keeps only the whole-number part of the quotient and discards the rest. The remainder operator, written as a percent sign, calculates the amount that is left over after that division. If either number is a double, Java converts the other number to a double as well, so the answer is a double and the decimal part is kept. Java also provides two shortcuts, called compound assignment and increment, for changing the value stored in a box. The line total += 3; means the same as total = total + 3;, and the line total++; adds 1 to total.

One Program, Line by Line

The program below uses each of these rules, and the trace table after its output records every box after each line runs. Its lines are numbered starting from int total, so line 1 is the first line inside main, and an empty cell in the table means that the box does not exist yet.

1public class Main {
2    public static void main(String[] args) {
3        int total = 17;
4        int groups = total / 5;
5        int left = total % 5;
6        double share = total / 5.0;
7        double trap = total / 5;
8        total += 3;
9        total++;
10        System.out.println(groups);
11        System.out.println(left);
12        System.out.println(share);
13        System.out.println(trap);
14        System.out.println(total);
15    }
16}
3
2
3.4
3.0
21
Line | total | groups | left | share | trap
1    | 17    |        |      |       |
2    | 17    | 3      |      |       |
3    | 17    | 3      | 2    |       |
4    | 17    | 3      | 2    | 3.4   |
5    | 17    | 3      | 2    | 3.4   | 3.0
6    | 20    | 3      | 2    | 3.4   | 3.0
7    | 21    | 3      | 2    | 3.4   | 3.0

Line 1 stores 17 in total. Line 2 divides 17 by 5 using two int values, so groups stores 3 and the remainder is discarded. Line 3 uses the remainder operator, so left stores 2, which is the amount left over after three groups of 5. Line 4 divides by 5.0, which is a double, so share stores 3.4. Line 5 stores 3.0 in trap, for a reason explained in the next section. Line 6 adds 3 to total, which becomes 20, and line 7 adds 1 more, so total becomes 21. Finally, lines 8 to 12 print the five values in order, one on each line of the output.

The Trap the Bench Catches

The box named trap is a double, but it stores 3.0 and not 3.4. Java always calculates the right side of the = first, and total / 5 uses two int values, so the answer is already 3 before it is stored. When 3 is stored in a double box, it becomes 3.0, and the lost 0.4 does not come back. To keep the decimal part, at least one number on the right side has to be a double, as on line 4, because the type of the box on the left does not change how the division is calculated. Code Coach's second test input, int x = 7 / 2;, is the same trap, and x stores 3.

Sources: Oracle, The Java Language Specification, Java SE 21 Edition, Chapters 5 and 15 (n.d.); Oracle, Assignment, Arithmetic, and Unary Operators (n.d.); Code Coach test input t2 and the day's Java runs (classroom data, 2026).

Repositories, Diffs and Change Logs

This section explains where a project's history is kept, how to see exactly which lines one commit changed, and how a change log differs from that history.

What a Repository Holds

The previous lesson explained that each commit records the state of the whole project along with a message that describes the change, and Git stores all of those commits together in one place. A repository is a project's files together with their full history. Git keeps that history inside the project directory, in a hidden folder named .git, which stores every commit and every message. Because the whole history is stored there, a person can open any earlier version without asking another computer for it. For example, the class's Stops example is one repository, and it holds the current Stops.java along with the three commits that led to it. A folder of Java files on one Chromebook is not a repository, because it holds the files and none of their history. Nothing in that folder records what a file contained last week or why it was changed.

A repository can live on a single computer, and a copy of it can also be kept on a server, where the people who are allowed can reach it from any computer. Being on a server is not what makes a project a repository, since the files and their history together are what make it one. In this class, the example repository is the teacher's, and students read its history without making commits of their own.

How to Read a Diff

A history lists every commit, but a message alone does not show what changed in the code. A diff shows what changed between two versions of a file, usually one commit and the commit just before it. It shows the lines that were added and the lines that were removed. A removed line starts with a minus sign, and an added line starts with a plus sign. A line with no sign at the beginning is unchanged. Git shows three of those unchanged lines above and below each change by default, so that a reader can find the place in the file. The line that starts with @@ gives the line numbers where the change is.

Figure 1 is the real diff of commit 3 in the Stops example. It has one line removed, int stops = 5;, and one line added, int stops = 8;. This diff answers the Board Question, because Stops.java has the same number of lines as before, so counting lines would not locate the change, while the diff points directly at the one line that is different. The diff of commit 2, shown below, has two lines added and none removed.

diff --git a/Stops.java b/Stops.java
index 9f7ccaf..181b961 100644
--- a/Stops.java
+++ b/Stops.java
@@ -2,5 +2,7 @@ public class Stops {
     public static void main(String[] args) {
         int stops = 5;
         System.out.println(stops);
+        stops = stops - 1;
+        System.out.println(stops);
     }
 }

A diff has two limitations. First, it does not show the whole file again from top to bottom, because it shows only the changed lines and a few lines around them. Second, it does not explain why the change was made. The commit message says why, and the message for commit 3 is "Start at 8 stops because the route has 8."

What a Change Log Is For

Git makes the history and the diff, but a third record is written by a person. A change log is a list of the notable changes in a project, newest first, with the date of each version, written for the people who use the project. The history lists every commit exactly as it was made, while a change log is written afterward and keeps only the information a reader needs. For example, the change log for the Stops example has one line for Nov 4, which reads "The countdown now starts at 8." Git did not produce that line, because a person wrote it for someone who uses the program and does not need to read the code. The guide Keep a Changelog advises against using a list of commits as a change log. Such a list is full of noise, such as commits with unclear titles, that a user does not need.

These three records serve three different audiences. The history is for anyone tracing how the work went, one commit at a time. The diff is for anyone checking one change line by line. The change log is for someone who uses the program and wants to know what is different about it.

Summary

A repository is a project's files together with their full history, and it can live on one computer or be copied to a server. A diff shows the lines that one commit added and removed, with a minus sign on each removed line and a plus sign on each added line. A change log is written by a person and lists only the notable changes, newest first, for the people who use the project.

Sources: Chacon and Straub, Pro Git, sections 1.3, 2.1, 2.2, 2.3 and 2.5 (2014); Git project, git-diff Documentation (n.d.); GNU Project, Detailed Description of Unified Format (n.d.); Lacan, Keep a Changelog 1.1.0 (n.d.); the Stops example repository (classroom data, 2026).

Classwork · Section 1 — Reading 2

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

MC 1. According to the passage, what does a repository hold?

  • Only the newest version of each file, kept on a server
  • A project's files together with the full history of their commits
  • A list of the notable changes, newest first, written for the people who use the project
  • A folder of Java files on one Chromebook, saved as they are now

MC 2. In Figure 1, the line int stops = 5; starts with a minus sign. What does the minus sign mean?

  • The line subtracts a number from stops when the program runs.
  • The line was added in the new version of Stops.java.
  • The line was in the old version and was removed in this commit.
  • The line did not change and is shown only so a reader can find the place.

MC 3. Here is the real diff of a practice change to Stops.java. How many lines did this commit remove, and how many did it add?

diff --git a/Stops.java b/Stops.java
index ef9aef9..6b287ff 100644
--- a/Stops.java
+++ b/Stops.java
@@ -2,7 +2,8 @@ public class Stops {
     public static void main(String[] args) {
         int stops = 8;
         System.out.println(stops);
-        stops = stops - 1;
+        stops -= 2;
+        stops++;
         System.out.println(stops);
     }
 }
  • 1 line removed and 2 lines added
  • 2 lines removed and 1 line added
  • 0 lines removed and 3 lines added
  • 1 line removed and 7 lines added

MC 4. How is a change log different from a project's history?

  • Git writes the change log on its own each time a commit is made.
  • The change log lists every commit, oldest first, exactly as it was made.
  • A person writes the change log afterward and keeps only the notable changes for the people who use the project.
  • The change log shows the exact lines that each commit added and removed.

MC 5. What does this code print?

1System.out.println(30 / 8);
2System.out.println(30 % 8);
  • 3.75, then 0
  • 3, then 6
  • 3, then 0.75
  • 6, then 3

MC 6. What does this code print?

1System.out.println(9 / 2);
2System.out.println(9 / 2.0);
  • 4.5, then 4.5
  • 4, then 4
  • 4, then 4.5
  • 4.0, then 4.5

MC 7. What value is stored in score after these three lines run?

1int score = 12;
2score += 5;
3score += score;
  • 17
  • 22
  • 29
  • 34

MC 8. What value is stored in laps after these four lines run?

1int laps = 6;
2laps++;
3laps++;
4laps = laps * 2;
  • 16
  • 14
  • 12
  • 8

MC 9. Which box stores 3.5?

1int a = 7;
2int b = a / 2;
3double c = a / 2.0;
4double e = a / 2;
  • b, on line 2
  • c, on line 3
  • e, on line 4
  • None of them, because a is an int

MC 10. A carton holds 12 eggs. What does this code print?

1int eggs = 26;
2int cartons = eggs / 12;
3int extra = eggs % 12;
4System.out.println(cartons);
5System.out.println(extra);
  • 2, then 2
  • 2.17, then 2
  • 2, then 24
  • 3, then 10

CR 1. Trace the snippet below in a table with a Line column, one column for each box and an Output column. Fill one row for each line, then give what the snippet prints.

1int pages = 50;
2int full = pages / 8;
3int left = pages % 8;
4full += left;
5System.out.println(full);

Write your answer in your lab notebook, labeled CR 1.

CR 2. Explain in two sentences why `double trap = 17 / 5;` stores 3.0 and not 3.4.

Write your answer in your lab notebook, labeled CR 2.

Figure 1. The real diff of commit 3 in the Stops example, with its commit message on top, the removed line tinted rust and the added line tinted green.
Classwork · Section 2 — Past the floor

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

CR 3. Here is the real diff of another practice change to Stops.java. Its commit message is "Add the 5 new stops and split them between 2 buses." Say what the program printed before this change and what it prints after it, and explain how you used the minus and plus lines to work it out.

diff --git a/Stops.java b/Stops.java
index ef9aef9..9f1b993 100644
--- a/Stops.java
+++ b/Stops.java
@@ -2,7 +2,8 @@ public class Stops {
     public static void main(String[] args) {
         int stops = 8;
         System.out.println(stops);
-        stops = stops - 1;
-        System.out.println(stops);
+        stops += 4;
+        stops++;
+        System.out.println(stops / 2);
     }
 }

Write your answer in your lab notebook, labeled CR 3.

CR 4. The change in CR 3 was committed on Nov 12. Write a one-line change log entry for it, for a person who uses the program and never reads the code. Include the date and a reason.

Write your answer in your lab notebook, labeled CR 4.

Bench Basics

Java Math

Open the bench, do its six jobs, then paste your credit code into the credit-code form. You can turn in up to three codes per bench; your best score is your grade.

Bench Basics opens during your class period.

Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

Exit Ticket

This opens during your class period.