Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:

Similar presentations


Presentation on theme: "© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:"— Presentation transcript:

1 © 2005 Lawrenceville Press Slide 1 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 x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable ( z ) is assigned to x

2 © 2005 Lawrenceville Press Slide 2 Chapter 4 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 5 10

3 © 2005 Lawrenceville Press Slide 3 Chapter 4 Primitive Data Types TypeStorage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit

4 © 2005 Lawrenceville Press Slide 4 Chapter 4 Abstract Data Types A variable declared with a class is called an object. For example, the object spot is type Circle: Circle spot = new Circle(4);spot getRadius() area()

5 © 2005 Lawrenceville Press 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.

6 © 2005 Lawrenceville Press Slide 6 Chapter 4 The Scanner Class  Part of the java.util package  A Scanner object processes text and numbers from the input stream  Methods include: next() nextLine() nextInt() nextDouble() nextBoolean() close()

7 © 2005 Lawrenceville Press Slide 7 Chapter 4 Integer Division Integer division ( / ) is performed when both operands are integers. Only the integer portion of the quotient is returned:

8 © 2005 Lawrenceville Press 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;//result is 2.857

9 © 2005 Lawrenceville Press Slide 9 Chapter 4 Modulus Division Modulus division ( % ) returns the remainder of a division operation:

10 © 2005 Lawrenceville Press 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

11 © 2005 Lawrenceville Press 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

12 © 2005 Lawrenceville Press Slide 12 Chapter 4 Type Casting 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, wholeNum = (int)y * 2 2.truncate the decimal portion of a double. For example, wholeNum = (int)z 3.change the way in which a division ( / ) operation will be performed. For example, realDivision = (double)a / (double)b

13 © 2005 Lawrenceville Press Slide 13 Chapter 4 Assignment Operators OperatorOperation += addition and then assignment -= subtraction and then assignment *= multiplication and then assignment /= division and then assignment %= modulus division and then assignment

14 © 2005 Lawrenceville Press Slide 14 Chapter 4 Named Constants  A named memory location that cannot be changed from its initial value.  The keyword final is used in a constant declaration.  Constant identifiers are typically all uppercase with an underscore ( _ ) separating words within the identifier name.

15 © 2005 Lawrenceville Press Slide 15 Chapter 4 Java Keywords abstractdoubleintstrictfp booleanelseinterfacesuper breakextendslongswitch bytefinalnativesynchronized casefinallynewthis catchfloatpackagethrow charforprivatethrows classgotoprotectedtransient constifpublictry continueimplementsreturnvoid defaultimportshortvolatile doinstanceofstaticWhile

16 © 2005 Lawrenceville Press Slide 16 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 InputMismatchException.

17 © 2005 Lawrenceville Press Slide 17 Chapter 4 Flowchart Symbols process

18 © 2005 Lawrenceville Press Slide 18 Chapter 4 The BirthdayGame Flowchart


Download ppt "© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:"

Similar presentations


Ads by Google