Lecture 3:
Scope
CS 136: Spring 2024
Katie Keith
Record on Zoom
📣 Announcements
Weekly routine
Wed.
Thurs.
Fri.
Sun.
Mon.
Tues.
Sat.
Lab due 10pm for Weds labs
Plus
Quiz 1 (Feb 28)
Midterm Exam (March 20)
Final Project
Final Exam
Honor
Code
Two Late Days on Labs
*Max 1 per lab
Start Early!
Lab released
Attend lab
Attend lab
Lab due 10pm for Thurs labs
Talk to me after class if you have quiz/test accommodations
📣 Announcements
🎯 Today’s Learning Objectives
📚Readings
Basic building blocks for programming
primitive data types
assignment statements
Java’s built-in
input /output
conditionals and loops
arrays
objects
Any program you might want to write
Modulo operator (%)
x % y is referred to as “x modulo y” and returns the remainder
int foo = 17 % 5;
Caution for negative numbers: % returns the remainder, so the result will be the same sign as the dividend
int foo = -1 % 3;
Instead, Math.floorMod is a true modulo operator whose result will be the same sign as the divisor
int foo = Math.floorMod(-1, 3);
Here, foo will be equal to 2 since 17/5 is 3 remainder 2
Here, foo equals 2
Here, foo equals -1
Fill in the code such that it prints the table below, where “*” marks when one number divides evenly into another number.
Stars printed because 1, 2, 3, 6 all divide into 6.
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i % j == 0 || j % i == 0) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println(i);
}
Note: Symmetrical!
Row with all stars 1 divides everything
1
2
3
4
5
6
7
1 2 3 4 5 6 7
💡Think-pair-share
DivisorPattern.java
Take any n from the user (on the command line)
💻
Java: Command-line input arguments
public class DivisorPattern{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println(n);
...
}
}
% javac DivisorPattern.java
% java DivisorPattern 10
Terminal
Java
obtains the first argument the user types
cast the string to an integer
✅
🎯 Today’s Learning Objectives
Task: Print the nth harmonic number
The nth harmonic number is the sum of the reciprocals of the first n natural numbers.
Let’s take n from the user
Fun math fact: this approximates the natural logarithm function.
Harmonic.java
💻
A static method
Recall, a static method is method that belongs to the entire class.
A static method
Recall, a static method is method that belongs to the entire class.
Calling a static method
public static void main(String[] args) {
int userN = Integer.parseInt(args[0]);
double value = harmonic(userN);
System.out.println("The "+userN+"th harmonic number is "+value);
}
method call
argument to the method
Control flow
public static double harmonic(int n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
return sum;
}
public static void main(String[] args) {
int userN = Integer.parseInt(args[0]);
double value = harmonic(userN);
System.out.println("The "+userN+"th harmonic number is "+value);
}
public class Harmonic {
1
2
3
4
5
Control flow refers to the order in which the program executes statements
(e.g., 1 then 2 then 3 then 4 then 5)
}
Scope
public static double harmonic(int n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
return sum;
}
public static void main(String[] args) {
int userN = Integer.parseInt(args[0]);
double value = harmonic(userN);
System.out.println("The "+userN+"th harmonic number is "+value);
}
public class Harmonic {
}
Scope of sum and n
This method cannot access or refer to sum or n
Scope is the region within a program where a defined variable or method is accessible and can be used.
public static void negate(int a){
a = -1*a;
}
public static void main(String[] args){
int a = 17;
System.out.println(a);
negate(a);
System.out.println(a);
}
What is printed here?
💡Think-pair-share
✅
✅
🎯 Today’s Learning Objectives