Download presentation
Presentation is loading. Please wait.
Published byWilliam Kelly Modified over 8 years ago
1
Building Abstractions with Variables (Part 1) CS 21a: Introduction to Computing I First Semester, 2013-2014
2
Last Time… ► How to write, compile, and run programs in Java ► Introduction to OOP, Java classes and objects
3
The Next Several Meetings… ► Why OOP? Why does it look so complicated? ► How did programming get that way? ► Step by step development towards OOP
4
Outline ► Introduction and Motivation ► A Trivial Program ► Combination through Expressions ► Combination through Statements
5
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”
6
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.
7
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.
8
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.
9
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.
10
Everything
16
Outline ► Introduction and Motivation ► A Trivial Program ► Combination through Expressions ► Combination through Statements
17
Note ► Throughout this lecture System.out.print will be abbreviated to just print.
18
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
19
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 Note: 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.
20
Note the Analogy ► Statement ► Procedure Call ► Argument ► More on arguments and procedures later... ► Expression ► Operator ► Operand ► The imperative programming paradigm distinguishes between the two.
21
Values ► The value of an expression is the result of evaluating it.
22
A Trivial Program ► Solves a very limited number of problems ► Only one, in fact ► Has no meaning
23
Outline ► Introduction and Motivation ► A Trivial Program ► Combination through Expressions ► Combination through Statements
24
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)
25
Not This!
26
Think ► (Upside down) family tree ► File system ► Organization chart CEO Chief Technical Officer Project Head ResearcherDeveloper Project Head Human Resources Officer Chief Financial Officer node levels
27
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
28
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.
29
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.
30
Combination through Expressions ► Try it out on your computers!
31
Practice ► Draw the expression tree for the following expression: 3*5-(6+3)/(3+2*(7-4)) ► Determine the value at each node.
32
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?
33
Outline ► Introduction and Motivation ► A Trivial Program ► Combination through Expressions ► Combination through Statements
34
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.
35
Combinations of Expressions/Statements ► Still only solve one problem, but different problems can be solved with different combinations ► Still have no meaning
36
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.
37
Practice Programming Problem
38
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.