building abstractions with variables (part 2)
DESCRIPTION
Building Abstractions with Variables (Part 2). CS 21a: Introduction to Computing I First Semester, 2013-2014. Last Time…. The three main “customers” good programs must satisfy Expressions and statements Combinations through expressions and statements Recursive nature of expressions. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/1.jpg)
Building Abstractions
with Variables (Part 2)
CS 21a: Introduction to Computing I
First Semester, 2013-2014
![Page 2: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/2.jpg)
Last Time…►The three main “customers” good
programs must satisfy►Expressions and statements►Combinations through expressions
and statements►Recursive nature of expressions
![Page 3: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/3.jpg)
Outline►Important Differences►Abstraction through Variables►Abstraction In-Depth►Values and Environments, Names and
Scope
![Page 4: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/4.jpg)
A Combination of Expressions/Statements
►Gives an answer
►Instead of a solution
Program/Algorithm
Program/Algorithm
One answer
Infinitely manyanswers
Infinitely manyquestions
![Page 5: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/5.jpg)
Difference between Computations and Algorithms
► A computation answers a question.► An algorithm solves a problem.► Each particular selection of the set of inputs
to a problem is called a problem instance or question.
► Every time a program runs or an algorithm “runs,” the instance is called a computation.“Instance” connotes “at this instant,” “at a certain
finite point in time.” The difference between instances and whatever it is they are instantiating is that the former is the finite realization of the latter, which is more “infinite” because of its generality.
![Page 6: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/6.jpg)
Difference between Computations and Programs
Program/Algorithm
SolutionInfinitely many
answers
ProblemInfinitely many
questions
Computation/Program Instance AnswerProblem Instance
Choose one questionout of the infinitely many
![Page 7: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/7.jpg)
Difference between Algorithms and Programs
►Algorithms are abstract ideas.►Programs are algorithms written down
in a specific programming language.►Programming is the act of turning
algorithms into programs.
![Page 8: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/8.jpg)
Difference between Algorithms and Programs
►Programs can be run by a computer and each instance or run is a computation.
►Algorithms can’t be run, so there really is no algorithm instance. ►When you try to simulate an algorithm
(with specific inputs) in your mind, you have mentally turned the algorithm into a program, and are acting as a computer.
![Page 9: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/9.jpg)
Outline►Important Differences►Abstraction through Variables►Abstraction In-Depth►Values and Environments, Names and
Scope
![Page 10: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/10.jpg)
A Combination of Expressions/Statements
►Gives an answer
►Instead of a solution
Program/Algorithm
Program/Algorithm
One answer
Infinitely manyanswers
Infinitely manyquestions
![Page 11: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/11.jpg)
A Combination of Expressions/Statements
print(3 * 12);
![Page 12: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/12.jpg)
Abstraction through Variables
int number_of_coconuts = 3;
int cost_per_coconut = 12;
print(number_of_coconuts * cost_per_coconut);
Or
int number_of_coconuts = 3;
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
![Page 13: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/13.jpg)
Abstraction through Variables
int number_of_coconuts = 3;
int cost_per_coconut = 12;
print(number_of_coconuts * cost_per_coconut);
Or
int number_of_coconuts = 3;
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
Variable declaration and assignment, like procedure call, is another kind of statement.
![Page 14: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/14.jpg)
Abstraction through Variables
int number_of_coconuts = 3;
int cost_per_coconut = 12;
print(number_of_coconuts * cost_per_coconut);
Or
int number_of_coconuts = 3;
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
Notice how expressions can contain variables. If an atomic expression is a variable, its value is based on a previous assignment.
![Page 15: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/15.jpg)
Abstraction through Variables
int number_of_coconuts = 3;
int cost_per_coconut = 12;
print(number_of_coconuts * cost_per_coconut);
Or
int number_of_coconuts = 3;
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
Notice how expressions can be assigned to variables. The expression is evaluated first and the resulting value becomes the value the variable takes on.
![Page 16: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/16.jpg)
The Anatomy of a Variable
3number_of_coconutsname
value
variable
![Page 17: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/17.jpg)
Abstraction through Variables
► Input and output values can now be referred to with names.► The variable can be thought of as a bridge that
allows this.► Inputs can be changed without touching the
central algorithm► For now, we can solve problems (answer many
questions) with the same algorithm but only the “same” program.►The program still needs to be recompiled. How to
avoid this? Next time…
![Page 18: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/18.jpg)
Abstraction through Variables
►Variables allow each computation to be different, while still using the same algorithm.►+1 for the generality required for
science
![Page 19: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/19.jpg)
Abstraction through Variables
►Variable names still have no meaning ► In itself – remember, these are all just
symbols►But the names can be very suggestive
of meanings we should give the symbols so to us the program now looks more meaningful►+1 for the modularity or readability
required for humans.
![Page 20: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/20.jpg)
Practice Programming Problem:Adding Fractions
►Given two fractions, and , print out the numerator and the denominator, one on each line, of the sum . No need to express in lowest terms.
►Assume the following declarations:int a = 5, b = 3, c = 2, d = 6;
►Change the values to test if your program works. Prepare test cases (input/expected output pairs) in advance.
![Page 21: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/21.jpg)
Outline►Important Differences►Abstraction through Variables►Abstraction In-Depth►Values and Environments, Names and
Scope
![Page 22: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/22.jpg)
By Using Variables, You…►Are like using pronoun clauses►Do a multiplication on that which was
given to me as input
![Page 23: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/23.jpg)
By Using Variables, You…►Allow the algorithm to be an algorithm,
instead of just a specific instance►Do something to whatever it is that was
given to me►Whatever it is that was given to me can be
given a short but descriptive name for convenience and meaningfulness.►Programmers choose the name, but they must
choose responsibly.►Don’t care about the specific selection of input
![Page 24: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/24.jpg)
By Using Variables, You…►Give abstract symbols for concrete
things, and describe a general process in terms of the abstract symbols only, separate from the concrete things.►When it’s time to carry out the solution
for a specific instance, the abstract symbols are given concrete values and become concrete things again.
![Page 25: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/25.jpg)
This Is Not A New Idea►Think about money.►Money (variables) is an abstraction
over the relative worth (values) of things against each other.
►The numbers (symbols) we use to talk about money are different from the money.
![Page 26: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/26.jpg)
This Is Not A New Idea►We can have principles of finance, about
money alone.►Without respect to actual things that the
money can buy►Each time we are faced with a new concrete
situation, we can apply the same principles.►Example: Computing change. Actual prices
may vary but we apply the same process each time.
![Page 27: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/27.jpg)
So In Reality, Algorithms Do Not Manipulate Data
►A particular execution instance of a program, a computation, manipulates data.
►Algorithms only manipulate variables which later refer to data.
►The written program is a representation of that manipulation, and the identifiers are representations of variables.
![Page 28: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/28.jpg)
Remember►Thoughts are to algorithms are to variables►Abstract process, abstract language
as►Words are to programs are to identifiers►Abstract process, concrete language
as►Actions are to computations are to values►Concrete process, no language
![Page 29: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/29.jpg)
Why We Have Laws►To prevent crime?►Not the computer scientist’s answer!
►To solve a problem►Problem: a party has inflicted harm on
another, and has caused a dispute, an imbalance in their relationship.
►Desire behind the problem: to give justice, to balance out the imbalance created
![Page 30: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/30.jpg)
Why We Have Laws►A law (code or program) represents a
means (algorithm) of delivering justice (solving the problem) for a particular kind of dispute.
![Page 31: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/31.jpg)
Why We Have Laws►To settle a dispute between two
parties, a judicial arbiter/court (computer) must execute a law (computation).►The code can include phrases like “5 to
10 years sentence” (symbols to represent variables).►The actual penalties (values) can be
different for different circumstances.
![Page 32: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/32.jpg)
Discuss With Your Seatmates►Why we insist on discovering
mathematical formulae►If you ask a professional pianist to
play every other note of a piece, he can easily do it. On the other hand, a very good amateur fails at doing this. Give a possible explanation.
![Page 33: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/33.jpg)
Outline►Important Differences►Abstraction through Variables►Abstraction In-Depth►Values and Environments, Names
and Scope
![Page 34: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/34.jpg)
The Name is Not the Variable!►The name or identifier is a symbol.►The variable is the abstract entity
which the name refers to.►The variable is what the name means
(if it’s a name for a variable) at a given instant.►Other things with names: procedures,
keywords, classes
![Page 35: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/35.jpg)
The Value is Not the Variable!
►The value is what a variable “means” at a given instant.
►What “at a given instant” means will be clearer later. The phrase doesn’t mean the same thing in the two sentences.
![Page 36: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/36.jpg)
The Name is Not the Variable!
►Declaration is mapping a name to a variable.►To make it possible for the program to
refer to it►If it’s impossible to refer to a variable, it
might as well not exist.►Another definition: declaration is making a
variable exist.
![Page 37: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/37.jpg)
The Value is Not the Variable!
►Assignment is mapping a variable to a value.►To make it possible for the computation
to use it.Declaration and assignment can be separate:
int x;x = 42;
![Page 38: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/38.jpg)
The Name is Not the Variable!The Value is Not the Variable!
►You have a name, and you have values, but you are neither your name or your values!
►You are an abstract entity with a name and values!
![Page 39: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/39.jpg)
Variables Need Memory►Need memory for the name-value
pairs they hold►Variables only have values with
respect to an environment, a context, or a space in short-term memory.
![Page 40: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/40.jpg)
The Environment►Pronouns make no sense if they don’t
refer to anything.►You can declare variables without
assigning them anything, but you probably shouldn’t.
►Pronouns change what they mean depending on context.►Each new computation yields a new
environment.
![Page 41: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/41.jpg)
The Environment is Like a Table
Identifier Valuenumber_of_coconuts 3cost_per_coconut 12
environment
variable
![Page 42: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/42.jpg)
Identifiers Need Scope►Need a namespace or scope for the
names that refer to them.►Each new program yields a new scope.
►Two different variables can’t have the same name in the same scope!
int number_of_coconuts = 3;int number_of_coconuts = 5;
![Page 43: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/43.jpg)
Identifiers Need Scope►Need a namespace or scope for the
names that refer to them.►Each new program yields a new scope.
►Two different variables can’t have the same name in the same scope!
int number_of_coconuts = 3;int number_of_coconuts = 5;
![Page 44: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/44.jpg)
Also…►You can’t use a name that means
nothing (does not exist in the scope).
total_cost = number_of_coconuts * cost_per_banana;makes no sense, not just because the units are wrong
![Page 45: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/45.jpg)
Does This Work? Why Or Why Not?
int x = 3;print(x);
![Page 46: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/46.jpg)
How About This?int x;print(x);
![Page 47: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/47.jpg)
How About This?int x = 1;print(y);
![Page 48: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/48.jpg)
How About This?int car = 3;print(automobile);
![Page 49: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/49.jpg)
How About This?int car = 3;int automobile = 4;
![Page 50: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/50.jpg)
How About This?int car = 3;automobile = car;print(automobile);
![Page 51: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/51.jpg)
How About This?int car = 3;int automobile = car;print(automobile);
![Page 52: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/52.jpg)
How About This?int x = 3;print(x);int x = 5;print(x);
![Page 53: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/53.jpg)
How About This?int x = 3;print(x);x = 5;print(x);
![Page 54: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/54.jpg)
Summary►Algorithms, programs, and computations are
different.►Variables, identifiers, and values are different.►Problems and questions are different.►Solutions and answers are different.►We turn an answer into a solution with
abstraction.►Variables are the most basic tools for
abstraction.
![Page 55: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/55.jpg)
Summary►Identifiers must be unique in every
scope.►Variable names can’t be used if they
haven’t been declared first (don’t exist in the scope).
►Computations have environment.►Programs have scope.
![Page 56: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/56.jpg)
Practice Programming Problem:Big Balls?
►Read about data types in Java, particularly boolean data types.
►Read about operators in Java, particularly comparison operators.
![Page 57: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/57.jpg)
Practice Programming Problem:Big Balls?
►Given the dimensions of two spheres and a cube, figure out if the combined volume of the spheres is larger than the volume of the cube. Print true if they are, false otherwise.
►What declarations do you need? Include them on your own in your program.
►Test your program as done in the first practice.
![Page 58: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/58.jpg)
Practice Programming Problem:Big Balls?
►HintVolume of a
sphere:Volume of a cube:
![Page 59: Building Abstractions with Variables (Part 2)](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568163c6550346895dd4f9bb/html5/thumbnails/59.jpg)
Next Time…►More complicated programs will have
plenty of combinations of expressions and statements.►How to manage complexity, next time