Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Abstractions with Variables (Part 2) CS 21a: Introduction to Computing I First Semester, 2013-2014.

Similar presentations


Presentation on theme: "Building Abstractions with Variables (Part 2) CS 21a: Introduction to Computing I First Semester, 2013-2014."— Presentation transcript:

1 Building Abstractions with Variables (Part 2) CS 21a: Introduction to Computing I First Semester, 2013-2014

2 Last Time… ► The three main “customers” good programs must satisfy ► Expressions and statements ► Combinations through expressions and statements ► Recursive nature of expressions

3 Outline ► Important Differences ► Abstraction through Variables ► Abstraction In-Depth ► Values and Environments, Names and Scope

4 A Combination of Expressions/Statements ► Gives an answer ► Instead of a solution Program/ Algorithm Program/ Algorithm One answer Infinitely many answers Infinitely many questions

5 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.

6 Difference between Computations and Programs Program/ Algorithm Solution Infinitely many answers Problem Infinitely many questions Computation/ Program Instance Answer Problem Instance Choose one question out of the infinitely many

7 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.

8 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.

9 Outline ► Important Differences ► Abstraction through Variables ► Abstraction In-Depth ► Values and Environments, Names and Scope

10 A Combination of Expressions/Statements ► Gives an answer ► Instead of a solution Program/ Algorithm Program/ Algorithm One answer Infinitely many answers Infinitely many questions

11 A Combination of Expressions/Statements print(3 * 12);

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);

13 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.

14 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.

15 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.

16 The Anatomy of a Variable 3 number_of_coconuts name value variable

17 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…

18 Abstraction through Variables ► Variables allow each computation to be different, while still using the same algorithm. ► +1 for the generality required for science

19 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.

20 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.

21 Outline ► Important Differences ► Abstraction through Variables ► Abstraction In-Depth ► Values and Environments, Names and Scope

22 By Using Variables, You… ► Are like using pronoun clauses ► Do a multiplication on that which was given to me as input

23 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

24 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.

25 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.

26 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.

27 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.

28 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

29 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

30 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.

31 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.

32 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.

33 Outline ► Important Differences ► Abstraction through Variables ► Abstraction In-Depth ► Values and Environments, Names and Scope

34 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

35 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.

36 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.

37 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;

38 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!

39 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.

40 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.

41 The Environment is Like a Table IdentifierValue number_of_coconuts3 cost_per_coconut12 environment variable

42 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;

43 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;

44 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

45 Does This Work? Why Or Why Not? int x = 3; print(x);

46 How About This? int x; print(x);

47 How About This? int x = 1; print(y);

48 How About This? int car = 3; print(automobile);

49 How About This? int car = 3; int automobile = 4;

50 How About This? int car = 3; automobile = car; print(automobile);

51 How About This? int car = 3; int automobile = car; print(automobile);

52 How About This? int x = 3; print(x); int x = 5; print(x);

53 How About This? int x = 3; print(x); x = 5; print(x);

54 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.

55 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.

56 Practice Programming Problem: Big Balls? ► Read about data types in Java, particularly boolean data types. ► Read about operators in Java, particularly comparison operators.

57 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.

58 Practice Programming Problem: Big Balls? ► Hint Volume of a sphere:Volume of a cube:

59 Next Time… ► More complicated programs will have plenty of combinations of expressions and statements. ► How to manage complexity, next time


Download ppt "Building Abstractions with Variables (Part 2) CS 21a: Introduction to Computing I First Semester, 2013-2014."

Similar presentations


Ads by Google