Lecture 8b

advertisement
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
Outline
► Important
Differences
► Abstraction through Variables
► Abstraction In-Depth
► Values and Environments, Names and
Scope
A Combination of
Expressions/Statements
► Gives
an answer
Program/
Algorithm
► Instead
Infinitely many
questions
One answer
of a solution
Program/
Algorithm
Infinitely many
answers
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.
Difference between Computations
and Programs
Problem
Infinitely many
questions
Problem Instance
Choose one question
out of the infinitely many
Program/
Algorithm
Computation/
Program Instance
Solution
Infinitely many
answers
Answer
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.
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.
Outline
► Important
Differences
► Abstraction through Variables
► Abstraction In-Depth
► Values and Environments, Names and
Scope
A Combination of
Expressions/Statements
► Gives
an answer
Program/
Algorithm
► Instead
Infinitely many
questions
One answer
of a solution
Program/
Algorithm
Infinitely many
answers
A Combination of
Expressions/Statements
print(3 * 12);
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);
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;
Variable declaration and
assignment, like procedure
call, is another kind of
statement.
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
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;
Notice how expressions can
contain variables. If an
atomic expression is a
variable, its value is based
on a previous assignment.
int cost_per_coconut = 12;
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
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;
Notice how expressions can
be assigned to variables.
The expression is evaluated
first and the resulting value
becomes the value the
variable takes on.
int total_cost = number_of_coconuts * cost_per_coconut;
print(total_cost);
The Anatomy of a Variable
name
number_of_coconuts
value
variable
3
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…
Abstraction through Variables
► Variables
allow each computation to be
different, while still using the same
algorithm.
► +1
for the generality required for science
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.
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.
Outline
► Important
Differences
► Abstraction through Variables
► Abstraction In-Depth
► Values and Environments, Names and
Scope
By Using Variables, You…
► Are
like using pronoun clauses
► Do a multiplication on that which was given
to me as input
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
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.
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.
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.
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.
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
Why We Have Laws
► To
prevent crime?
► Not
► To
the computer scientist’s answer!
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
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.
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.
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.
Outline
► Important
Differences
► Abstraction through Variables
► Abstraction In-Depth
► Values and Environments, Names and
Scope
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
The Value is Not the Variable!
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.
► The
The Name is Not the Variable!
► Declaration
variable.
is mapping a name to a
make it possible for the program to refer
to it
► To
►If
it’s impossible to refer to a variable, it might
as well not exist.
► Another
definition: declaration is making a
variable exist.
The Value is Not the Variable!
► Assignment
value.
is mapping a variable to a
make it possible for the computation to
use it.
► To
Declaration and assignment can be separate:
int x;
x = 42;
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!
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.
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.
The Environment is Like a Table
environment
Identifier
Value
number_of_coconuts
3
cost_per_coconut
12
variable
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;
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;
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
Does This Work? Why Or Why Not?
int x = 3;
print(x);
How About This?
int x;
print(x);
How About This?
int x = 1;
print(y);
How About This?
int car = 3;
print(automobile);
How About This?
int car = 3;
int automobile = 4;
How About This?
int car = 3;
automobile = car;
print(automobile);
How About This?
int car = 3;
int automobile = car;
print(automobile);
How About This?
int x = 3;
print(x);
int x = 5;
print(x);
How About This?
int x = 3;
print(x);
x = 5;
print(x);
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.
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.
Practice Programming Problem:
Big Balls?
► Read
about data types in Java, particularly
boolean data types.
► Read about operators in Java, particularly
comparison operators.
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.
Practice Programming Problem:
Big Balls?
► Hint
Volume of a sphere:
Volume of a cube:
Next Time…
► More
complicated programs will have
plenty of combinations of expressions and
statements.
► How
to manage complexity, next time
Download