+ All Categories
Home > Documents > Building Abstractions with Variables (Part 1)

Building Abstractions with Variables (Part 1)

Date post: 22-Feb-2016
Category:
Upload: maine
View: 57 times
Download: 0 times
Share this document with a friend
Description:
Building Abstractions with Variables (Part 1). CS 21a: Introduction to Computing I First Semester, 2013-2014. Last Time…. How to write, compile, and run programs in Java Introduction to OOP, Java classes and objects. The Next Several Meetings…. Why OOP? Why does it look so complicated? - PowerPoint PPT Presentation
Popular Tags:
38
Building Abstractions with Variables (Part 1) CS 21a: Introduction to Computing I First Semester, 2013-2014
Transcript
Page 1: Building Abstractions with Variables (Part 1)

Building Abstractions

with Variables (Part 1)

CS 21a: Introduction to Computing I

First Semester, 2013-2014

Page 2: Building Abstractions with Variables (Part 1)

Last Time…►How to write, compile, and run

programs in Java►Introduction to OOP, Java classes and

objects

Page 3: Building Abstractions with Variables (Part 1)

The Next Several Meetings…►Why OOP? Why does it look so

complicated?►How did programming get that way?►Step by step development towards

OOP

Page 4: Building Abstractions with Variables (Part 1)

Outline►Introduction and Motivation►A Trivial Program►Combination through Expressions►Combination through Statements

Page 5: Building Abstractions with Variables (Part 1)

Three Elements of Every Powerful Programming Language

►Primitives► Simplest entities in a language

►Combination►Means by which compound elements are built

from simpler ones►Abstraction

►Means by which compound elements can be named and manipulated as a unit

►Means by which compound elements can be treated as “primitives”

Page 6: Building Abstractions with Variables (Part 1)

Why Primitives?►We only care about a certain level of detail.►There is a point where language ends and

action begins.►Real solutions to real problems must be easily

convertible to real means for carrying them out.

►Computers are concerned mainly with performance.► Limit to the number of primitives allows

computers to be efficient.

Page 7: Building Abstractions with Variables (Part 1)

Why Combination?►Science demands generality.

►Limit to number of primitives must be compensated by endless possibilities of wholes that can be formed by them.

►Humans need modularity.►We can’t keep viewing everything in

terms of the smallest parts, especially as the systems we’re looking at grow larger and larger.

Page 8: Building Abstractions with Variables (Part 1)

Why Abstraction?►Science demands generality.

►Useful solutions are those which can be applied in many circumstances, infinitely many selections of valid input.►Giving a name to the input data is one of the many

abstractions that can allow us to deal with infinity.►You abstract away from the concrete situation of

the world by giving symbols to things and manipulating only the symbols, turning the symbols back into things whenever the concrete situation calls for it.

Page 9: Building Abstractions with Variables (Part 1)

Why Abstraction?►Humans need modularity.►Once we’ve built larger parts out of the

smaller parts, there has to be a way to treat the larger parts as small parts to talk about even larger parts.

►Building abstraction barriers allow us to think in different levels of detail and choose one most appropriate to our goal.

Page 10: Building Abstractions with Variables (Part 1)

Everything

Page 11: Building Abstractions with Variables (Part 1)
Page 12: Building Abstractions with Variables (Part 1)
Page 13: Building Abstractions with Variables (Part 1)
Page 14: Building Abstractions with Variables (Part 1)
Page 15: Building Abstractions with Variables (Part 1)
Page 16: Building Abstractions with Variables (Part 1)

Outline►Introduction and Motivation►A Trivial Program►Combination through Expressions►Combination through Statements

Page 17: Building Abstractions with Variables (Part 1)

Note►Throughout this lecture System.out.print will be abbreviated to just print.

Page 18: Building Abstractions with Variables (Part 1)

A Trivial Program

print( 3 * 2 );

► 6 is the result of evaluating the expression.► 6 gets printed out because the computer executed the

statement.► 3 and 2 are operands of the * operator, the result of that

(6) is the argument to the print procedure call.

statement expression

primitives

Page 19: Building Abstractions with Variables (Part 1)

A Trivial Program

print( 3 * 2 );

► 6 is the result of evaluating the expression.► 6 gets printed out because the computer executed the

statement.► 3 and 2 are operands of the * operator, the result of that

(6) is the argument to the print procedure call.

statement expression

primitivesNote: The print procedure in Java is not really a primitive of the Java language, but the beauty of abstraction is that we can regard it as one for our purposes. It is someone else’s obligation to express the print procedure as a combination of lower-level primitives.

Page 20: Building Abstractions with Variables (Part 1)

Note the Analogy► Statement► Procedure Call► Argument

► More on arguments and procedures later...

► Expression► Operator► Operand

► The imperative programming paradigm distinguishes between the two.

Page 21: Building Abstractions with Variables (Part 1)

Values►The value of an expression is the

result of evaluating it.

Page 22: Building Abstractions with Variables (Part 1)

A Trivial Program►Solves a very limited number of

problems►Only one, in fact

►Has no meaning

Page 23: Building Abstractions with Variables (Part 1)

Outline►Introduction and Motivation►A Trivial Program►Combination through Expressions►Combination through Statements

Page 24: Building Abstractions with Variables (Part 1)

Combination through Expressions

print( 3 * 2 – ( 4 + 12 / 3 ));

► Expressions are recursive in nature and can be nested to form an expression tree.

► Use parentheses to specify evaluation order.► MDAS is default evaluation order, but is an artefact of

tradition and not intrinsic to programming.

expression (*, 6)expression (/, 4)expression (+, 8)

expression (-, -2)

Page 25: Building Abstractions with Variables (Part 1)

Not This!

Page 26: Building Abstractions with Variables (Part 1)

Think►(Upside down) family tree►File system►Organization chart

CEO

Chief Technical Officer

Project Head

Researcher

Developer

Project Head

Human Resources Officer

Chief Financial Officer

node

levels

Page 27: Building Abstractions with Variables (Part 1)

The Recursive Nature of Expressions

►An expression is either►A single atom

►A number by itself is an expression►Or a combination of two expressions

Page 28: Building Abstractions with Variables (Part 1)

The Recursive Nature of Expressions

►Each expression forms a tree.A tree is either►A single node►Or combinations of trees joined by a

single node.

Page 29: Building Abstractions with Variables (Part 1)

The Recursive Nature of Expressions

►The value of an expression is►The value of itself if it’s an atom.►The result of applying the combination

operator on the values of the two expressions, if it’s a combination.

Page 30: Building Abstractions with Variables (Part 1)

Combination through Expressions

►Try it out on your computers!

Page 31: Building Abstractions with Variables (Part 1)

Practice►Draw the expression tree for the

following expression: 3*5-(6+3)/(3+2*(7-4))

►Determine the value at each node.

Page 32: Building Abstractions with Variables (Part 1)

Practice►How many nodes are there in the

tree?►How many trees are there in the tree?►How many expressions are there in

the expression?►How many levels deep does the tree

go at the most?

Page 33: Building Abstractions with Variables (Part 1)

Outline►Introduction and Motivation►A Trivial Program►Combination through Expressions►Combination through Statements

Page 34: Building Abstractions with Variables (Part 1)

Combination through Statements

►Another, hopefully obvious, way to combine primitives is to perform them one after the other – direct sequencing.

►Try it: evaluate several expressions and print the results using one program.

Page 35: Building Abstractions with Variables (Part 1)

Combinations of Expressions/Statements

►Still only solve one problem, but different problems can be solved with different combinations

►Still have no meaning

Page 36: Building Abstractions with Variables (Part 1)

Summary►Expressions and statements are the simplest

parts of a program.►Expressions have values.►Expressions and statements can be primitives

or can also be combinations.►Expressions are combined by operators and

nesting, in a recursive manner.►Expressions can be visualized with trees.►Statements are combined by direct sequencing.

Page 37: Building Abstractions with Variables (Part 1)

Practice Programming Problem

►Write a program that prints out the values of the following expressions, one per line:

Page 38: Building Abstractions with Variables (Part 1)

Next Time…►Each combination of

expressions/statements only solves one question at a time.► I don’t want a really expensive

calculator.►How to fix that, next time


Recommended