Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can...
Transcript of Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can...
![Page 1: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/1.jpg)
Variables, Types,and Expressions
![Page 2: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/2.jpg)
Announcements
● Karel the Robot due right now.● Email: Due Sunday, January 22 at 11:59PM.
● Update to assignment due dates:● Assignments 2 – 5 going out one day later.● Contact me if this is a problem.● Updated syllabus will be posted to the course
website.
● Blank Java project available.● Play around with Java on your own!
![Page 3: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/3.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 4: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/4.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 5: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/5.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 6: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/6.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 7: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/7.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 8: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/8.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 9: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/9.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 10: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/10.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 11: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/11.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 12: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/12.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 13: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/13.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 14: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/14.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 15: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/15.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 16: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/16.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 17: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/17.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 18: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/18.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 19: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/19.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 20: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/20.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 21: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/21.jpg)
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 22: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/22.jpg)
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 23: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/23.jpg)
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 24: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/24.jpg)
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 25: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/25.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 26: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/26.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 27: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/27.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 28: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/28.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 29: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/29.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 30: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/30.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 31: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/31.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 32: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/32.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 33: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/33.jpg)
Values
137 int numVotes
0.97333 double fractionVoting
0.64110 double fractionYes
![Page 34: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/34.jpg)
Declaring Variables
![Page 35: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/35.jpg)
Declaring Variables
public void run() {
}
![Page 36: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/36.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828;
}
![Page 37: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/37.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828;
}
2.71828
ourDouble
![Page 38: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/38.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;
![Page 39: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/39.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828; int ourInt = 137;
}
2.71828
ourDouble
![Page 40: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/40.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828; int ourInt = 137;
}
2.71828
ourDouble
137
ourInt
![Page 41: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/41.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828; int ourInt = 137;
int anotherInt;
}
2.71828
ourDouble
137
ourInt
![Page 42: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/42.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828; int ourInt = 137;
int anotherInt;
}
2.71828
ourDouble
137
ourInt
anotherInt
![Page 43: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/43.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;
![Page 44: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/44.jpg)
Declaring Variables
public void run() { double ourDouble = 2.71828; int ourInt = 137;
int anotherInt; anotherInt = 42;
}
2.71828
ourDouble
137
ourInt
anotherInt
![Page 45: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/45.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 46: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/46.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.
![Page 47: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/47.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 48: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/48.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 49: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/49.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 50: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/50.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 51: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/51.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 52: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/52.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 53: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/53.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 54: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/54.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 55: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/55.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 56: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/56.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 57: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/57.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 58: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/58.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 59: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/59.jpg)
The Add2Integers Program
Add2Integers
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
25
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
17 25 42
Enter n1: 17
Graphic courtesy of Eric Roberts
![Page 60: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/60.jpg)
The GObject Hierarchy
The classes that represent graphical objects form a hierarchy, part of which looks like this:
GObject
GRect GOval GLineGLabel
Graphic courtesy of Eric Roberts
![Page 61: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/61.jpg)
public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world", 100, 75); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label); }}
HelloProgram
Sending Messages to a GLabel
hello, world
label
hello, worldhello, worldhello, world
public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world", 100, 75); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label); }}
label
hello, world
Graphic courtesy of Eric Roberts
![Page 62: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/62.jpg)
Objects and Variables
● Variables can be declared to hold objects.● The type of the variable is the name of
the class:● GLabel label;● GOval oval;
● Instances of a class can be created using the new keyword:● GLabel label = new GLabel("Y?", 0, 0);
![Page 63: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/63.jpg)
Sending Messages
● To call a method on an object stored in a variable, use the syntax
object.method(parameters)
● For example: label.setFont("Comic Sans-32");
label.setColor(Color.ORANGE);
![Page 64: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/64.jpg)
Graphics Coordinates
HelloProgram
hello, world
(100, 75)
• Origin is upper left.
• x coordinates increase from left to right.
• y coordinates increase from top to bottom.
• Units are pixels (dots on the screen).
• GLabel coordinates are baseline of first character.+x
+y
Graphic courtesy of Eric Roberts
![Page 65: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/65.jpg)
Operations on the GObject Class
object.setColor(color)Sets the color of the object to the specified color constant.
object.setLocation(x, y)Changes the location of the object to the point (x, y).
object.move(dx, dy)Moves the object on the screen by adding dx and dy to its current coordinates.
The following operations apply to all GObjects:
Standard color names defined in the java.awt package: Color.BLACKColor.DARK_GRAYColor.GRAYColor.LIGHT_GRAYColor.WHITE
Color.REDColor.YELLOWColor.GREENColor.CYAN
Color.BLUEColor.MAGENTAColor.ORANGEColor.PINK
Graphic courtesy of Eric Roberts
![Page 66: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/66.jpg)
Operations on the GLabel ClassConstructor new GLabel(text, x, y)
Creates a label containing the specified text that begins at the point (x, y).
Methods specific to the GLabel class label.setFont( font)
Sets the font used to display the label as specified by the font string.
The font is specified as
"family-style-size"
family is the name of a font family.style is either PLAIN, BOLD, ITALIC, or BOLDITALIC.size is an integer indicating the point size.
Graphic courtesy of Eric Roberts
![Page 67: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/67.jpg)
Drawing Geometrical Objects
Graphic courtesy of Eric Roberts
![Page 68: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/68.jpg)
Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)
Creates a rectangle whose upper left corner is at (x, y) of the specified size
Graphic courtesy of Eric Roberts
Graphics Program
+x
+y
(x, y)
(x + width, y + height)
![Page 69: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/69.jpg)
Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)
Creates a rectangle whose upper left corner is at (x, y) of the specified size
new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.
Graphic courtesy of Eric Roberts
Graphics Program
+x
+y
(x, y)
(x + width, y + height)
![Page 70: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/70.jpg)
Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)
Creates a rectangle whose upper left corner is at (x, y) of the specified size
new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.
new GLine( x0, y0, x1, y1)Creates a line extending from (x0, y0) to (x1, y1).
Graphic courtesy of Eric Roberts
Graphics Program
+x
+y
(x0, y
0)
(x1, y
1)
![Page 71: Variables, Types, and Expressions€¦ · Variables A variable is a location where a program can store information for later use. Each variable has three pieces of information associated](https://reader033.fdocuments.us/reader033/viewer/2022052016/602edc8d1de10d22480b6558/html5/thumbnails/71.jpg)
Drawing Geometrical ObjectsConstructors new GRect( x, y, width, height)
Creates a rectangle whose upper left corner is at (x, y) of the specified size
new GOval( x, y, width, height)Creates an oval that fits inside the rectangle with the same dimensions.
Methods shared by the GRect and GOval classes object.setFilled( fill)
If fill is true, fills in the interior of the object; if false, shows only the outline.
object.setFillColor( color)Sets the color used to fill the interior, which can be different from the border.
new GLine( x0, y0, x1, y1)Creates a line extending from (x0, y0) to (x1, y1).
Graphic courtesy of Eric Roberts