76
Introduction to Java

Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Introduction to Java

Page 2: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Announcements

● Programming Assignment #1 Out:● Karel the Robot: Due Friday, January 18 at 3:15 PM.● Email: Due Sunday, January 20 at 11:59PM.

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

● Assignment review hours: 7:00 – 9:00PM in Herrin T-175.● Not recorded; sorry about that!

● LaIR hours start tonight!

Page 3: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

A Farewell to Karel

Page 4: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Welcome to Java

Page 5: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

But First...

A Brief History of Digital Computers

Page 6: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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

Page 7: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Programming in the 1940s

ElectricalDevice

Page 8: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 9: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

ComputerComputer

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler

Page 10: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler Computer

Page 11: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Programming Now (ish)

Compiler

Machine Code

move();turnLeft();

1101110010111011

Source Code

Page 12: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Hey! I wrote a programthat can draw stick figures!

That's great! I wrote aprogram that makes

speech bubbles!

Page 13: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Computer

Programming Now

Compiler

Source Code

Object File

move();turnLeft();

1101110111

1101110111

Machine Code

Linker

Object File

1101110111

Page 14: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Programming Now

Compiler

Source Code

Object File

Computer

move();turnLeft();

0010111000

Linker 1001010110

Machine Code

Linker

Object File

0110011101

Page 15: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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

Page 16: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Computer

The Java Model

Compiler

Source Code

.class File

Computer

move();turnLeft();

1101110111

Linker 1101110111

JAR File

JavaVirtual

Machine

Linker

.class File

1101110111

Page 17: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Let's See Some Java!

Page 18: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 19: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 20: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 21: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 22: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 24: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 25: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 26: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 28: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 29: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 30: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 31: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 32: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 37: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Variable Naming Conventions

● You are free to name variables as you see fit, but there are 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 38: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Variable Naming Conventions

● You are free to name variables as you see fit, but there are 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 39: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Variable Naming Conventions

● You are free to name variables as you see fit, but there are 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 40: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Variable Naming Conventions

● You are free to name variables as you see fit, but there are 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 41: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 42: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 43: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 44: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 46: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 47: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 48: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 49: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Values

137 int numVotes

0.97333 double fractionVoting

0.64110 double fractionYes

Page 50: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

Page 51: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

public void run() {

}

Page 52: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

public void run() { double ourDouble = 2.71828;

}

Page 53: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

public void run() { double ourDouble = 2.71828;

}

2.71828

ourDouble

Page 54: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 55: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

}

2.71828

ourDouble

Page 56: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

}

2.71828

ourDouble

137

ourInt

Page 57: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

int anotherInt;

}

2.71828

ourDouble

137

ourInt

Page 58: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

int anotherInt;

}

2.71828

ourDouble

137

ourInt

anotherInt

Page 59: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 60: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

int anotherInt; anotherInt = 42;

}

2.71828

ourDouble

137

ourInt

anotherInt

Page 61: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

Declaring Variables

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

int anotherInt; anotherInt = 42;

}

2.71828

ourDouble

137

ourInt

42

anotherInt

Page 62: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 63: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 64: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 65: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 66: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 67: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 68: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 69: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 70: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 71: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 72: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 73: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 74: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 75: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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 76: Introduction to Java - Stanford Universityweb.stanford.edu/class/archive/cs/cs106a/cs106a.1134/...Announcements Programming Assignment #1 Out: Karel the Robot: Due Friday, January

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