Variables, Types,
and Expressions
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!
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?
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?
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?
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?
Variables
● A variable is a location where a program can
store information for later use.
int numVoters
● 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?
Variables
● A variable is a location where a program can
store information for later use.
int numVoters
● 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?
Variables
● A variable is a location where a program can
store information for later use.
int numVoters
● 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?
Variables
● A variable is a location where a program can
store information for later use.
int numVoters
● 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?
Variables
● A variable is a location where a program can
store information for later use.
137 int numVoters
● 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?
Variables
A variable is a location where a program can
store information for later use.
137 int numVoters
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?
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
7thHorcrux LOUD_AND_PROUD
Harry Potter that'sACoolName
noOrdinaryRabbit true
lots_of_underscores C_19_H_14_O_5_S
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
noOrdinaryRabbit
lots_of_underscores C_19_H_14_O_5_S
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
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
Values
137 int numVotes
0.97333 double fractionVoting
0.64110 double fractionYes
Declaring Variables
Declaring Variables
public void run() {
}
Declaring Variables
public void run() {
double ourDouble = 2.71828;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble
The syntax for declaring
a variable with an initial
value is
type name = value;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
137
ourInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137
ourInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137
ourInt
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137
ourInt Variables can be declared
without an initial value:
type name;
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137 anotherInt = 42;
ourInt
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137 anotherInt = 42;
ourInt
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137 anotherInt = 42;
ourInt
An assignment statement has
the form
42
anotherInt
variable = value;
} This stores value in variable.
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
137 anotherInt = 42;
ourInt
ourInt = 13;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
13 anotherInt = 42;
ourInt
ourInt = 13;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
13 anotherInt = 42;
ourInt
ourInt = 13;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
13 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
42
anotherInt
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
42
anotherInt anotherInt = ourInt;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
14
anotherInt anotherInt = ourInt;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
14
anotherInt anotherInt = ourInt;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
14 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
14
anotherInt anotherInt = ourInt;
ourInt = 1258;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
1258 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
14
anotherInt anotherInt = ourInt;
ourInt = 1258;
}
Declaring Variables
public void run() {
2.71828 double ourDouble = 2.71828;
ourDouble int ourInt = 137;
int anotherInt;
1258 anotherInt = 42;
ourInt
ourInt = 13;
ourInt = ourInt + 1;
14
anotherInt anotherInt = ourInt;
ourInt = 1258;
}
The Add2Integers Program
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
Add2Integers
This program adds two numbers.
Enter n1: 17
Enter n2: 25
The total is 42.
Graphic courtesy of Eric Roberts
The GObject Hierarchy
The classes that represent graphical objects form a
hierarchy, part of which looks like this:
GObject
GLabel GRect GOval GLine
Graphic courtesy of Eric Roberts
Sending Messages to a GLabel
public class HelloProgram extends GraphicsProgram {
public void run() {
GLabel label = new GLabel("hello, world", 100, 75);
[Link]("SansSerif-36");
[Link]([Link]);
add(label);
} label
}
hello, world
hello, world
HelloProgram
hello, world
Graphic courtesy of Eric Roberts
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);
Sending Messages
● To call a method on an object stored in a
variable, use the syntax
[Link](parameters)
● For example:
[Link]("Comic Sans-32");
[Link]([Link]);
Graphics Coordinates
• 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
HelloProgram
hello, world
+y
(100, 75)
Graphic courtesy of Eric Roberts
Operations on the GObject Class
The following operations apply to all GObjects:
[Link](color)
Sets the color of the object to the specified color constant.
[Link](x, y)
Changes the location of the object to the point (x, y).
[Link](dx, dy)
Moves the object on the screen by adding dx and dy to its current
coordinates.
Standard color names defined in the [Link] package:
[Link] [Link] [Link]
Color.DARK_GRAY [Link] [Link]
[Link] [Link] [Link]
Color.LIGHT_GRAY [Link] [Link]
[Link]
Graphic courtesy of Eric Roberts
Operations on the GLabel Class
Constructor
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
[Link]( 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
Drawing Geometrical Objects
Graphic courtesy of Eric Roberts
Drawing Geometrical Objects
Constructors
new GRect( x, y, width, height)
Creates a rectangle whose upper left corner is at (x, y) of the specified size
+x
Graphics Program
(x, y)
(x + width, y + height)
+y
Graphic courtesy of Eric Roberts
Drawing Geometrical Objects
Constructors
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.
+x
Graphics Program
(x, y)
(x + width, y + height)
+y
Graphic courtesy of Eric Roberts
Drawing Geometrical Objects
Constructors
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( x , y , x , y ) 0 0 1 1
Creates a line extending from (x0, y0) to (x1, y1).
+x
Graphics Program
(x0, y0)
(x1, y1)
+y
Graphic courtesy of Eric Roberts
Drawing Geometrical Objects
Constructors
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( x , y , x , y ) 0 0 1 1
Creates a line extending from (x0, y0) to (x1, y1).
Methods shared by the GRect and GOval classes
[Link]( fill)
If fill is true, fills in the interior of the object; if false, shows only the
outline.
[Link]( color)
Sets the color used to fill the interior, which can be different from the
border.
Graphic courtesy of Eric Roberts