1 / 38

Building Abstractions with Variables (Part 1)

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?

maine
Download Presentation

Building Abstractions with Variables (Part 1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  11. Outline • Introduction and Motivation • A Trivial Program • Combination through Expressions • Combination through Statements

  12. Note • Throughout this lecture System.out.printwill be abbreviated to just print.

  13. 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 printprocedure call. primitives statement expression

  14. A Trivial Program 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. 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 printprocedure call. primitives statement expression

  15. Note the Analogy • Statement • Procedure Call • Argument • More on arguments and procedures later... • Expression • Operator • Operand • The imperative programming paradigm distinguishes between the two.

  16. Values • The value of an expression is the result of evaluating it.

  17. A Trivial Program • Solves a very limited number of problems • Only one, in fact • Has no meaning

  18. Outline • Introduction and Motivation • A Trivial Program • Combination through Expressions • Combination through Statements

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

  20. Not This!

  21. Think • (Upside down) family tree • File system • Organization chart levels node

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

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

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

  25. Combination through Expressions • Try it out on your computers!

  26. Practice • Draw the expression tree for the following expression: 3*5-(6+3)/(3+2*(7-4)) • Determine the value at each node.

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

  28. Outline • Introduction and Motivation • A Trivial Program • Combination through Expressions • Combination through Statements

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

  30. Combinations of Expressions/Statements • Still only solve one problem, but different problems can be solved with different combinations • Still have no meaning

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

  32. Practice Programming Problem • Write a program that prints out the values of the following expressions, one per line:

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

More Related