Unformatted text preview:

ExpressionsOnce upon a time . . .Holism vs. ReductionismSlide 4The Add2Integers ProgramExpressions in JavaPrimitive Data TypesConstants and VariablesVariable DeclarationsOperators and OperandsDivision and Type CastsThe Pitfalls of Integer DivisionSlide 13The Remainder OperatorPrecedenceExercise: Precedence EvaluationAssignment StatementsShorthand AssignmentsIncrement and Decrement OperatorsExtending Add2IntegersThe Add4Integers ProgramThe Repeat-N-Times IdiomThe AddNIntegers ProgramThe Repeat-Until-Sentinel IdiomThe AddIntegerList ProgramSlide 26Designing for ChangeThe EndExpressionsEric RobertsCS 106AJanuary 13, 2010Once upon a time . . .Holism vs. ReductionismIn his Pulitzer-prizewinning book, computer scientist Douglas Hofstadter identifies two concepts—holism and reductionism—that turn out to be important as you begin to learn about programming.Hofstadter explains these concepts using a dialogue in the style of Lewis Carroll:I will be glad to indulge both of you, if you will first oblige me, by telling me the meaning of these strange expressions, “holism” and “reductionism”.Achilles:Crab:Holism is the most natural thing in the world to grasp. It’s simply the belief that “the whole is greater than the sum of its parts”. No one in his right mind could reject holism.Anteater:Reductionism is the most natural thing in the world to grasp. It’s simply the belief that “a whole can be understood completely if you understand its parts, and the nature of their ‘sum’”. No one in her left brain could reject reductionism.ExpressionsThe Add2Integers ProgramAdd2Integersclass Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); }}n1 n2 totalThis program adds two numbers.Enter n2:The total is 42.17 25 42 25class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); }}n1 n2 total17 25 42Enter n1: 17Expressions in Java•The heart of the Add2Integers program from Chapter 2 is the line•The n1 + n2 that appears to the right of the equal sign is an example of an expression, which specifies the operations involved in the computation.•An expression in Java consists of terms joined together by operators.•Each term must be one of the following:–A constant (such as 3.14159265 or "hello, world")–A variable name (such as n1, n2, or total)–A method calls that returns a values (such as readInt)–An expression enclosed in parenthesesint total = n1 + n2;that performs the actual addition.Primitive Data Types•Although complex data values are represented using objects, Java defines a set of primitive types to represent simple data.•Of the eight primitive types available in Java, the programs in this text use only the following four:intThis type is used to represent integers, which are whole numbers such as 17 or –53.doubleThis type is used to represent numbers that include a decimal fraction, such as 3.14159265.charThis type represents a single character.booleanThis type represents a logical value (true or false).Constants and Variables•The simplest terms that appear in expressions are constants and variables. The value of a constant does not change during the course of a program. A variable is a placeholder for a value that can be updated as the program runs.•The format of a constant depends on its type:–Integral constants consist of a string of digits, optionally preceded by a minus sign, as in 0, 42, -1, or 1000000.–Floating-point constants include a decimal point, as in 3.14159265 or 10.0. Floating-point constants can also be expressed in scientific notation by adding the letter E and an exponent after the digits of the number, so that 5.646E-8 represents the number 5.646 x 10-8.–The two constants of type boolean are true and false.–Character and string constants are discussed in detail in Chapter 8. For the moment, all you need to know is that a string constant consists of a sequence of characters enclosed in double quotation marks, such as "hello, world".•A variable in Java is most easily envisioned as a box capable of storing a value.•Each variable has the following attributes:–A name, which enables you to differentiate one variable from another.–A type, which specifies what type of value the variable can contain.–A value, which represents the current contents of the variable.total(contains an int)42•The name and type of a variable are fixed. The value changes whenever you assign a new value to the variable.Variable Declarations•In Java, you must declare a variable before you can use it. The declaration establishes the name and type of the variable and, in most cases, specifies the initial value as well.type name = value;•The most common form of a variable declaration iswhere type is the name of a Java primitive type or class, name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value.•Most declarations appear as statements in the body of a method definition. Variables declared in this way are called local variables and are accessible only inside that method.•Variables may also be declared as part of a class. These are called instance variables and are covered in Chapter 6.Operators and Operands•As in most languages, Java programs specify computation in the form of arithmetic expressions that closely resemble expressions in mathematics.•The most common operators in Java are the ones that specify arithmetic computation:+Addition–Subtraction*Multiplication/Division%Remainder•Operators in Java usually appear between two subexpressions, which are called its operands. Operators that take two operands are called binary operators.•The - operator can also appear as a unary operator, as in the expression -x, which denotes the negative of x.Division and Type Casts•Whenever you apply a binary operator to numeric values in Java, the result will be of type int if


View Full Document

Stanford CS 106A - Expressions

Download Expressions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Expressions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Expressions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?