Chapter 3 Numerical data 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 3-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 1 Chapter 3 Numerical Data
Chapter 3 objectives After you have read and studied this chapter, you should be able to e Select proper types for numerical data e Write arithmetic expressions in Java Evaluate arithmetic expressions using the precedence rules e Describe how the memory allocation works for objects and primitive data values e Write mathematical expressions using methods in the Math class e Write programs that input and output data using the Input Box and OutputBox classes from the javabook package Apply the incremental development technique in writing programs (Optional) Describe how the integers and real numbers are represented in memory. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 2 Chapter 3 Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write arithmetic expressions in Java. Evaluate arithmetic expressions using the precedence rules. Describe how the memory allocation works for objects and primitive data values. Write mathematical expressions using methods in the Math class. Write programs that input and output data using the InputBox and OutputBox classes from the javabook package. Apply the incremental development technique in writing programs. (Optional) Describe how the integers and real numbers are represented in memory
Manipulating Numbers r In Java, to add two numbers x and y, we write Y r But before the actual addition of the two numbers takes place, we must declare their data type. If X and y are integers, we write int x, yi or int xi int yi C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 3-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 3 Manipulating Numbers In Java, to add two numbers x and y, we write x + y But before the actual addition of the two numbers takes place, we must declare their data type. If x and y are integers, we write int x, y; or int x; int y;
Variables r When the declaration is made, memory space is llocated to store the values of x and y x and y are called variables. a variable has three properties: aA memory location to store the value a The type of data stored in the memory location, and a The name used to refer to the memory location. r Sample variable declarations: int xi int v, W, y C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 4 Variables When the declaration is made, memory space is allocated to store the values of x and y. x and y are called variables. A variable has three properties: A memory location to store the value, The type of data stored in the memory location, and The name used to refer to the memory location. Sample variable declarations: int x; int v, w, y;
Numerical data Types r There are six numerical data types: byte, short, int, long float, and double r Sample variable declarations int i,j, ki float number one, numberTwoi long big integer double bigNumber r At the time a variable is declared it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count =10, height =34 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 3-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 5 Numerical Data Types There are six numerical data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne, numberTwo; long bigInteger; double bigNumber; At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count = 10, height = 34;
Data Type Precisions The six data types differ in the precision of values they can store in memory Data Default Minimum Maximum Type ContentValuet Value Value byte Integer 127 short Integer 0000 -32768 32767 ant Integer 2147483648 2147483647 Integer 9223372036854775808 9223372036854775807 float Real 0.0 340282347E+38tt 3.40282347E+38 doubl Real 0.0 179769313486231570E+308179769313486231570E+308 f. No default value is assigned to a local variable. A local variable is explained on page 162 tt. The character E indicates a number is expressed in scientific notation. This notation is explained on page 99 C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 6 Data Type Precisions The six data types differ in the precision of values they can store in memory
Assignment statements r We assign a value to a variable using an assignment statements. r The syntax is = i r Examples: sum firstNumber secondnumber avg =(one two three)/3.0 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 3-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 7 Assignment Statements We assign a value to a variable using an assignment statements. The syntax is = ; Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0;
Primitive data Declaration and assignments int firstNumber, secondNumberi firstNumber 234 secondNumber =87: A. Variables are allocated in memory A firstNumber 234 int firstNumber, secondNumberi secondNumber 87 firstNumber 234 secondNumber =87; B B. Values are assigned Code State of memo C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 8 Primitive Data Declaration and Assignments Code State of Memory int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; A B int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; firstNumber secondNumber A. Variables are allocated in memory. B. Values are assigned to variables. 234 87
Assigning Numerical Data int number; number 237 number =35/ number 35 A. The variable is allocated in int numberi A memory. number =237 B B. The value 237 number =35: is assigned to number C. The value 35 overwrites the previous value 237. Code State of Memory C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 3-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 9 Assigning Numerical Data Code State of Memory int number; number = 237; number = 35; number A. The variable is allocated in memory. B. The value 237 is assigned to number. 237 int number; number = 237; number = 35; A B number = 35; C C. The value 35 overwrites the previous value 237. 35
Assigning Objects Customer customer customer customer new Customer()i customer new Customer( )i C Cus A B Customer customer A. The variable is allocated in memory customer new Customer()i B. The reference to the cus tomer new Customer()i new object is assigned to customer. C) C. The refer another object overwrites the reference in customer. Code State of memo C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 10 Assigning Objects Code State of Memory Customer customer; customer = new Customer( ); customer = new Customer( ); customer A. The variable is allocated in memory. Customer customer; customer = new Customer( ); customer = new Customer( ); A B C customer = new Customer( ); B. The reference to the new object is assigned to customer. Customer C. The reference to another object overwrites the reference in customer. Customer