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

Slides:



Advertisements
Similar presentations
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Copyright © Cengage Learning. All rights reserved.
Building Abstractions with Procedures (Part 1) CS 21a: Introduction to Computing I First Semester,
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS 201 Functions Debzani Deb.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Pseudocode.
Elementary Algebra Exam 2 Material.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
CIS Computer Programming Logic
Programming Logic and Design Sixth Edition
Programming.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Algorithms and their Applications CS2004 ( ) Dr Stephen Swift 1.2 Introduction to Algorithms.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Working With Objects Tonga Institute of Higher Education.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Algorithm Design.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Data Structures and Algorithms Dr. Tehseen Zia Assistant Professor Dept. Computer Science and IT University of Sargodha Lecture 1.
Working With Objects Tonga Institute of Higher Education.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
Building Abstractions with Variables (Part 1) CS 21a: Introduction to Computing I First Semester,
The Hashemite University Computer Engineering Department
Algorithms and Pseudocode
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Algorithms and Problem Solving
Introduction to Computer Science / Procedural – 67130
Data Structures and Algorithms
Introduction to Computer Programming
Programming – Touch Sensors
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Lesson 2 Programming constructs – Algorithms – Scratch – Variables Intro.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
T. Jumana Abu Shmais – AOU - Riyadh
Problem Solving Skill Area 305.1
Algorithms and Problem Solving
Introduction to Primitives
Introduction to Primitives
Basic Concepts of Algorithm
Presentation transcript:

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

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 ► Instead of a solution Program/ Algorithm Program/ Algorithm One answer Infinitely many answers Infinitely many questions

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 Program/ Algorithm Solution Infinitely many answers Problem Infinitely many questions Computation/ Program Instance Answer Problem Instance Choose one question out of the infinitely many

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 ► Instead of a solution Program/ Algorithm Program/ Algorithm One answer Infinitely many answers Infinitely many questions

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

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.

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.

The Anatomy of a Variable 3 number_of_coconuts name value variable

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

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

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.

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;

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 IdentifierValue number_of_coconuts3 cost_per_coconut12 environment 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