mat 2170
play

Mat 2170 Week 3 Chapter Three Java Expressions Variable - PowerPoint PPT Presentation

Mat 2170 Chapter Three Java Expressions Mat 2170 Week 3 Chapter Three Java Expressions Variable Declarations Java Expressions Operators Mixed Mode Assignment Applications Spring 2014 Boolean Expressions Data Types Java


  1. Mat 2170 Chapter Three Java Expressions Mat 2170 Week 3 Chapter Three Java Expressions Variable Declarations Java Expressions Operators Mixed Mode Assignment Applications Spring 2014 Boolean Expressions Data Types Java Expressions Mat 2170 Chapter Three

  2. Student Responsibilities Mat 2170 Chapter Three Reading: Textbook, Chapters 3 Java Expressions Lab 2, due Thursday at the beginning of Lab 3 Week 3 1 Electronic Submission ( sub-mit ) Java Expressions 2 Publish to Web ( web ) Variable 3 Printouts from the exercises Declarations Operators Lab 3 Mixed Mode 1 Worksheet #3 - due at the beginning of lab 3 Assignment 2 Lab 3 exercises due at the beginning of Lab 4 Applications Boolean Expressions Attendance Data Types Java Expressions Mat 2170 Chapter Three

  3. Chapter Three — Expressions Mat 2170 Chapter Three 3.1 Primitive data types Java Expressions 3.2 Constants and variables Week 3 Java Expressions 3.3 Operators and operands Variable Declarations Operators 3.4 Assignment statements Mixed Mode Assignment 3.5 Boolean expressions Applications Boolean Expressions 3.6 Designing for change Data Types Java Expressions Mat 2170 Chapter Three

  4. Expressions in Java Mat 2170 Chapter Consider the Java statement: int total = n1 + n2; Three Java Expressions This statement combines the values in the integer objects Week 3 n1 and n2 and places the sum in the integer object total Java Expressions Variable The n1 + n2 that appears on the right hand side (RHS) Declarations of the assignment operator (=) is an example of an Operators expression, which specifies the operations involved in the Mixed Mode computation. Assignment Applications A Java expression consists of terms joined together by Boolean Expressions operators. Data Types Java Expressions Mat 2170 Chapter Three

  5. Valid Java Expression Terms Mat 2170 Chapter Three Java A constant such as 3.141592 or ” Hello, World! ” Expressions Week 3 Java An object name such as n1 , n2 , or total Expressions Variable Declarations Operators A method call that returns a value, such as readInt() Mixed Mode Assignment Applications An expression enclosed in parentheses Boolean Expressions Data Types Java Expressions Mat 2170 Chapter Three

  6. Constants and Variables Mat 2170 Chapter The simplest terms that appear in expressions are Three constants and variables . Java Expressions The value of a constant does not change during the Week 3 course of a program. Go figure! Java Expressions Variable Declaring constants (by convention, between the last Declarations curly braces of the run() method and the class): Operators Mixed Mode private static final int MULT1 = 3; Assignment private static final double PI = 3.14159; Applications private static final double TWO_PI = 2 * PI; Boolean Expressions DO NOT USE “MAGIC NUMBERS” Data Types (aka un-named constants ). Java Expressions Mat 2170 Chapter Three

  7. Variables Mat 2170 Chapter A variable is the name used to identify the piece of Three memory set aside during execution to hold a value; it can Java Expressions be updated as the program runs. Week 3 Java Each variable has three attributes: Expressions name : differentiates one variable from another Variable Declarations type : specifies type of value variable can contain Operators value : current contents of the variable Mixed Mode Assignment The name and type of a variable are fixed . Applications Boolean Expressions The value changes whenever you assign a new value to Data Types the variable. Java Expressions Mat 2170 Chapter Three

  8. Differences in Storage Mat 2170 total: an int = 42 Chapter Three 42 Java Expressions Week 3 Java minimum: a float = 0.001096 Expressions Variable −3 (1).09600000000000 Declarations Operators mantissa Mixed Mode exponent Assignment sign Applications Boolean Variables may be visualized as naming various sizes of shoe Expressions boxes (depending on type), and are capable of storing a single Data Types value. Java Expressions Mat 2170 Chapter Three

  9. Java Identifiers Mat 2170 Chapter Names for variables (and other things) are called Three identifiers . Java Expressions Identifiers in Java conform to the following rules: Week 3 Java Expressions Must begin with a letter or underscore Variable Declarations Remaining characters must be letters, digits, or underscore Operators Must not be one of Java’s reserved words Mixed Mode Should make their purpose obvious to the reader Assignment (programmer) Applications Boolean Should adhere to standard conventions. (Variable names, Expressions for example, usually begin with a lowercase letter; while Data Types constants are all uppercase.) Java Expressions Mat 2170 Chapter Three

  10. Java Reserved Words Mat 2170 These may not be used as identifiers Chapter Three Java Expressions abstract else interface super boolean extends long switch Week 3 break false native synchronized Java byte final new this Expressions case finally null throw Variable Declarations catch float package throws Operators char for private transient Mixed Mode class goto protected true Assignment const if public try Applications continue implements return void Boolean default import short volatile Expressions do instanceof static while Data Types double int strictfp Java Expressions Mat 2170 Chapter Three

  11. Variable Declarations Mat 2170 A variable must be declared before it can be used. Chapter Three The declaration establishes the type and name of the Java Expressions variable. Week 3 A common form of a variable declaration is: Java type name ; Expressions Variable Declarations where type is the name of a primitive type or a class Operators name is a unique identifier Mixed Mode Note that no value is assigned to the variable Assignment — it is considered to be uninitialized . Applications After a variable is declared, it may then be initialized with Boolean Expressions an assignment statement: Data Types name = value ; Java Expressions Mat 2170 Chapter Three

  12. Variable Declarations with Initialization Mat 2170 Chapter Three The declaration may also establish the type , name , and Java initial value , all in one statement. Expressions Week 3 Another common form of a variable declaration is: Java Expressions type name = value ; Variable Declarations where Operators type is the name of a primitive type or a class Mixed Mode name is a unique identifier, and Assignment value is an expression specifying the initial value Applications Boolean The variable can be given another value in an assignment Expressions Data Types statement, just as before. Java Expressions Mat 2170 Chapter Three

  13. Equivalent Examples Mat 2170 Chapter Three // Declare, then initialize Java int n1; Expressions int n2; Week 3 int total; Java n1 = readInt("Enter first integer: "); Expressions n2 = readInt("Enter second integer: "); Variable Declarations total = n1 + n2; Operators Mixed Mode Assignment // Declare and initialize in same statement Applications int n1 = readInt("Enter first integer: "); Boolean Expressions int n2 = readInt("Enter second integer: "); Data Types int total = n1 + n2; Java Expressions Mat 2170 Chapter Three

  14. Variable Declarations in Class Methods Mat 2170 Most declarations appear as statements in the body of a Chapter Three method definition. Two examples we’ve seen: Java Expressions int n1 = readInt("Enter first number: "); Week 3 GRect Face = Java Expressions new GRect(LeftX, TopY, Variable FaceWidth, FaceHeight); Declarations Operators Variables declared in methods are called local variables Mixed Mode and are accessible (known) only inside the method where Assignment they are declared. Applications Boolean Variables may also be declared as part of a class, and then Expressions Data Types are called instance variables (or data members of the class). Java Expressions Mat 2170 Chapter Three

  15. Operators and Operands Mat 2170 Java arithmetic expressions closely resemble mathematical Chapter Three expressions. Java Expressions The most common operators are the ones that specify arithmetic computation: Week 3 Java + Addition * Multiplication Expressions Subtraction / Division − Variable Declarations % Remainder Operators Operators usually appear between two sub–expressions, Mixed Mode called operands Assignment Applications binary operator : an operator that take two operands. Boolean Expressions The − (minus) operator can also appear as a unary Data Types operator, as in the expression − x , which denotes the negative of x . Java Expressions Mat 2170 Chapter Three

  16. Integer Division Mat 2170 Chapter Whenever you apply a binary operator to numeric values, Three the result will be of type int if both operands are of type Java Expressions int , but will be double if either operand (or both) is double . Week 3 Java Expressions This rule has important consequences Variable in the case of division — when two integers are the Declarations Operators operands, the resulting quotient is truncated , meaning Mixed Mode cut off ! Assignment Applications Beware of Integer Division : Boolean Expressions 3 / 5 is 0 Data Types 5 / 3 is 1 1292 / 100 is 12 Java Expressions Mat 2170 Chapter Three

Recommend


More recommend