Chapter 4 Assignment Statement An assignment statement gives
- Slides: 20
Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is assigned to x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable ( is assigned to x Slide 1 x z)
Chapter 4 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; Slide 2 x 10 5
Chapter 4 Primitive Data Types Type int double char boolean Slide 3 Storage Required 4 bytes 8 bytes 2 bytes 1 bit
Classic Asteroids Game from Atari Slide 4
A variable declared with a class is called an object. For example, the object a 47 is type Asteroid: Asteroid a 47 = new Asteroid(); a 47. set. Location(x, y) a 47. destroy() Slide 5
Chapter 4 Java Packages § Numerous packages are included with JRE (Java Runtime Environment) § Packages contain classes § Packages can be added to an application with an import statement. For example, the statement import java. util. Scanner; makes the Scanner class and its methods accessible to the application. Slide 6
Chapter 4 The Scanner Class § Part of the java. util package import java. util. *; § A Scanner object processes text and numbers from the input stream § Methods include: next() next. Line() next. Int() next. Double() next. Boolean() close() Slide 7
Chapter 4 Integer Division Integer division ( /) is performed when both operands are integers. Only the integer portion of the quotient is returned: Source Code int answer; answer = 20 / 7; System. out. print(answer); Output 2 Slide 8
Chapter 4 Real Division Real division ( /) is performed when one or both operands are type double. The entire quotient, including the decimal portion is returned: double result; result = 20. 0/7. 0; Slide 9 //result is 2. 857
Chapter 4 Modulus Division Modulus division ( division operation: %) returns the remainder of a Source Code int answer; answer = 20 % 7; System. out. print(answer); Output 6 Slide 10
Chapter 4 Operator Precedence Operators in Java have the following precedence: 1. multiplication and division 2. addition and subtraction Operators of the same precedence are evaluated in order from left to right. For example, multiplication is performed first, then division, and finally addition: 5 + 6 * 4 / 2 = 17 Slide 11
Chapter 4 Changing the Order of Operations The order in which operators are evaluated can be changed by using parentheses. For example, addition is performed first, then multiplication, and finally division: (5 + 6) * 4 / 2 = 22 Slide 12
Chapter 4 Type Casting Assigning an integer value into a floating-point variable works just fine. For example: double bank. Account. Balance = 1000; However assigning a floating-point value into an integer variable is like trying to fit a large object in a small box. By default, Java treats it as an illegal operation. For example, this causes an error: int temperature = 22. 8; Slide 13
Chapter 4 Type Casting converts a number of one type to a number of a different, but compatible type. Type casting is used to: 1. make the operand types in an expression match. For example, whole. Num = (int)y * 2 2. truncate the decimal portion of a double. For example, whole. Num = (int)z 3. change the way in which a division ( /) operation will be performed. For example, real. Division = (double)a / (double)b Slide 14
Chapter 4 Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment *= multiplication and then assignment /= division and then assignment %= modulus division and then assignment Slide 15
Chapter 4 Named Constants § A named memory location that cannot be changed from its initial value. § The keyword declaration. final is used in a constant § Constant identifiers are typically all uppercase with an underscore ( _) separating words within the identifier name. Slide 16
Chapter 4 Java Keywords abstract boolean break byte case catch char class const continue default do Slide 17 double else extends finally float for goto if implements import instanceof interface long native new package private protected public return short static strictfp super switch synchronized this throws transient try void volatile while
Chapter 4 Programming Errors § Syntax errors violate the rules of Java. § Logic errors, also called semantic errors, occur in statements that are syntactically correct, but produce undesired or unexpected results. § Run-time errors, also called exceptions, halt program execution at the statement that cannot be executed. One type of exception is called Input. Mismatch. Exception. Slide 18
Chapter 4 Flowchart Symbols process Slide 19
Chapter 4 The Birthday. Game Flowchart Slide 20
- Disadvantage of using a safe deposit box
- Love never fails never gives up
- Chapter 16 catcher in the rye
- Kinetic theory of solids
- Disposition romeo and juliet
- N
- On saturday rima's father gives her a seed
- Primitive streak gives rise to
- Hertwig's epithelial root sheath
- When an author gives hints or clues
- A woman gives a beggar 50 cents
- The ground gives way summary
- Hydrolysis of ether gives
- Kanga gives piglet a bath
- Vision gives pain a purpose
- Green theorem is the relation between
- Eric birling
- Personification explanation
- Allah gives life and death
- Which bacteria gives protection against gastric injury
- Hints that the author gives to help