Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Assignment Statement

Similar presentations


Presentation on theme: "Chapter 4 Assignment Statement"— Presentation transcript:

1 Chapter 4 Assignment Statement
5/19/ :31 AM 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 Refer to page 78 in the text. Assignment requires an equal sign (=). The value on the right is "given to" or "assigned" the value of the variable on the left. © 2007 Lawrenceville Press

2 Chapter 4 Variable Assignment
5/19/ :31 AM Chapter 4 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 10 5 Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. Refer to page 78 in the text. The variable declaration, int x, associates the variable name x with a memory location <press space bar> The first assignment statement, x = 5, associates the memory location for x with the value 5 <press space bar> The second assignment statement, x = 10, associates the memory location for x with the value 10 <press space bar> Note that once the variable x is associated with a new value, in this case 10, the previous value, 5, cannot longer be accessed. © 2007 Lawrenceville Press

3 Chapter 4 Primitive Data Types
5/19/ :31 AM Chapter 4 Primitive Data Types Type Storage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit Refer to page 79 in the text. It is important to choose the appropriate data type when declaring variables so that the compiler allocates only the needed storage space for a variable. For example, declaring every variable a double will ensure that both ints and doubles can be represented, but unneeded storage space will be used. Additionally, the reader of the application will not be clear on what type of value a variable is expected to hold. © 2007 Lawrenceville Press

4 Chapter 4 Abstract Data Types
5/19/ :31 AM 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() Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains one (1) animation. Refer to page 80 in the text. The instantiation of object spot creates a reference to the object's data and method. <press space bar> © 2007 Lawrenceville Press

5 Numerous packages are included with JDK Packages contain classes
5/19/ :31 AM Chapter 4 Java Packages Numerous packages are included with JDK 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. Refer to pages 80 and 81 in the text. Packages are reusable units of code that can be added to an application. An asterisk (*) can be used in place of a class name to import all the classes of a package. © 2007 Lawrenceville Press

6 Chapter 4 The Scanner Class
5/19/ :31 AM 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() Refer to page 81 in the text. The Scanner class is new to Java 5. It is used to read input from the user. The Scanner method that should be used depends on the data expected from the user. © 2007 Lawrenceville Press

7 Chapter 4 Integer Division
5/19/ :31 AM Chapter 4 Integer Division Integer division (/) is performed when both operands are integers. Only the integer portion of the quotient is returned: Refer to page 83 in the text. © 2007 Lawrenceville Press

8 5/19/ :31 AM 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 Refer to page 83 in the text. © 2007 Lawrenceville Press

9 Chapter 4 Modulus Division
5/19/ :31 AM Chapter 4 Modulus Division Modulus division (%) returns the remainder of a division operation: Refer to page 83 in the text. © 2007 Lawrenceville Press

10 Chapter 4 Operator Precedence
5/19/ :31 AM 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 Refer to page 84 in the text. © 2007 Lawrenceville Press

11 Chapter 4 Changing the Order of Operations
5/19/ :31 AM 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 Refer to page 84 in the text. © 2007 Lawrenceville Press

12 5/19/ :31 AM 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 Refer to pages 84 and 85 in the text. © 2007 Lawrenceville Press

13 Chapter 4 Assignment Operators
5/19/ :31 AM 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 Refer to page 86 in the text. © 2007 Lawrenceville Press

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. Refer to page 87 in the text. © 2007 Lawrenceville Press

15 Chapter 4 Java Keywords abstract double int strictfp boolean else
5/19/ :31 AM Chapter 4 Java Keywords abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void default import short volatile do instanceof static While Refer to page 88 in the text. Keywords have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier. © 2007 Lawrenceville Press

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. Refer to page 87 in the text. © 2007 Lawrenceville Press

17 Chapter 4 Flowchart Symbols
5/19/ :31 AM Chapter 4 Flowchart Symbols process Refer to page 91 in the text. The rectangle flowchart symbol indicates a process. © 2007 Lawrenceville Press

18 Chapter 4 The BirthdayGame Flowchart
Refer to page 91 in the text. The BirthdayGame flowchart illustrates the application solution. Solution steps include: 1. displaying the directions for the player to calculate the number 2. prompting the player for the calculated number 3. subtracting 165 from the number 4. using integer division to divide the number by 100 and then storing the quotient as the birth month 5. using modulus division to divide the number by 100 and then storing the remainder as the birth day 6. displaying a message containing the player's birthday © 2007 Lawrenceville Press


Download ppt "Chapter 4 Assignment Statement"

Similar presentations


Ads by Google