Building Abstractions with Variables (Part 1)

Post on 22-Feb-2016

57 views 0 download

Tags:

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

transcript

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?►How did programming get that way?►Step by step development towards

OOP

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

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”

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.

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.

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.

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.

Everything

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

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

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

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.

Note the Analogy► Statement► Procedure Call► Argument

► More on arguments and procedures later...

► Expression► Operator► Operand

► The imperative programming paradigm distinguishes between the two.

Values►The value of an expression is the

result of evaluating it.

A Trivial Program►Solves a very limited number of

problems►Only one, in fact

►Has no meaning

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

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)

Not This!

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

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

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.

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.

Combination through Expressions

►Try it out on your computers!

Practice►Draw the expression tree for the

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

►Determine the value at each node.

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?

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

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.

Combinations of Expressions/Statements

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

►Still have no meaning

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.

Practice Programming Problem

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

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