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;