1 of 22

Lecture 3:

Scope

CS 136: Spring 2024

Katie Keith

2 of 22

Record on Zoom

3 of 22

  • Recordings, Code, Slides on course webpage
  • Please stay home if you’re sick!

📣 Announcements

4 of 22

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

5 of 22

  • Please be sure to read the entire README.md
  • Lab 0 due Monday at 10pm (for Weds labs) or Tuesday at 10pm (for Thurs labs)
    • Upload your final program (Hello.java) to Gradescope and it should look something like this when you’re done:

📣 Announcements

6 of 22

  • Inputs
  • Scope

🎯 Today’s Learning Objectives

7 of 22

📚Readings

  • Sedgewick & Wayne. Section 2.1

8 of 22

Basic building blocks for programming

primitive data types

assignment statements

Java’s built-in

Math library

input /output

conditionals and loops

arrays

objects

Any program you might want to write

9 of 22

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

10 of 22

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

11 of 22

DivisorPattern.java

Take any n from the user (on the command line)

💻

12 of 22

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

13 of 22

  • Inputs
  • Scope
  • Reference Types

🎯 Today’s Learning Objectives

14 of 22

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.

15 of 22

Harmonic.java

💻

16 of 22

A static method

Recall, a static method is method that belongs to the entire class.

17 of 22

A static method

Recall, a static method is method that belongs to the entire class.

18 of 22

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

19 of 22

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)

}

20 of 22

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.

21 of 22

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

22 of 22

  • Inputs
  • Scope

🎯 Today’s Learning Objectives