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

  • Published on
    19-Jan-2016

  • View
    216

  • Download
    2

Transcript

<p>Building Abstractions with Variables I</p> <p>Building Abstractions with Variables (Part 1)CS 21a: Introduction to Computing IFirst Semester, 2013-20141Last TimeHow to write, compile, and run programs in JavaIntroduction to OOP, Java classes and objectsThe Next Several MeetingsWhy OOP? Why does it look so complicated?How did programming get that way?Step by step development towards OOPOutlineIntroduction and MotivationA Trivial ProgramCombination through ExpressionsCombination through StatementsThree Elements of Every Powerful Programming LanguagePrimitivesSimplest entities in a languageCombinationMeans by which compound elements are built from simpler onesAbstractionMeans by which compound elements can be named and manipulated as a unitMeans by which compound elements can be treated as primitivesWhy 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 cant keep viewing everything in terms of the smallest parts, especially as the systems were looking at grow larger and larger.</p> <p>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.</p> <p>Unfortunately, its impossible to write a universal solution for all problems. Fortunately, programmers wont lose their jobs.</p> <p>8Why Abstraction?Humans need modularity.Once weve 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</p> <p>OutlineIntroduction and MotivationA Trivial ProgramCombination through ExpressionsCombination through StatementsNoteThroughout this lecture System.out.print will be abbreviated to just print.A Trivial Programprint( 3 * 2 );</p> <p>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.</p> <p>statementexpressionprimitives18A Trivial Programprint( 3 * 2 );</p> <p>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.</p> <p>statementexpressionprimitivesNote: 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 elses obligation to express the print procedure as a combination of lower-level primitives.19Note the AnalogyStatementProcedure CallArgument</p> <p>More on arguments and procedures later...ExpressionOperatorOperand</p> <p>The imperative programming paradigm distinguishes between the two.</p> <p>ValuesThe value of an expression is the result of evaluating it.A Trivial ProgramSolves a very limited number of problemsOnly one, in factHas no meaningOutlineIntroduction and MotivationA Trivial ProgramCombination through ExpressionsCombination through StatementsCombination through Expressionsprint( 3 * 2 ( 4 + 12 / 3 ));</p> <p>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.</p> <p>expression (*, 6)expression (/, 4)expression (+, 8)expression (-, -2)24Not This!</p> <p>Think(Upside down) family treeFile systemOrganization chartnodelevelsThe Recursive Nature of ExpressionsAn expression is eitherA single atomA number by itself is an expressionOr a combination of two expressionsThe Recursive Nature of ExpressionsEach expression forms a tree.A tree is eitherA single nodeOr combinations of trees joined by a single node.The Recursive Nature of ExpressionsThe value of an expression isThe value of itself if its an atom.The result of applying the combination operator on the values of the two expressions, if its a combination.</p> <p>Combination through ExpressionsTry it out on your computers!</p> <p>PracticeDraw the expression tree for the following expression: 3*5-(6+3)/(3+2*(7-4))Determine the value at each node.PracticeHow 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?</p> <p>OutlineIntroduction and MotivationA Trivial ProgramCombination through ExpressionsCombination through StatementsCombination through StatementsAnother, 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/StatementsStill only solve one problem, but different problems can be solved with different combinationsStill have no meaningSummaryExpressions 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 ProblemNext TimeEach combination of expressions/statements only solves one question at a time.I dont want a really expensive calculator.How to fix that, next time</p>

Recommended

View more >