welcome back! - computer science and...

497
Welcome Back!

Upload: others

Post on 01-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Welcome Back!

Page 2: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Welcome to Java!

Page 3: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

What is Java?

● Java is an industrial programming language used to build large applications.

● Used in web servers, Android phones, desktop applications, etc.

● Extremely common: probably the second-most-widely used language in professional software engineering.

Page 4: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Transitioning to Java

● For the remainder of Girl Code, we'll be using the Java programming language.

● Java and Alice have many things in common:● Methods● Parameters● Control Statements (while, if, etc.)● Events

● Although the languages look different, you'll see these parallels as we keep going.

Page 5: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Our First Java Program

Page 6: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Running This Program

● Downloading and running programs in Java is different from running them in Alice.

● Don't worry, we'll work through it together!

Page 7: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step One: Download the Project

Go to http://bit.ly/girlcode andclick on Java Projects.

Page 8: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step Two: Extract the Files

Double-click the .zip file to extract the folder. Don't click on any of the files just

yet – we'll get there in a second!

Page 9: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step Three: Move the Files

Copy the directory you extracted somewhere convenient (we recommend the

desktop.)

Page 10: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step Four: Import the Project

Click on the Import button,which looks like this:

Navigate to the folder Java01and click Finish.

Page 11: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step Five: Open the Source Files

Navigate in the project to “Default Package” and double-click on the Java

source file AddTwoIntegers.java

Page 12: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Step Six: Run the Program!

Click on the icon of a little running guy. The program should start running!

Page 13: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Dissecting our Program

Page 14: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 15: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 16: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

This Java code needs to be in every program we'll write in these next two weeks. Because of this, it's often called boilerplate code.

We'll provide this code for you so

This Java code needs to be in every program we'll write in these next two weeks. Because of this, it's often called boilerplate code.

We'll provide this code for you so that you don't have to type it.

Page 17: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

This is the name of the program. Since this program

adds two integers, I've named it “AddTwoIntegers

This is the name of the program. Since this program

adds two integers, I've named it “AddTwoIntegers.”

Page 18: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 19: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 20: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}} This is the run

method. It's the actual

This is the run method. It's

the actual program to run.

Page 21: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Each of these lines of code is called a statement. Each

Each of these lines of code is called a statement. Each

statement ends with a semicolon.

Page 22: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

The println method (print line) displays a line of text on the

screen. The quoted text in the parentheses is the argument to

The println method (print line) displays a line of text on the

screen. The quoted text in the parentheses is the argument to

the method.

Page 23: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

These statements are called variable declarations. They allow

us to give names to quantities (here, the first two numbers

These statements are called variable declarations. They allow

us to give names to quantities (here, the first two numbers

entered and their sum).

Page 24: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}} These are comments, just

like in Alice. (Notice that

These are comments, just like in Alice. (Notice that

they still start with //).

Page 25: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Working with Variables

● The previous program declared three variables: n1, n2, and sum.

● In the previous example, we used these variables to keep track of values that the user entered and to store information for later on.

● Variables are very important in Java, so we'll start with a quick overview of how to use them.

Page 26: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

Page 27: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

Page 28: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:

Page 29: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?

Page 30: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?● Value: What value does the variable have at any

particular moment in time?

int numVoters

Page 31: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?

int numVoters

Page 32: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?● Value: What value does the variable have at any

particular moment in time?

int numVoters

Page 33: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?● Value: What value does the variable have at any

particular moment in time?

int numVoters

Page 34: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

● A variable is a location where a program can store information for later use.

● Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?● Value: What value does the variable have at any

particular moment in time?

137 int numVoters

Page 35: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables

A variable is a location where a program can store information for later use.

Each variable has three pieces of information associated with it:● Name: What is the variable called?● Type: What sorts of things can you store in the

variable?● Value: What value does the variable have at any

particular moment in time?

137 int numVoters

Page 36: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 37: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 38: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 39: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 40: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 41: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 42: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x w7thBookInTheSeries LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit voidlots_of_underscores C_19_H_14_O_5_S

Page 43: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Names

● Legal names for variables● begin with a letter or an underscore (_)● consist of letters, numbers, and underscores,

and● aren't one of Java's reserved words.

x wLOUD_AND_PROUD

noOrdinaryRabbit lots_of_underscores C_19_H_14_O_5_S

Page 44: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Naming Conventions

● You are free to name variables as you see fit, but there are some standard conventions.

● Names are often written in lower camel case:

capitalizeAllWordsButTheFirst

Choose names that describe what the variable does.

If it's a number of votes, call it numberOfVotes, numVotes, votes, etc.

Don't call it x, volumeControl, or severusSnape

Page 45: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Naming Conventions

● You are free to name variables as you see fit, but there are some standard conventions.

● Names are often written in lower camel case:

capitalizeAllWordsButTheFirst

Choose names that describe what the variable does.

If it's a number of votes, call it numberOfVotes, numVotes, votes, etc.

Don't call it x, volumeControl, or

Page 46: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Naming Conventions

● You are free to name variables as you see fit, but there are some standard conventions.

● Names are often written in lower camel case:

capitalizeAllWordsButTheFirst

Choose names that describe what the variable does.

If it's a number of votes, call it numberOfVotes, numVotes, votes, etc.

Don't call it x, volumeControl, or

Page 47: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Naming Conventions

● You are free to name variables as you see fit, but there are some standard conventions.

● Names are often written in lower camel case:

capitalizeAllWordsButTheFirst ● Choose names that describe what the variable does.

● If it's a number of voters, call it numberOfVoters, numVoters, voters, etc.

● Don't call it x, volumeControl, or severusSnape.

Page 48: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:

Page 49: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers.

Page 50: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers.● double: Real numbers.

Page 51: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers.● double: Real numbers.● boolean: Logical true and false.●

Page 52: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers. (counting)● double: Real numbers.● boolean

Page 53: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers. (counting)● double: Real numbers. (measuring)

Page 54: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Types

● The type of a variable determines what can be stored in it.

● Java has several primitive types that it knows how to understand:● int: Integers. (counting)● double: Real numbers. (measuring)● boolean: Logical true and false.● (Plus a few more)

Page 55: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Values

137 int numVotes

0.97333 double fractionVoting

0.64110 double fractionYes

Page 56: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Declaring Variables

● In Java, before you can use a variable, you need to declare it so that Java knows the name, type, and value.

● The syntax for declaring a variable is

type name = value; ● For example:

● int numVotes = 137;● double pricePerPound = 0.93;

Page 57: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the summationprintln("The sum of those numbers is " + sum);

}}

Page 58: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the summationprintln("The sum of those numbers is " + sum);

}}

Page 59: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Reading Values

● You can prompt the user for a value by using the readInt and readDouble methods.

● For example:int numBunnies = readInt("How many bunnies? ");

double weight = readDouble("Each bunny weighs? ");

● Notice that there's a space at the end of each of the prompts – we'll see why in a second.

Page 60: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the summationprintln("The sum of those numbers is " + sum);

}}

Page 61: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the summationprintln("The sum of those numbers is " + sum);

}}

Page 62: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Expressions

● Variables and other values can be used in expressions.

● Some familiar mathematical operators:● + (addition)

● – (subtraction)

● * (multiplication)

● / (division)

Page 63: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Operator Precedence

● Java's mathematical operators have the following precedence:

() (highest)

* /

+ - (lowest)

● Operators of equal precedence are evaluated left-to-right.

Page 64: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Red Squigglies

● In Alice, you programmed with drag-and-drop commands.

● In Java, you have to type everything in.● If you mistype something, you'll see it

underlined in red squigglies in Eclipse and your program won't run.

● This is called a syntax error. It's Java's way of saying “I don't understand what you want me to do.”

Page 65: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Red Squigglies

● Here are some common causes of red squigglies:● Forgetting a semicolon. For now, every line in your

program, except comments, should end in a semicolon. If you forget it, you'll get a red squiggly.

● Misspelling something. Java is case-sensitive, so int and Int are different things. If you capitalize something wrong or misspell it, you'll get red squigglies.

● At the back of the room we have set up the Wall of Bugs. If you get a red squiggly in your program, once you've fixed it, head back to that wall and write down what the problem was. If you get stuck, take a look at what people have written down – there might be some great things there!

Page 66: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import the project Java 2 into Eclipse.● Your Task: Suppose that you own a grocery store

that sells apples. Your job is to write a program that prompts the user for the number of pounds of apples a customer bought and the price of apples, then prints the amount that the customer owes.

● Finished early? Try the basketball scoring problem. If you finished that early and are up for a challenge, try the Pythagorean Theorem problem!

Page 67: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Fun with Division

Page 68: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Rounding Down

● In Java, dividing two ints will divide and then round down.

● For example, this will print 3:

int value = 7 / 2;

println("The value is " + value);

● This might be a bit weird, but there's a good reason for it.

Page 69: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sharing Cookies

Page 70: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

She got morethan me!

Page 71: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 72: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cookies for everyone!

Page 73: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The Mod Operator

● The special operator % (called the modulus operator or mod operator) computes the remainder of one value divided by another.

● a % b is pronouned “a mod b.”

● For example:● 15 % 3 = 0● 14 % 8 = 6● 21 % 2 = 1● 14 % 17 = 14

Page 74: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

So… how do we take an average?

Page 75: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Dividing Doubles

● In Java, dividing two ints will divide and then round down.

● Dividing two doubles will do the division correctly.

● If either operand is a double, the division will be done correctly.

● For example, to compute the average of two ints n1 and n2, you could write

double average = (n1 + n2) / 2.0;

Page 76: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn

● Import the Java 3 project into Eclipse.● Your Task: Write a program that

determines the average speed you're traveling at when you take a trip somewhere.

● Finished early? Try the triangle area problem. If you finish that and are up for a challenge, try the change-making problem!

Page 77: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Programming with Graphics

Page 78: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Working with Graphics

● We will manipulate graphics on-screen by creating graphics objects and manipulating their properties.

● To create a graphics object, we need to● declare a variable to hold that object, and● actually create the object using the new

keyword.

● For example:

GLabel label = new GLabel("Hi!");

Page 79: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sending Messages

● You can manipulate graphics objects by calling methods on those objects.

● To call a method on an object, use the syntax

object.method(parameters)

● For example:

label.setFont("Comic Sans-32");

label.setColor(Color.ORANGE);

Page 80: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sending Messages

● You can manipulate graphics objects by calling methods on those objects.

● To call a method on an object, use the syntax

object.method(parameters)

● For example:

label.setFont("Comic Sans-32");

label.setColor(Color.ORANGE);

Page 81: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sending Messages

● You can manipulate graphics objects by calling methods on those objects.

● To call a method on an object, use the syntax

object.method(parameters)

● For example:

label.setFont("Comic Sans-32");

label.setColor(Color.ORANGE);

Who?What?

What specifically?

Page 82: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Graphics Coordinates

HelloProgram

hello, world

+x

+y

Graphic courtesy of Eric Roberts

● Graphics objects are positioned by specifying an x and y coordinate.

● x increases left-to-right, y increases top-to-bottom.● x and y should be specified in pixels.● For a GLabel, the x and y coordinates give the start of the

baseline where the text is drawn.

Page 83: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Graphics Coordinates

HelloProgram

hello, world

+x

+y

Graphic courtesy of Eric Roberts

● Graphics objects are positioned by specifying an x and y coordinate.

● x increases left-to-right, y increases top-to-bottom.● x and y should be specified in pixels.● For a GLabel, the x and y coordinates give the start of the

baseline where the text is drawn.

(0, 0)

Page 84: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Graphics Coordinates

HelloProgram

hello, world

(100, 75)

+x

+y

Graphic courtesy of Eric Roberts

● Graphics objects are positioned by specifying an x and y coordinate.

● x increases left-to-right, y increases top-to-bottom.● x and y should be specified in pixels.● For a GLabel, the x and y coordinates give the start of the

baseline where the text is drawn.

(0, 0)

Page 85: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Drawing Geometrical Objects

Graphic courtesy of Eric Roberts

Page 86: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Graphics Program

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

Graphic courtesy of Eric Roberts

+x

+y

(x, y)

(x + width, y + height)

Page 87: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

Graphic courtesy of Eric Roberts

Graphics Program

+x

+y

(x, y)

(x + width, y + height)

Page 88: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

new GLine( x0, y0, x1, y1)Creates a line extending from (x0, y0) to (x1, y1).

Graphic courtesy of Eric Roberts

Graphics Program

+x

+y

(x0, y0)

(x1, y1)

Page 89: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.

Methods shared by the GRect and GOval classes object.setFilled( fill)

If fill is true, fills in the interior of the object; if false, shows only the outline.

object.setFillColor( color)Sets the color used to fill the interior, which can be different from the border.

new GLine( x0, y0, x1, y1)Creates a line extending from (x0, y0) to (x1, y1).

Graphic courtesy of Eric Roberts

Page 90: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The Collage Model

Page 91: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import the Java 4 project into Eclipse.

● Your Task: Draw anything you want! Try to include at least one line, at least one oval, at least one rectangle, and at least one piece of text.

Page 92: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Computing with Graphics

Page 93: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Size of the Graphics Window

Methods provided by GraphicsProgram class getWidth()

Returns the width of the graphics window.

getHeight()

Returns the height of the graphics window.

Based on slides by Eric Roberts

Like println, readInt, and readDouble, you don't need to prefix these methods with the object. notation.

Page 94: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making This Circle

Page 95: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making This Circle

Page 96: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100 pixels

Making This Circle

Page 97: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100 pixels

Making This Circle

Where is this Where is this point?

Page 98: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100 pixels

Making This Circle

Where is this Where is this point?

getWidth() pixels

Page 99: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100 pixels

Making This Circle

Where is this Where is this point?

getWidth() pixels

double x = getWidth() - 100;

Page 100: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100

pixe

ls

Making This Circle

double x = getWidth() - 100;

Page 101: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100

pixe

ls

Making This Circle

double x = getWidth() - 100;

getH

eight()

pix

els

Page 102: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100

pixe

ls

Making This Circle

double x = getWidth() - 100;double y = getHeight() - 100;

getH

eight()

pix

els

Page 103: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

100

pixe

ls

Making This Circle

double x = getWidth() - 100;double y = getHeight() - 100;

GOval circle = new GOval(x, y, 100, 100);

getH

eight()

pix

els

Page 104: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making This Circle

double x = getWidth() - 100;double y = getHeight() - 100;

GOval circle = new GOval(x, y, 100, 100);

Page 105: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making This Circle

double x = getWidth() - 100;double y = getHeight() - 100;

GOval circle = new GOval(x, y, 100, 100);circle.setFilled(true);circle.setColor(Color.BLUE);

Page 106: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making This Circle

double x = getWidth() - 100;double y = getHeight() - 100;

GOval circle = new GOval(x, y, 100, 100);circle.setFilled(true);circle.setColor(Color.BLUE);add(circle);

Page 107: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Exploring the Coordinate Space

Page 108: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import the Java 5 project into Eclipse.

● Your Task: Write a program that puts filled, black circles of radius 100 pixels in all four corners of the window.

● Finish early? Try the diamond and Belgian flag problems!

Graphics Program

Page 109: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

Page 110: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

Page 111: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

Page 112: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

Page 113: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

Page 114: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

50 50

Page 115: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Illusory Contours

50 getWidth() - 100 50

Page 116: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Magic Numbers

● A magic number is a number written in a piece of code whose meaning cannot easily be deduced from context.

double weight = 9.8 * (mass – 14.3);

● Magic numbers make code harder to readand harder to change.

Page 117: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Constants

● A constant is a name for a value that never changes.

● Syntax (defined outside of any method):

private static final type name = value;

● By convention, constants are named in UPPER_CASE_WITH_UNDERSCORES to differentiate them from variables.

● Constants can significantly improve code readability. They also improve code maintainability.

Page 118: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Centering an Object

Graphics Program

getWidth();

W

getWidth() / 2.0;

W / 2.0

double x = (getWidth() / 2.0) – (W / 2.0);

- or -

double x = (getWidth() - W) / 2.0;

Page 119: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

More Coordinate Space Exploration!

Page 120: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import the Java 6 project into Eclipse.

● Your Task: Write a program that draws the Flag of Paris centered in the screen.

● Each bar should be 100 pixels across and 200 pixels tall, so the total flag is of size 200 × 200.

● Finished early? Try the Snowman or No Dice problems!

Page 121: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Structures

Page 122: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Structures

● In our second day of Alice, we saw three control structures:● Counted Loops● While Loops● If Statements

● These exist in Java as well!● Let's see what they look like.

Page 123: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Structures

iffor

while

Page 124: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Structures

iffor

while

Page 125: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

if statements

if (condition) {… statements to run if condition holds …} else {… statements to run if condition doesn't hold …}

Page 126: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Based on slides by Mehran Sahami

Boolean Expressions

● A boolean expression is a test for a condition (it is either true or false).

● Value comparisons:

== “equals” (note: not single =)

!= “not equals”

> “greater than”

< “less than”

>= “greater than or equal to”

<= “less than or equal to”

Page 127: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Or else

if (condition) {… statements to run if condition holds …} else {… statements to run if condition doesn't hold …}

Page 128: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Note on Semicolons

● Notice that there isn't a semicolon after the parentheses in if statements.

● You'll get weird red squigglies if you accidentally put semicolons there.

if (condition) {… statements to run if condition holds …} else {… statements to run if condition doesn't hold …}

Page 129: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Logical Operators

● We can combine conditions with logical operators.● Logical NOT: !p

if (!isWeekday()) {

relaxAndUnwind();

}

● Logical AND: p && q if (youreHappy() && youKnowIt()) {

clapYourHands();

}

● Logical OR: p || q (inclusive OR)

if (hasPuppy() || hasKitty()) {

beHappy();

}

Page 130: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cascading if

Based on slides by Mehran Sahami

if (score >= 90) { println(" AWWWW YEAHHHHH ");} else if (score >= 80) { println(" <(^_^)> ");} else if (score >= 70) { println(" : - | ");} else if (score >= 60) { println(" ಠ_ಠ ");} else {

println(" ( ° °╯ □ )╯︵ ┻━┻ ");}

Page 131: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cascading if

Based on slides by Mehran Sahami

if (score >= 90) { println(" AWWWW YEAHHHHH ");} else if (score >= 80) { println(" <(^_^)> ");} else if (score >= 70) { println(" : - | ");} else if (score >= 60) { println(" ಠ_ಠ ");} else {

println(" ( ° °╯ □ )╯︵ ┻━┻ ");}

Page 132: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cascading if

Based on slides by Mehran Sahami

if (score >= 90) { println(" AWWWW YEAHHHHH ");} else if (score >= 80) { println(" <(^_^)> ");} else if (score >= 70) { println(" : - | ");} else if (score >= 60) { println(" ಠ_ಠ ");} else {

println(" ( ° °╯ □ )╯︵ ┻━┻ ");}

Page 133: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cascading if

Based on slides by Mehran Sahami

if (score >= 90) { println(" AWWWW YEAHHHHH ");} else if (score >= 80) { println(" <(^_^)> ");} else if (score >= 70) { println(" : - | ");} else if (score >= 60) { println(" ಠ_ಠ ");} else {

println(" ( ° °╯ □ )╯︵ ┻━┻ ");}

Page 134: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Cascading if

Based on slides by Mehran Sahami

if (score >= 90) { println(" AWWWW YEAHHHHH ");} else if (score >= 80) { println(" <(^_^)> ");} else if (score >= 70) { println(" : - | ");} else if (score >= 60) { println(" ಠ_ಠ ");} else {

println(" ( ° °╯ □ )╯︵ ┻━┻ ");}

Page 135: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 7 into Eclipse.● Your Task: Astronomers searching for planets

orbiting other stars like it when those planets' temperatures fall into the “habitable zone,” which we'll treat as between 32°F and 120°F.

● Write a program that prompts the user for the temperature of a planet and tells the user whether it's too hot, too cold, or in the habitable zone.

● Finished early? Try the Know Your Rights problem, and if you finish that, then try the Time Conversion problem!

Page 136: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Program Tracing

Page 137: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 1import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); }

if (temp > MAX_TEMPERATURE) { println("Too hot!"); }

if (temp >= MIN_TEMPERATURE && temp <= MAX_TEMPERATURE) { println("Just right!"); } }}

Page 138: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 2import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); }

if (temp >= MIN_TEMPERATURE && temp <= MAX_TEMPERATURE) { println("Just right!"); }

if (temp > MAX_TEMPERATURE) { println("Too hot!"); } }}

Page 139: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 3import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); }

if (temp >= MIN_TEMPERATURE || temp <= MAX_TEMPERATURE) { println("Just right!"); }

if (temp > MAX_TEMPERATURE) { println("Too hot!"); } }}

Page 140: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 4import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); } else if (temp > MAX_TEMPERATURE) { println("Too hot!"); } else if (temp >= MIN_TEMPERATURE && temp <= MAX_TEMPERATURE) { println("Just right!"); } }}

Page 141: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 5import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); } else if (temp > MAX_TEMPERATURE) { println("Too hot!"); } else { println("Just right!"); } }}

Page 142: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 6import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp > MAX_TEMPERATURE) { println("Too hot!"); } else if (temp < MIN_TEMPERATURE) { println("Too cold!"); } else { println("Just right!"); } }}

Page 143: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 7import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp < MIN_TEMPERATURE) { println("Too cold!"); }

if (temp > MAX_TEMPERATURE) { println("Too hot!"); } else { println("Just right!"); } }}

Page 144: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

// Program 8import acm.program.*;

public class HabitableZone extends ConsoleProgram { private static final double MIN_TEMPERATURE = 32.0; private static final double MAX_TEMPERATURE = 120.0;

public void run() { double temp = readDouble("How hot? "); if (temp > MAX_TEMPERATURE) { println("Too hot!"); } else { println("Just right!"); }

if (temp < MIN_TEMPERATURE) { println("Too cold!"); } }}

Page 145: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Statements

iffor

while

Page 146: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Statements

iffor

while

Page 147: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

For Loops

● In Java, the for loop is used to repeat a statement a certain number of times.

● Similar to the loop construct from Alice, though is a bit more powerful.

● Today, we'll see some quick examples using for loops. When we come back next time, we'll explore how they work in more depth.

Page 148: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The Syntax

● To repeat a set of commands N times, use the following code:

for (int i = 0; i < N; i++) {

// … statements to execute …

}

● We'll talk about how exactly this works later on. For now, let's focus on what we can do with it!

Page 149: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Some Song Lyrics

● Let's write a program to print these lyrics:

Baby,

Baby,

Baby,

Ohhh

- Justin Bieber

Page 150: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Counter

● Inside a for loop, the variable i keeps track of the index of the current loop, starting at 0.● First time through the loop: i = 0● Second time through the loop: i = 1● Third time through the loop: i = 2

● Let's see an example of this.

Page 151: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Counter

● The different control structures in Java can be combined together to produce results more complex than any individual piece.

● For example, we can combine for loops and if statements together.

Page 152: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Counter

● Suppose we want to print out the first fifteen multiples of 50 (0, 50, 100, …).

● We can accomplish this using a for loop.

for (int i = 0; i < 15; i++) {

println(i * 50);

}

● Do you see why?

Page 153: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Counter

● Suppose we want to draw a row of boxes, like these:

● Suppose each box is 50 pixels wide and 50

pixels tall.● Look where their corners are... seem familiar?

Page 154: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Counter

● Suppose we want to draw a row of boxes, like these:

● Suppose each box is 50 pixels wide and 50

pixels tall.● Look where their corners are... seem familiar?

(0, 0) (50, 0) (100, 0) (150, 0) (200, 0) (250, 0)

Page 155: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Download Java 8.

● Your Task: Draw a caterpillar! Start by drawing the body, which should consist of a bunch of circles. (Use a for loop!)

● Then, make it pretty however you'd like!

● Finished early, or want to try something else? Check out the Fizz Bazz Buzz problem. If you finish that and are up for a challenge, try the Seeing Stars program!

Page 156: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Double for Loops

Page 157: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Double for Loops

● Just as you can put if statements inside of for loops, you can put for loops inside of for loops!

● Syntax:

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

// … statements to execute …

}

}

● This will run through all possible combinations of i and j where i is less than M and j is less than N.

Page 158: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 159: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

GRect box = new GRect(0, 0, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box);

Page 160: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

GRect box = new GRect(0, 0, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box);

Page 161: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int j = 0; j < 5; j++) { double x = j * BOX_SIZE;

GRect box = new GRect(x, 0, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box); }

Page 162: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int j = 0; j < 5; j++) { double x = j * BOX_SIZE;

GRect box = new GRect(x, 0, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box); }

Page 163: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { double x = j * BOX_SIZE; double y = i * BOX_SIZE;

GRect box = new GRect(x, y, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box); }}

Page 164: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { double x = j * BOX_SIZE; double y = i * BOX_SIZE;

GRect box = new GRect(x, y, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box); }}

Page 165: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { double x = j * BOX_SIZE; double y = i * BOX_SIZE;

GRect box = new GRect(x, y, BOX_SIZE, BOX_SIZE); box.setFilled(true); box.setFillColor(Color.BLUE); add(box); }}

Page 166: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 9.

● Your Task: Draw this optical illusion!

● Each box is 50 × 50 with 10 additional pixels of horizontal and vertical spacing between each box.

● Think about the caterpillar – how might you adjust the spacing between these boxes?

● Finished early? Try the checkerboard or yarn pattern problems!

Page 167: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variables, Revisited

Page 168: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The Story So Far

● We've now covered graphics programming, if statements, for loops, and the basics of variables.

● Before we can move on any further, we need to explore variables in more depth.

● This part might seem a bit counterintuitive, but it will unlock many doors.

Page 169: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Assignment Statements

● A variable consists of● a name (what is it called?),● a type (what sort of value does it hold?), and● a value.

● A variable's name and type can never change.

● However, it is possible to change the value stored in a variable.

Page 170: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Assignment Statements

● A statement of the form

variable = newValue;

changes variable so that it now stores newValue instead of its old value.

● This statement is called an assignment statement.

Page 171: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 3; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

3137179

Page 172: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

3137179

Page 173: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

3137179

4favoriteNumber

Page 174: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

3137179

4favoriteNumber

Page 175: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

4favoriteNumber

Page 176: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

4favoriteNumber

Page 177: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

137favoriteNumber

Page 178: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

137favoriteNumber

Page 179: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

137favoriteNumber

Page 180: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

137favoriteNumber

Page 181: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

179favoriteNumber

Page 182: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

179favoriteNumber

Page 183: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

An Examplepublic void run() { int favoriteNumber = 4; println(favoriteNumber);

favoriteNumber = 137; println(favoriteNumber);

favoriteNumber = 137 + 42; println(favoriteNumber);}

Changing Variables

4137179

179favoriteNumber

Page 184: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

1010

Page 185: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

Page 186: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

Page 187: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

5a

7b

Page 188: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

5a

7b

Page 189: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

7b

Page 190: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

7b

Page 191: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

7b

Page 192: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

7b

Page 193: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

5b

Page 194: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

5b

Page 195: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

5b

Page 196: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

5b

Page 197: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; int b = 7;

a = b + 3; println(a);

b = 5; println(a);}

Changing Variables

1010

10a

5b

Page 198: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Page 199: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

Page 200: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

Page 201: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

5a

Page 202: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

5a

Page 203: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Guh, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

5a

Page 204: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

5a

Page 205: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

6a

Page 206: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

6a

Page 207: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; println(a);}

Changing Variables

5612

6a

Page 208: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; // <--- Seriously? println(a);}

Changing Variables

5612

6a

Page 209: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; // <--- Seriously? println(a);}

Changing Variables

5612

12a

Page 210: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; // <--- Seriously? println(a);}

Changing Variables

5612

12a

Page 211: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Examplepublic void run() { int a = 5; println(a);

a = a + 1; // <--- Um, what? println(a);

a = a * 2; // <--- Seriously? println(a);}

Changing Variables

5612

12a

Page 212: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Nudging Values

● In Java (and many other languages), it's normal to see statements like these:

x = x + 1;

y = y / 137;

● Don't read these as mathematical statements – you'll just get confused.

● Two intuitions:● Read these statements as “add one to x” or “divide y

by 137.”● Read these statements as commands – we are

ordering x and y to update the values they are storing.

Page 213: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

Page 214: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Why would you do this?

Page 215: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Write a program that reads in a list of five values, then outputs their sum.

Page 216: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

Page 217: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

Page 218: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

Page 219: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Page 220: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Page 221: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Page 222: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 223: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 224: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

27nextValue

Page 225: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

0total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

27nextValue

Page 226: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

27nextValue

Page 227: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 228: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 229: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 230: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 231: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 232: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

18nextValue

Page 233: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

27total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

18nextValue

Page 234: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

18nextValue

Page 235: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 236: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 237: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 238: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 239: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 240: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

28nextValue

Page 241: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

45total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

28nextValue

Page 242: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

73total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

28nextValue

Page 243: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { int total = 0; for (int i = 0; i < 5; i++) { int nextValue = readInt("Enter next number: ");

/* Add the next value to the total. */ total = total + nextValue; } println("The total is " + total);}

SumFiveValues

73total

Enter next number: 27Enter next number: 18Enter next number: 28Enter next number: 18Enter next number: 28The total is 119

Page 244: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Aggregating Information

● It's extremely common to aggregate information across multiple iterations of a loop.

● General pattern:● If information needs to persist across loop

iterations, store it in a variable defined outside the loop.

● If information only needs to survive for a single iteration of the loop, define it inside the loop.

Page 245: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 10.

● Your Task: The American Heart Association recommends that everyone get 150 minutes of aerobic exercise every week.

● Write a program that prompts the user to enter the amount of aerobic exercise they did on each of the seven days of the week, then reports their total aerobic exercise.

● If the user hasn't gotten 150 minutes of aerobic exercise, tell them how many more minutes of exercise they need.

● Finished early? There's some instructions in the source file about some other things to check for. Update your program to also track that other information. If you finish that early, try the medicine half-lives program!

Page 246: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Useful Shorthand

● Commonly, programs contain code like this:

x = x + 1; y = y * 137;

z = z / 14; w = w – 3;

Page 247: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Useful Shorthand

● Commonly, programs contain code like this:

x = x + 1; y = y * 137;

z = z / 14; w = w – 3;

● The statement

variable = variable op value;

can be rewritten as

variable op= value;

Page 248: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Useful Shorthand

● Commonly, programs contain code like this:

x += 1; y *= 137;

z /= 14; w -= 3;

● The statement

variable = variable op value;

can be rewritten as

variable op= value;

Page 249: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Another Useful Shorthand

● In the special case of writing

variable = variable + 1;

we can instead write

variable++;● In the special case of writing

variable = variable - 1;

we can instead write

variable--;

Page 250: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

++: Seem Familiar?

● Hmmm... haven't we seen this ++ thing somewhere before?

● How about in

for (int i = 0; i < N; i++) {

}

● What does this mean?

Page 251: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < N; i++) {…

}

This is called the initialization statement and is performed before

This is called the initialization statement and is performed before

the loop starts.

This is called the loop condition or termination condition. The loop will check whether this

statement is true before each

This is called the loop condition or termination condition. The loop will check whether this

statement is true before each iteration of the loop.

This is called the step or increment and is performed at the end

This is called the step or increment and is performed at the end of each loop iteration.

Page 252: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Page 253: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

Page 254: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

Page 255: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

0int i

Page 256: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

0int i

Page 257: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

0int i

Page 258: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

0int i

Baby

Page 259: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

0int i

Baby

Page 260: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

1int i

Baby

Page 261: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

1int i

Baby

Page 262: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

1int i

Baby

Page 263: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

1int i

Baby

Baby

Page 264: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

1int i

Baby

Baby

Page 265: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

2int i

Baby

Baby

Page 266: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

2int i

Baby

Baby

Page 267: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

2int i

Baby

Baby

Page 268: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

2int i

Baby

Baby

Baby

Page 269: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

2int i

Baby

Baby

Baby

Page 270: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

3int i

Baby

Baby

Baby

Page 271: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

3int i

Baby

Baby

Baby

Page 272: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

3int i

Baby

Baby

Baby

Page 273: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

Baby

Baby

Baby

Page 274: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Console Program

Baby

Baby

Baby

Ohhh

Page 275: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Console Program

Baby

Baby

Baby

Ohhh

for (int i = 0; i < 3; i++) { println("Baby"); } println("Ohhh");

Page 276: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Console Program

for (int i = 5; i > 0; i--) { println(i + "..."); } println("Lift-off!");

5...

4...

3...

2...

1...

Lift-off!

Page 277: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Statements

iffor

while

Page 278: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Control Statements

iffor

while

Page 279: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

while (condition) {

… statements …

}

● This loop works as follows:● Check whether condition is true.● If so, execute statements in their entirety,

then repeat this process.● If not, move on to whatever comes after the

loop.

Page 280: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Based on slides by Mehran Sahami

Console Program

Page 281: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

Based on slides by Mehran Sahami

Page 282: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

15 int x

Based on slides by Mehran Sahami

Page 283: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

15 int x

Based on slides by Mehran Sahami

Page 284: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

15 int x

Based on slides by Mehran Sahami

Page 285: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

7 int x

Based on slides by Mehran Sahami

Page 286: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

7 int x

Based on slides by Mehran Sahami

Page 287: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

7 int x

7

Based on slides by Mehran Sahami

Page 288: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

7 int x

7

Based on slides by Mehran Sahami

Page 289: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

7 int x

7

Based on slides by Mehran Sahami

Page 290: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

3 int x

7

Based on slides by Mehran Sahami

Page 291: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

3 int x

7

Based on slides by Mehran Sahami

Page 292: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

3 int x

73

Based on slides by Mehran Sahami

Page 293: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

3 int x

73

Based on slides by Mehran Sahami

Page 294: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

3 int x

73

Based on slides by Mehran Sahami

Page 295: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

1 int x

73

Based on slides by Mehran Sahami

Page 296: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

1 int x

73

Based on slides by Mehran Sahami

Page 297: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

1 int x

731

Based on slides by Mehran Sahami

Page 298: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

1 int x

731

Based on slides by Mehran Sahami

Page 299: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The while Loop

int x = 15; while (x > 1) { x /= 2; println(x); }

Console Program

1 int x

731

Based on slides by Mehran Sahami

Page 300: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Infinite Loops

Page 301: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 11.

● Your Task: Start with any positive whole number. If that number is even, divide it by two. If it's odd, multiply it by three and add one. You then repeat this process until the number becomes one. Write a program that prompts the user for a number, then prints out this sequence beginning at that number. For example, starting at 13, the sequence is

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1● Finished early? Try the powers of two problem. If

you finish early, try the Fibonacci numbers problem!

Page 302: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Randomness and Computing

Page 303: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Random Number Generators

Page 304: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

RandomGenerator

● The class RandomGenerator acts as a random number generator. To use it, you'll need to import acm.util.*;

● To generate random numbers, start by getting a random generator:

RandomGenerator rgen = RandomGenerator.getInstance();

● Then, use the nextX functions to get the random values you want:● rgen.nextInt(low, high)● rgen.nextDouble(low, high)● rgen.nextBoolean(probability)● rgen.nextColor() // Ooh, shiny!

Page 305: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 12.

● Your Task: Write a program that flips coins until it flips three heads and three tails, then reports the number of necessary flips.

● Finished early? Try the consecutive heads problem or the coin flipping game problem!

Page 306: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Writing Your Own Methods

Page 307: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

● To define your own custom method, you can usethis syntax:

private void methodName() {

/* … Method body … */

}

● This code needs to go outside of the run method; Java will get sad otherwise.

● You can call your method by writing

methodName();

Defining Your Own Methods

Page 308: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Lord of the Rings

private void methodName() {

/* … Method body … */

}

methodName();

Page 309: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Methods With Parameters

● To define a method that takes some number of parameters, you can write

private void methodName(parameters) {

/* … method body … */

}

● These parameters behave like Alice parameters – you specify them when you call the method.

Page 310: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Methods and Parameters

● A while back, we said that every variable has three pieces of information associated with it. What are they?● Name● Type● Value

● When writing a method that takes parameters:● The method specifies the name and type of each

parameter.● The caller specifies the value.

Page 311: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Scope of Method Calls

● A variable declared inside a method is called a local variable.

● Local variables can only be accessed inside of the method that declares them.

public void run() {

int x = 5;

someOtherMethod();

}

private void someOtherMethod() {

x = 4; // Error!

}

Page 312: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

More Method Examples

Page 313: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Drawing a Stoplight

Page 314: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Methods and Parameters

● When you are implementing a method, trust that the parameters provided are correct, then use them in the method to provide the desired behavior.

● When you are calling a method, trust that the method works correctly, then provide the necessary information as arguments.

● The implementer trusts the parameters and needs to ensure the method works right.

● The caller trusts the method and needs to ensure that the parameters are right.

Page 315: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 13.● Your Task: Write a method

that prints out a row of stars. Our provided starter code will then use it to print a pyramid of stars!

● Once you're done, switch to the Pawprints program and write a method that draws a pawprint at an (x, y) coordinate of your choice.

● Then, draw some animal tracks with your method!

● Finished early? Try the soccer field problem!

(x, y)

Page 316: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Review: Methods with Parameters

Page 317: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

···*·····***···*****·*******·*****···***·····*···

Page 318: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

···*·····***···*****·*******·*****···***·····*···

Page 319: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

····*···· ···***··· ··*****·· ·*******· ********* ·*******· ··*****·· ···***··· ····*···

Page 320: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

···*·····***···*****·*******·*****···***·····*···

Page 321: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

···*·····***···*****·*******·*****···***·····*···

Page 322: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

···*·····***···*****·*******·*****···***·····*···

number-of-stars + number-of-spaces = width

number-of-stars + 2 · spaces-to-draw = width

2 · spaces-to-draw = width – number-of-stars

spaces-to-draw = (width – number-of-stars) / 2

Page 323: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Methods that Return Values

Page 324: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 325: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

import acm.program.*;

public class AddTwoIntegers extends ConsoleProgram {public void run() {

println("This program adds two integers.");

// Read two values from the user.int n1 = readInt("Enter first integer: ");int n2 = readInt("Enter second integer: ");

// Compute their sum.int sum = n1 + n2;

// Print out the sum.println("The sum of those numbers is " + sum);

}}

Page 326: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Return Values

● Methods can return values that can be used elsewhere in the program.

● Examples:● The getWidth and getHeight methods return the

width and height of the window.● The readInt and readDouble methods return

values entered by the user.

● You can write your own methods that return values!

Page 327: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Return Syntax

● To make a method that communicates a value to the outside world, you need to do two things.

● First, say what kind of value you want to communicate back by specifying a return type. Declare your method as

private returnType methodName(parameters)

● Then, include a return statement in your method saying what value to hand back.

return value;

Page 328: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Factorials!

● The number n factorial, denoted n!, is

1 × 2 × 3 × … × (n – 1) × n ● For example:

● 3! = 1 × 2 × 3 = 6.● 5! = 1 × 2 × 3 × 4 × 5 = 120● 0! = 1 (by definition)

● Factorials arise surprisingly frequently in computer science:● Determining how quickly computers can sort a list of

values.● Analyzing the efficiency of various algorithms.

Page 329: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Many Happy returns

● A method may have multiple return statements. The method ends as soon as return is executed.

private int thisIsLegal(int x) {

if (x == 5) {

return 0;

} else {

return 1;

}

}

Page 330: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Many Happy returns

● A method may have multiple return statements. The method ends as soon as return is executed.

private int thisIsLegal(int x) {

if (x == 5) {

return 0;

}

return 1;

}

The only way we can get here is if x is not The only way we can

get here is if x is not equal to 5.

Page 331: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 14.● Your Task: First, write a method called max

that takes in two ints and returns the larger of the two. Use our provided code in run to test it out.

● Next, write a raiseToPower method that takes in two integers a and b and computes the value ab. Use our provided code in run to test it out.

Page 332: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Variable Scoping

Page 333: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Scope

● Each variable has a scope where it can be accessed and how long it lives.

for (int i = 0; i < 5; i++) {

int y = i * 4;

println(y);

}

println(i); // Error!

println(y); // Error!

Page 334: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Scope

● Each variable has a scope where it can be accessed and how long it lives.

● Variables declared outside a loop persist across all loop iterations.

● Variables declared inside a loop persist only for a single iteration.

● The loop counter in a for loop persists as long as the loop runs, then disappears.

● General rule: A variable's scope is determined by the curly braces it lives in.

Page 335: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Scope of Method Calls

● A variable declared inside a method is called a local variable.

● Local variables can only be accessed inside of the method that declares them.

public void run() {

int x = 5;

someOtherMethod();

}

private void someOtherMethod() {

x = 4; // Error!

}

Page 336: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Scoping Revisited

● Variables defined in one method aren't visible inside of other methods.

● You can send values into a method by using parameters.

● You can receive values from a method by using return values.

● This can lead to some slightly unusual results.

Page 337: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

i

Slides by Mehran Sahami

Console Program

Page 338: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

Slides by Mehran Sahami

Console Program

Page 339: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

Slides by Mehran Sahami

Console Program

Page 340: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

Slides by Mehran Sahami

Console Program

Page 341: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

Slides by Mehran Sahami

Console Program

Page 342: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

result0n i

Slides by Mehran Sahami

Console Program

Page 343: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result0n i

Slides by Mehran Sahami

Console Program

Page 344: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result0n 1i

Slides by Mehran Sahami

Console Program

Page 345: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result0n 1i

Slides by Mehran Sahami

Console Program

Page 346: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result0n 1i

Slides by Mehran Sahami

Console Program

Page 347: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

1

Slides by Mehran Sahami

Console Program

Page 348: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0i

1

Slides by Mehran Sahami

Console Program

0! = 1

Page 349: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 350: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 351: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 352: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 353: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

result1n i

Slides by Mehran Sahami

Console Program

0! = 1

Page 354: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n i

Slides by Mehran Sahami

Console Program

0! = 1

Page 355: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 356: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 357: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 1i

Slides by Mehran Sahami

Console Program

0! = 1

Page 358: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 2i

Slides by Mehran Sahami

Console Program

0! = 1

Page 359: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 2i

Slides by Mehran Sahami

Console Program

0! = 1

Page 360: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result1n 2i

Slides by Mehran Sahami

Console Program

0! = 1

Page 361: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

1

Slides by Mehran Sahami

Console Program

0! = 1

Page 362: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

1i

1

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 363: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 364: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 365: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 366: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 367: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

result2n i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 368: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 369: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n 1i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 370: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n 1i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 371: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n 1i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 372: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n 2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 373: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

1result2n 2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 374: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

2result2n 2i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 375: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

2result2n 3i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 376: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

2result2n 3i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 377: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

0

i

private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

2result2n 3i

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 378: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

2

Slides by Mehran Sahami

Console Program

0! = 11! = 1

Page 379: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

2i

2

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 380: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 381: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 382: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 383: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 384: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

6

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 2

Page 385: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

3i

6

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 23! = 6

Page 386: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

4i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 23! = 6

Page 387: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public void run() { for(int i = 0; i < MAX_NUM; i++) { println(i + "! = " + factorial(i)); } }

4i

Slides by Mehran Sahami

Console Program

0! = 11! = 12! = 23! = 6

Page 388: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Retiring Young

Page 389: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Pass-by-Value

● Method parameters act as their own variables. They are independent of any similarly-named variable in the calling method.

● This is called pass-by-value.

private void retireYoung(int myMoney) {

myMoney = 1000000000;

}

public void run() {

int myMoney = 42;

retireYoung(myMoney);

println(myMoney);

}

Page 390: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Pass-by-Value

● Method parameters act as their own variables. They are independent of any similarly-named variable in the calling method.

● This is called pass-by-value.

private void retireYoung(int myMoney) {

myMoney = 1000000000;

}

public void run() {

int myMoney = 42;

retireYoung(myMoney);

println(myMoney);

}

public void run() { int myMoney = 42; retireYoung(myMoney); println(myMoney); }

42myMoney

Page 391: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Pass-by-Value

● Method parameters act as their own variables. They are independent of any similarly-named variable in the calling method.

● This is called pass-by-value.

private void retireYoung(int myMoney) {

myMoney = 1000000000;

}

public void run() {

int myMoney = 42;

retireYoung(myMoney);

println(myMoney);

}

public void run() { int myMoney = 42; retireYoung(myMoney); println(myMoney); }

42

private void retireYoung(int myMoney) { myMoney = 1000000000; }

42myMoney

Page 392: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Pass-by-Value

● Method parameters act as their own variables. They are independent of any similarly-named variable in the calling method.

● This is called pass-by-value.

private void retireYoung(int myMoney) {

myMoney = 1000000000;

}

public void run() {

int myMoney = 42;

retireYoung(myMoney);

println(myMoney);

}

public void run() { int myMoney = 42; retireYoung(myMoney); println(myMoney); }

42

private void retireYoung(int myMoney) { myMoney = 1000000000; }

kaching!myMoney

Page 393: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Pass-by-Value

● Method parameters act as their own variables. They are independent of any similarly-named variable in the calling method.

● This is called pass-by-value.

private void retireYoung(int myMoney) {

myMoney = 1000000000;

}

public void run() {

int myMoney = 42;

retireYoung(myMoney);

println(myMoney);

}

public void run() { int myMoney = 42; retireYoung(myMoney); println(myMoney); }

42myMoney

Page 394: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn: Scoping Practice

Page 395: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

Page 396: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

Page 397: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

Page 398: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

Page 399: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory

Page 400: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory

Page 401: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

Page 402: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

Page 403: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

jenny charlie

Page 404: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

1jenny charlie 2

Page 405: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

1jenny charlie 2

Page 406: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

1jenny charlie 2

Page 407: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

1jenny charlie 2

Page 408: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

Page 409: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

Page 410: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

FindingJava

Page 411: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

FindingJava

jenny = 3

Page 412: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

FindingJava

jenny = 3

Page 413: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

FindingJava

jenny = 3charlie = 2

Page 414: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

nemo()

3jenny charlie 2

FindingJava

jenny = 3charlie = 2

Page 415: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2

Page 416: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2

Page 417: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2

Page 418: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2dory = 1

Page 419: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2dory = 1

Page 420: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2dory = 1marlin = 2

Page 421: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); }

public class FindingJava extends ConsoleProgram { public void run() { int dory = 1; int marlin = 2; nemo(dory, marlin); println("dory = " + dory); println("marlin = " + marlin); } private void nemo(int jenny, int charlie) { jenny += charlie; println("jenny = " + jenny); println("charlie = " + charlie); } }

run()

1dory marlin 2

FindingJava

jenny = 3charlie = 2dory = 1marlin = 2

Page 422: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

What to Do

● Trace through the programs one line at a time. Don't skip steps! You should read one line at a time, decide what it does, then make changes.

● Keep track of which method you're in. When creating a new variable, add a new box to the current method. When calling a method, make a box for that whole method. Remember that there can be different variables with the same name in different scopes!

● Keep track of the output produced by the program in a separate place. We recommend drawing and labeling one box as the console.

Page 423: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Animation

Page 424: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Moving Objects

● You can move or change graphics objects after you've added them. If you do, the changes will show up on the screen.

● If you have a graphics object obj, you can call obj.move(dx, dy) to shift the object over dx pixels to the left and dy pixels down.● dx and dy can be negative to move the object

right or up.

Page 425: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Animation

● By repositioning objects after they have been added to the canvas, we can create animations.

● General pattern for animation:

while (animation-not-finished) {

update graphics;

pause(pause-time);

}

Page 426: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Simple Animation

● We're going to animate the sun moving across the sky.

● We'll use a GOval to represent the sun.● For fun, we'll make the background a

nice shade of sky blue.

Page 427: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 15.● Your Task: Create an

animation of the sun setting below the horizon!

● The sun should appear to dip below the horizon.

● The sun should stop moving when it's no longer visible.

Page 428: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 15.● Your Task: Create an

animation of the sun setting below the horizon!

● The sun should appear to dip below the horizon.

● The sun should stop moving when it's no longer visible.

Page 429: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Physics Simulation

Page 430: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Δx

Δy Δx

Δy Δx

Δy Δx

Δy

Note that Δy increases because the object is accelerating

downward.Thanks, gravity!

Note that Δy increases because the object is accelerating

downward.Thanks, gravity!

A Falling Object

Page 431: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Bouncing Ball

ball.getY()

ball.getHeight()

getHeight()

Page 432: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Sticky Situation

The ball is below the ground, so we reverse its yΔ

The ball is below the ground, so we reverse its yΔ

It's still below the ground, so we

reverse its y again.Δ

It's still below the ground, so we

reverse its y again.Δ

Page 433: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 16.● Your Task: Make

any animation that you'd like!

● Be creative! What can you come up with?

Page 434: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Events

Page 435: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Events

● An event is some external stimulus that your program can respond to.

● Common events include:● Mouse motion / clicking.● Keyboard buttons pressed.● Timers expiring.● Network data available.

Page 436: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Events

An event is some external stimulus that your program can respond to.

Common events include:● Mouse motion / clicking.

Keyboard buttons pressed.

Timers expiring.

Network data available.

Page 437: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Responding to Mouse Events

● To respond to events, your program must● indicate that it wants to receive events, and● write methods to handle those events.

● Call the addMouseListeners() method to tell your program receive mouse events.● This is typically done in run.

● Write appropriate methods to process the mouse events.

Page 438: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Methods for Handling Events

● Define any or all of the following mouse event handlers to respond to the mouse:

@Override public void mouseMoved(MouseEvent e)

@Override public void mouseDragged(MouseEvent e)

@Override public void mousePressed(MouseEvent e)

@Override public void mouseReleased(MouseEvent e)

@Override public void mouseClicked(MouseEvent e)

@Override public void mouseEntered(MouseEvent e)

@Override public void mouseExited(MouseEvent e)● Notice that these are public void and not private void. This is

important! Also notice the @Override prefix.● You must also import java.awt.event.*; for the MouseEvent class.

Page 439: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Virtual Hole Puncher

Page 440: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

The Mirror Box

Page 441: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 442: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 443: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Making the Mirror Box

Page 444: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

Page 445: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

Page 446: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

Page 447: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

(x, y)

Page 448: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

(x, y)

(??, ??)

Page 449: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

(x, y)

(??, y)

Page 450: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

(x, y)

x

(??, y)

x

Page 451: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

MirrorBox

(x, y)

x

(getWidth() - x, y)

x

Page 452: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 17.● Your Task: Write a program

that places bear tracks wherever the mouse is clicked.

● We've provided a drawBearTrack method that, given an x and y coordinate, draws a bear track centered at that location.

● Finished early? Try the event visualizer problem or the color Theremin problem!

Page 453: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Boolean Values

Page 454: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Boolean Values

● A boolean value is a value that is either true or false.● In Java, you can store boolean values using the boolean

type.● Example:

int n = readInt("Enter an integer: ");

boolean isEven = (n % 2 == 0);

if (isEven) {

… }

Page 455: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Predicates

● Methods can return boolean values.● In CS, we use the fancy term predicate

to refer to a method that returns a boolean value.

● Because predicates return booleans, we can use them in if statements, for loops, and other contexts.

Page 456: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Prime Numbers

● An integer greater than 1 is called prime if its only divisors are 1 and itself.

● For example:● 5 is prime.● 17 is prime.● 15 is not prime: it's 3 × 5● 24 is not prime: it's 2 × 12, 3 × 8, and 4 × 6.

Page 457: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Note about Programming Languages

Page 458: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Java and Python

private void isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true;}

def isPrime(n): if n <= 1: return False

for i in range(2, n): if n % i == 0: return False

return True

Page 459: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Interacting with the Canvas

Page 460: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Canvas

● It is possible to determine what, if anything, is at the canvas at a particular point.

● The method

GObject getElementAt(double x, double y)

returns which object is at the given location on the canvas.

The return type is GObject, since we don't know what specific type (GRect, GOval, etc.) is really there.

If no object is present, the special value null is returned.

GObject

GRect GOval GLineGLabel

Page 461: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Accessing the Canvas

● It is possible to determine what, if anything, is at the canvas at a particular point.

● The method

GObject getElementAt(double x, double y)

returns which object is at the given location on the canvas.

● The return type is GObject, since we don't know what specific type (GRect, GOval, etc.) is really there.

● If no object is present, the special value null is returned.

Page 462: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Debris Sweeper

Page 463: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 18.

● Your Task: Write a program that draws random circles on the screen that move when they're clicked on.

● Finished early and up for a challenge? Try the circle subdivision problem!

● As a hint, remember that you can ask an object for its position and size with obj.getX(), obj.getY(), obj.getWidth(), and obj.getHeight().

Page 464: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 18.

● Your Task: Write a program that draws random circles on the screen that move when they're clicked on.

● Finished early and up for a challenge? Try the circle subdivision problem!

● As a hint, remember that you can ask an object for its position and size with obj.getX(), obj.getY(), obj.getWidth(), and obj.getHeight().

Page 465: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 18.

● Your Task: Write a program that draws random circles on the screen that move when they're clicked on.

● Finished early and up for a challenge? Try the circle subdivision problem!

● As a hint, remember that you can ask an object for its position and size with obj.getX(), obj.getY(), obj.getWidth(), and obj.getHeight().

Page 466: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 18.

● Your Task: Write a program that draws random circles on the screen that move when they're clicked on.

● Finished early and up for a challenge? Try the circle subdivision problem!

● As a hint, remember that you can ask an object for its position and size with obj.getX(), obj.getY(), obj.getWidth(), and obj.getHeight().

Page 467: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 18.

● Your Task: Write a program that draws random circles on the screen that move when they're clicked on.

● Finished early and up for a challenge? Try the circle subdivision problem!

● As a hint, remember that you can ask an object for its position and size with obj.getX(), obj.getY(), obj.getWidth(), and obj.getHeight().

Page 468: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Fields

Page 469: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Friendly CircleGraphics Program

Page 470: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Fields

● A field (or instance variable) is a variable that can be read or written by any of the methods of a class.

● Syntax (defined outside of any method):

private type name;

● When using fields inside a method, just use its name, not its type.

Page 471: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Working with Images

● The GImage type is a graphics type that stores an image loaded from disk.

● To create a GImage, specify the name of the file you want to load like this:

GImage variableName = new GImage("filename");

● This will search the current directory for the file you're looking for. You'll need to move or copy the files into the appropriate directory to do this.

● You can add GImages to the canvas the same way that you add other objects and can move and resize them as usual.

Page 472: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

A Picture Reveal Game

Page 473: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

RevealPuzzle

Page 474: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 475: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 476: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 477: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 478: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 479: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 480: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 481: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming
Page 482: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sketchpad

Page 483: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sketchpad

Page 484: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sketchpad

Page 485: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Sketchpad

Page 486: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Clicking and Dragging

Page 487: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 488: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 489: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 490: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 491: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 492: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 493: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 494: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 495: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

SuchADrag

Page 496: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Your Turn!

● Import Java 19.

● Your Task: Right now, our mouse tracker can move the cat out of the window.

● Update the mouse tracker so that the cat picture always stays within the bounds of the window.

● Finish early? Update the Mirror Box so that it draws continuous lines. Or take one of your previous drawings and make it interactive!

MouseTracker

Page 497: Welcome Back! - Computer Science and Engineeringcse.unl.edu/~scooper/summer/lectures/Java.pdfTransitioning to Java For the remainder of Girl Code, we'll be using the Java programming

Project: Breakout!