+ All Categories
Home > Documents > Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning....

Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning....

Date post: 29-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
79
Methods, Parameters, and Scope CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 6
Transcript
Page 1: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Methods, Parameters, and Scope

CS106A, Summer 2019Sarai Gould && Laura Cruz-Albrecht

Lecture 6

Page 2: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Announcements

● Assignment 1 is due at 10am tomorrow morning.○ Practice submitting today to make sure you know how to do it!

● No lecture or sections Thursday for 4th of July. ○ If your section in cancelled, please try to attend a Wednesday

section or Friday’s section (11:30am in Skilling Auditorium)● No LaIR Wednesday, July 3rd due to Holiday

2

Page 3: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Announcements

● Quick Note: If you joined the class late, you will receive your lecture assignments for Lecture Feedback on Monday, July 8th.

3

Page 4: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Duke, the Java mascot!

4

Hi! My name is Duke!

Page 5: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Plan for Today

● Review: Shorthand Operators, Constants, If, While, For● Remember Methods?● Making a Sandwich● A Variable Love Story

5

Page 6: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Review: Shorthand OperatorsShorthand: Equivalent Longer Version:// +, -, /, *, % any value

variable += value; variable = variable + value;

x -= 3; x = x - 3;

y /= 5; y = y / 5;

z *= someValue; z = z * someValue;

k %= 10; k = k % 10;

// add or subtract exactly 1

x++; x = x + 1;

y--; y = y - 1;6

Page 7: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Review: Constantspublic class ReceiptForFive extends ConsoleProgram {

private static final double TAX_RATE = 0.08;

private static final double TIP_RATE = 0.2;

public void run() {

double subtotal1 = readDouble("Meal cost? $");

double tax1 = subtotal * TAX_RATE;

double tip1 = subtotal * TIP_RATE;

double total1 = subtotal1 + tax1 + tip1;

println("Total for person 1: $" + total1);

...

double subtotal5 = readDouble("Meal cost? $");

double tax5 = subtotal * TAX_RATE;

double tip5 = subtotal * TIP_RATE;

double total5 = subtotal1 + tax1 + tip1;

println("Total for person 5: $" + total5);

}

} 7

much better

Page 8: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Review: Relational Operators

8

Operator Meaning Example Value

== equals 1 + 1 == 2 true

!= does not equal 3.2 != 2.5 true

< less than 10 < 5 false

> greater than 10 > 5 true

<= less than or equal to 126 <= 100 false

>= greater than or equal to 5.0 >= 5.0 true

* all have equal precedence

Page 9: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Review: Sentinel Loops// fencepost problem!

// ask for number - post

// add number to sum - fence

int sum = 0;

int num = readInt("Enter a number: ");

while (num != -1) {

sum += num;

num = readInt("Enter a number: ");

}

println("Sum is " + sum);

9

Ask for # Update sum Ask for # Update sum Ask for #

Page 10: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Review: Using the For Loop Variable

10

// Adds up the numbers 1 through 42

int sum = 0;

for (int i = 1; i <= 42; i++) {

sum += i;

}

println(“Sum is ” + sum);

Page 11: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

???

11

I have a question...

Page 12: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

12

Would someone make me a sandwich, please?

Page 13: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

13

Let’s make a method that will make Duke a sandwich!

Page 14: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Methods

A method is a set of new instructions we’ve created!

Example of a comment explaining Pre and Post conditions.

14

/* Method description.

* Pre: What we assume is true beforehand.

* Post: What we promise is true afterwards.

*/

private void nameOfMethod(){

// commands go in here!

}

Page 15: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

15

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich

*/

private void makeASandwich(){

}

Page 16: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

16

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich

*/

private void makeASandwich(){

}

Pretend we’ve added a new primitive variable type called “food”.

Page 17: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

17

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich

*/

private void makeASandwich(food bread, food veg, food protein, food spread){

}

By using parameters we can tell our method what the ingredients for our

sandwich are!

Page 18: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

18

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich.

*/

private void makeASandwich(food bread, food veg, food protein, food spread){

}

Page 19: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

19

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich.

*/

private void makeASandwich(food bread, food veg, food protein, food spread){

food sandwich = bread + veg + protein + spread + bread;

}

Page 20: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

20

Wait, but you never gave me the sandwich. :(

Page 21: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

21

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich.

*/

private void makeASandwich(food bread, food veg, food protein, food spread){

food sandwich = bread + veg + protein + spread + bread;

}

Page 22: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

22

/* We will make Java a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich

*/

private void makeASandwich(food bread, food veg, food protein, food spread){

food sandwich = bread + veg + protein + spread + bread;

return sandwich;}

By using a return statement a method can give back, or return, information

to the rest of your code!

Page 23: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

23

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich

*/

private food makeASandwich(food bread, food veg, food protein, food spread){

food sandwich = bread + veg + protein + spread + bread;

return sandwich;}

Since our method is returning information, it’s no longer void. We change void to the type of

data we are returning!

Page 24: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

24

/* We will make Duke a sandwich.

* Pre: n/a

* Post: We will have a completed sandwich.

*/

private food makeASandwich(food bread, food veg, food protein, food spread){

food sandwich = bread + veg + protein + spread + bread;

return sandwich;}

Page 25: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Making Sandwiches

25

Thanks! 🥪

Page 26: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Methods w/ Parameters and Return Statements

26

/* We will do x, y, and z actions and return methodType data

* Pre: What we assume is true beforehand.

* Post: What we promise is true afterwards.

*/

visibility methodType methodName(paramType paramName){

commands;

return dataThatIsMethodType;}

Page 27: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Methods w/ Parameters and Return Statements

27

visibility methodType methodName(paramType paramName){

commands;

return dataThatIsMethodType;}

Visibility: Usually public or private

Types:methodType is the type of data returned by the method. This is void when the method doesn’t return any data.

paramType is the type of data that particular parameter is. Parameters are information the method needs in order for it to work!

1

1

2

2

3

3

2

Page 28: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Explained Another Way...

A method is like a recipe. You give it a name and tell us what type of thing it will make!

The parameters are the ingredients you need to make it.

You have to return something of the same type that the recipe promised.

If something’s the wrong type, you’re missing ingredients, the recipe promised a cake, but you return spaghetti… something is seriously wrong with your recipe!

28

Page 29: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Explained Another Way...

A method is like a recipe. You give it a name and tell us what type of thing it will make!

The parameters are the ingredients you need to make it.

You have to return something of the same type that the recipe promised.

If something’s the wrong type, you’re missing ingredients, the recipe promised a cake, but you return spaghetti… something is seriously wrong with your recipe!

29

Page 30: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

30

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

Page 31: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

31

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 32: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

32

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

Page 33: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

33

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

hello world

Page 34: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

34

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

hello worldI’m a void method, so I don’t return any data!

Page 35: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

35

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

hello worldI’m a void method, so I don’t return any data!I just do an action.

Page 36: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

36

private void printHello(){

println(“hello world”);println(“I’m a void method, so I don’t return any data!”);println(“I just do an action.”);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 37: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

37

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 38: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

38

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 39: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

39

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 40: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

40

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 41: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

41

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

Page 42: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

42

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

5.0

Page 43: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

43

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

5.0

num2

10.0

Page 44: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

44

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

num2

I can also do actions!

5.0

10.0

Page 45: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

45

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

num2

I can also do actions!I also HAVE to return a double because that’s the method type!

5.0

10.0

Page 46: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

46

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

num2

I can also do actions!I also HAVE to return a double because that’s the method type!

5.0 + 10.0

5.0

10.0

Page 47: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

47

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2;

}

num1

num2

I can also do actions!I also HAVE to return a double because that’s the method type!

sum

15.0

5.0

10.0

Page 48: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

48

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2; // This will equal 7.5

}

num1

num2

I can also do actions!I also HAVE to return a double because that’s the method type!

sum

15.0

5.0

10.0

Page 49: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

49

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2; // This will equal 7.5

}

num1

num2

I can also do actions!I also HAVE to return a double because that’s the method type!

sum

15.0

5.0

10.0

Page 50: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

50

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2; // This will equal 7.5

}

calculateAverage(5, 10)

7.5

I can also do actions!I also HAVE to return a double because that’s the method type!

Page 51: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

51

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2; // This will equal 7.5

}

average

7.5

I can also do actions!I also HAVE to return a double because that’s the method type!

Page 52: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Few Examples

52

public void run(){

double average = calculateAverage(5, 10);println(“The average is: ” + average);

}

private double calculateAverage(double num1, double num2){

println(“I can also do actions!”);println(“I also HAVE to return a double because that’s the method type!”);

double sum = num1 + num2;return sum / 2; // This will equal 7.5

}

average

7.5

I can also do actions!I also HAVE to return a double because that’s the method type!The average is 7.5

Page 53: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Now, Time for a Story

53

Page 54: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Now, Time for a Story

54

It’s time to hear a Variable Love Story.I love Love Stories...

Page 55: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

55

public void run(){

int romeo = 18;

}

romeo was lonely and looking for love...

Page 56: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

56

public void run(){

int romeo = 18;findRomance();

}

What’s this? That sounds great!

*Like a dating site, but for variables

Page 57: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

57

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

“But, soft! what light through yonder window breaks?It is the east, and Juliet is the sun.”~ romeo, probably

Page 58: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

58

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

romeo cannot be resolved to a variable????

Page 59: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

59

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

romeo cannot be resolved to a variable????

Page 60: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

60

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

What about trueLove???

Page 61: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

61

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

romeo only exists between those two brackets!

Page 62: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

62

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

romeo only exists between those two brackets!

Once we enter findRomance, romeo essentially disappears.

How can we make romeo exist inside of findRomance?

Page 63: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

63

What about using parameters?

Page 64: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

64

public void run(){

int romeo = 18;findRomance();

}

private void findRomance(int r){

int juliet = 13;

int trueLove = romeo + juliet;}

We’ll add a parameter to findRomance!

This means findRomance needs more information (aka an extra ingredient) in order to work.

Page 65: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

65

public void run(){

int romeo = 18;findRomance(romeo);

}

private void findRomance(int r){

int juliet = 13;

int trueLove = r + juliet;}

At the beginning of the code, we’ll pass romeo into our findRomance method.

Now, our parameter r will be equal to what romeo was equal to!

So, r + juliet is essentially equal to romeo + juliet.

Page 66: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

66

public void run(){

int romeo = 18;findRomance(romeo);

}

private void findRomance(int r){

int juliet = 13;

int trueLove = r + juliet;}

What does this mean?

Page 67: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story

67

public void run(){

int romeo = 18;findRomance(romeo);

}

private void findRomance(int r){

int juliet = 13;

int trueLove = r + juliet;}

trueLove at last!

*We do not condone relationships of 13 and 18 year olds. Blame Shakespeare.

Page 68: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

68

public void run(){

int newVariable = 0; // I am born!.........

} // I hope I haven’t bored you. Goodbye.

Scope is the idea that variables only exist inside a certain block of code.

In Java, a variable is born when it is declared.

A variable terminates when it hits the ending bracket of the code block in which it was declared.

Page 69: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

69

The variable only exists from its declaration to the end of its current code block.

while (conditionAIsTrue){

if (conditionBIsTrue){

...

int newVariable = 0; // I am!...

} // I’m bored with it all. Goodbye.

}

int newVariable = 0; // I am born!...

Page 70: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

70

This won’t work!while (conditionAIsTrue){

if (conditionBIsTrue){

...

int newVariable = 0; // I am!...

} // I’m bored with it all. Goodbye.

println(newVariable);}

int newVariable = 0; // I am born!...

Page 71: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

71

It doesn’t exist before it’s declared, and it doesn’t exist outside of its current code block!

while (conditionAIsTrue){

if (true){

...

int newVariable = 0; // I am born!...

} // I’m bored with it all. Goodbye.

}

while (conditionAIsTrue){

if (conditionBIsTrue){

...

println(newVariable);

Page 72: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

72

int romeo = 18;

public void run(){

findRomance();}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

Why can’t we just declare romeo outside of any brackets?

Page 73: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

73

int romeo = 18;

public void run(){

findRomance();}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

Why can’t we just declare romeo outside of any brackets?

1. It’s harder to understand.

Page 74: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

74

int romeo = 18;

public void run(){

findRomance();}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

Why can’t we just declare romeo outside of any brackets?

1. It’s harder to understand.2. More likely to accidentally

introduce bugs.

Page 75: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

75

int romeo = 18;

public void run(){

findRomance();}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

Why can’t we just declare romeo outside of any brackets?

1. It’s harder to understand.2. More likely to accidentally

introduce bugs.3. We don’t get rid of romeo

when we’re done with romeo.

Page 76: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

76

int romeo = 18;

public void run(){

findRomance();}

private void findRomance(){

int juliet = 13;

int trueLove = romeo + juliet;}

Don’t do this!

Page 77: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

A Variable Love Story: Scope

77

public void run(){

int romeo = 18;findRomance(romeo);

}

private void findRomance(int r){

int juliet = 13;

int trueLove = r + juliet;}

Do this!

It’s easier to read and debug, and we remove variables from memory when we’re done with them!

Page 78: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Plan for Today

● Review: Shorthand Operators, Constants, If, While, For● Remember Methods?● Making a Sandwich● A Variable Love Story

78

Page 79: Methods, Parameters, and Scope...Announcements Assignment 1 is due at 10am tomorrow morning. Practice submitting today to make sure you know how to do it! No lecture or sections Thursday

Reminders

● Assignment 1 is due at 10am tomorrow morning.○ Practice submitting today to make sure you know how to do it!

● No lecture or sections Thursday for 4th of July. ○ If your section in cancelled, please try to attend a Wednesday

section or Friday’s section (11:30am in Skilling Auditorium)● No LaIR Wednesday, July 3rd due to Holiday

79


Recommended