Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the...
Transcript of Introduction to Java - Stanford University...Announcements Programming Assignment #1 Out: Karel the...
![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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/48.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/49.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/50.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/51.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/52.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/53.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/54.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/55.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/56.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/57.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/58.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/59.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/60.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/61.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/62.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/63.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/64.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/65.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/66.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/67.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/68.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/69.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/70.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/71.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/72.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/73.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/74.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/75.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/76.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062606/5fe3b23b65fcc656b149bca9/html5/thumbnails/77.jpg)
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