+ All Categories
Home > Documents > Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the...

Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the...

Date post: 01-Sep-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
77
Introduction to Java
Transcript
Page 1: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Introduction to Java

Page 2: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

A Farewell to Karel

Page 3: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Welcome to Java

Page 4: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

But First...

A Brief History of Digital Computers

Page 5: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Image credit: http://upload.wikimedia.org/wikipedia/commons/4/4e/Eniac.jpg

Page 6: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Programming in the 1940s

ElectricalDevice

Page 7: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpg

http://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

Page 8: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

ComputerComputer

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler

Page 9: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler Computer

Page 10: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Programming Now (ish)

Compiler

Machine Code

move();turnLeft();

1101110010111011

Source Code

Page 11: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Hey! I wrote a programthat can draw stick figures!

That's great! I wrote aprogram that makes

speech bubbles!

Page 12: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Computer

Programming Now

Compiler

Source Code

Object File

move();turnLeft();

1101110111

1101110111

Machine Code

Linker

Object File

1101110111

Page 13: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Programming Now

Compiler

Source Code

Object File

Computer

move();turnLeft();

0010111000

Linker 1001010110

Machine Code

Linker

Object File

0110011101

Page 14: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Image credit: http://upload.wikimedia.org/wikipedia/commons/d/d2/Internet_map_1024.jpg

Page 15: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Computer

The Java Model

Compiler

Source Code

.class File

Computer

move();turnLeft();

1101110111

Linker 1101110111

JAR File

JavaVirtual

Machine

Linker

.class File

1101110111

Page 16: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Time-Out For Announcements

Page 17: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Section Assignments

● Section assignments given out on Tuesday; you can submit assignments once you have an SL assigned.● Didn't sign up? Signups reopen on Tuesday.

● Section Handout 1 released.● Recommendation: review the handout and

think about the problem before attending section.

● Section problems are not collected or graded; you'll go over them in section.

Page 18: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Announcements

● Programming Assignment #1 Out:● Karel the Robot: Due Friday, January 17 at

3:15 PM.– Suggestion: Try to have a working solution to all

the Karel problems by Wednesday. That gives you two buffer days to do final testing and cleanup.

● Email: Due Sunday, January 19 at 11:59PM.– Please wait until you get your section

assignments before writing these emails – we'd like you to introduce yourself to your SL as well!

Page 19: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Getting Help

● It's normal to ask for help in CS106A!● LaIR hours start tonight! 6PM – Midnight,

Sunday through Thursday.● Keith's Office Hours:

● Tuesday, 10:15AM – 12:15PM in Gates 505.● Wednesday, 4:30PM – 6:30PM in Gates 505.

● QuestionHut (link on the CS106A website)● Q&A site for CS106A.● Keith and Vikas frequently look over it, and you

can answer questions as well!

Page 20: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Let's See Some Java!

Page 21: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

The Add2Integers Program

Add2Integers

public class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); }} n1 n2 total

This program adds two numbers.

Enter n2:The total is 42.

17 25 42

25Enter n1: 17

Graphic courtesy of Eric Roberts

Page 22: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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?

Page 23: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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?

Page 24: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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?

Page 25: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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?

Page 26: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 27: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 28: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 29: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 30: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 31: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 32: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 33: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 34: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 35: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 36: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 37: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 38: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w7thHorcrux LOUD_AND_PROUDHarry Potter that'sACoolNamenoOrdinaryRabbit truelots_of_underscores C_19_H_14_O_5_S

Page 39: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 w LOUD_AND_PROUD

noOrdinaryRabbitlots_of_underscores C_19_H_14_O_5_S

Page 40: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 41: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 42: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 43: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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 44: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 45: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 46: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 47: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 48: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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. ● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 49: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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)● char: Characters (letters, punctuation, etc.)● boolean: Logical true and false.

Page 50: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.

Page 51: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

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.● char: Characters and punctuation.

Page 52: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Values

137 int numVotes

0.97333 double fractionVoting

0.64110 double fractionYes

Page 53: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

Page 54: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() {

}

Page 55: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828;

}

Page 56: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828;

}

2.71828

ourDouble

Page 57: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828;

}

2.71828

ourDouble

The syntax for declaring a variable with an initial

value is

type name = value;

The syntax for declaring a variable with an initial

value is

type name = value;

Page 58: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

}

2.71828

ourDouble

Page 59: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

}

2.71828

ourDouble

137

ourInt

Page 60: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt;

}

2.71828

ourDouble

137

ourInt

Page 61: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt;

}

2.71828

ourDouble

137

ourInt

anotherInt

Page 62: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt;

}

2.71828

ourDouble

137

ourInt

anotherInt

Variables can be declared without an initial value:

type name;

Variables can be declared without an initial value:

type name;

Page 63: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

}

2.71828

ourDouble

137

ourInt

anotherInt

Page 64: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

}

2.71828

ourDouble

137

ourInt

42

anotherInt

Page 65: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

}

2.71828

ourDouble

137

ourInt

42

anotherInt

An assignment statement has the form

variable = value;

This stores value in variable.

An assignment statement has the form

variable = value;

This stores value in variable.

Page 66: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13;

}

2.71828

ourDouble

137

ourInt

42

anotherInt

Page 67: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13;

}

2.71828

ourDouble

13

ourInt

42

anotherInt

Page 68: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13;

}

2.71828

ourDouble

13

ourInt

42

anotherInt

Page 69: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

}

2.71828

ourDouble

13

ourInt

42

anotherInt

Page 70: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

}

2.71828

ourDouble

14

ourInt

42

anotherInt

Page 71: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

}

2.71828

ourDouble

14

ourInt

42

anotherInt

Page 72: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; }

2.71828

ourDouble

14

ourInt

42

anotherInt

Page 73: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; }

2.71828

ourDouble

14

ourInt

14

anotherInt

Page 74: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; }

2.71828

ourDouble

14

ourInt

14

anotherInt

Page 75: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; ourInt = 1258;}

2.71828

ourDouble

14

ourInt

14

anotherInt

Page 76: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; ourInt = 1258;}

2.71828

ourDouble

1258

ourInt

14

anotherInt

Page 77: Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January 17 at 3:15 PM. – Suggestion: Try to have a working

Declaring Variables

public void run() { double ourDouble = 2.71828; int ourInt = 137;

int anotherInt; anotherInt = 42;

ourInt = 13; ourInt = ourInt + 1;

anotherInt = ourInt; ourInt = 1258;}

2.71828

ourDouble

1258

ourInt

14

anotherInt


Recommended