? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 1
Chapter 3
Numerical Data
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.
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;
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;
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 beinitialized,For example,we may initialize the integer
variables count and height to 10 and 34 as
int count = 10,height = 34;
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.
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
<variable> = <expression> ;
Examples:
sum = firstNumber + secondNumber;
avg = (one + two + three) / 3.0;
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
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
C
C,The value 35
overwrites the
previous value 237.
35
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
B,The reference to the
new object is assigned
to customer.
Customer
C,The reference to
another object overwrites
the reference in customer.
Customer
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 11
Having Two References to a Single Object
Code State of Memory
Customer clemens,twain;
clemens = new Customer( );
twain = clemens;
Customer clemens,twain,
clemens = new Customer( );
twain = clemens;
A
ain; B
C
A,Variables are
allocated in memory.
clemens
twain
B,The reference to the
new object is assigned
to clemens.
Customer
C,The reference in
clemens is assigned to
customer.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 12
Arithmetic Operators
The following table summarizes the arithmetic
operators available in Java.
This is an integer division
where the fractional part
is truncated.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 13
Arithmetic Expression
How does the expression
x + 3 * y
get evaluated? Answer,x is added to 3*y.
We determine the order of evaluation by following the precedence rules,
A higher precedence operator is evaluated before the lower one,If two operators are the same precedence,
then they are evaluated left to right for most
operators.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 14
Precedence Rules
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 15
Type Casting
If x is a float and y is an int,what will be the data
type of the following expression?
x * y
The answer is float,
The above expression is called a mixed expression,
The data types of the operands in mixed expressions
are converted based on the promotion rules,The promotion rules ensure that the data type of the
expression will be the same as the data type of an
operand whose type has the highest precision,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 16
Explicit Type Casting
Instead of relying on the promotion rules,we can
make an explicit type cast by prefixing the operand
with the data type using the following syntax:
( <data type> ) <expression>
Example
(float) x / 3
(int) (x / y * 3.0)
Type case x to float and
then divide it by 3.
Type cast the result of the
expression x / y * 3.0 to
int.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 17
Implicit Type Casting
Consider the following expression:
double x = 3 + 5;
The result of 3 + 5 is of type int,However,since the
variable x is double,the value 8 (type int) is
promoted to 8.0 (type double) before being assigned
to x.
Notice that it is a promotion,Demotion is not allowed.
int x = 3.5;
A higher precision value
cannot be assigned to a
lower precision variable.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 18
Constants
We can change the value of a variable,If we want the
value to remain the same,we use a constant.
final double PI = 3.14159;final int MONTH_IN_YEAR = 12;
final short FARADAY_CONSTANT = 23060;
These are constants,
also called named
constant.
The reserved word
final is used to
declare constants.
These are called
literal constant.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 19
The Math Class
The Math class in the java.lang package includes many common and useful mathematical functions
such sin,cos,tan,square root,exponentiation,and others.
The mathematical formula
is expressed in Java as
Math.abs( Math.sin( Math.PI / 4.0) * x )
See Table 3.5 page 101
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 20
InputBox
The InputBox class is used to get input values from
the user.
InputBox inputBox;
inputBox = new InputBox( mainWindow );
inputBox.getInteger( );
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 21
InputBox—Error Message
If an invalid value is entered,an error message is
displayed.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 22
InputBox—Custom Message
A programmer-designated prompt is possible.
inputBox.getInteger(―Enter your age:‖);
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 23
InputBox Methods
See the complete documentation for more details.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 24
OutputBox
The OutputBox class is used to display text,
OutputBox outputBox;
outputBox = new OutputBox( mainWindow );
outputBox.print(―Hello,Dr,Caffeine‖ );
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 25
OutputBox—printLine
Unlike MessageBox,OutputBox is capable of
displaying multiple lines of text.
outputBox.printLine(―one‖ );
outputBox.printLine(―two‖ );
outputBox.printLine(―three‖ );
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 26
OutputBox—print
Unlike MessageBox,OutputBox is capable of
displaying multiple lines of text.
outputBox.print(―one ‖ );
outputBox.print(―two ‖ );
outputBox.print(―three‖ );
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 27
OutputBox Methods
See the complete documentation for more details.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 28
Sample Program,Loan Calculator
Problem Statement
Write a loan calculator program that computes both
monthly and total payments for a given loan amount,
annual interest rate,and loan period.
Major Tasks
1,Get three input values
2,Compute the monthly and total payments
3,Output the results
Key Formula
L – loan amount
R – monthly interest rate
N – number of payments
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 29
Loan Calculator – Design
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 30
Loan Calculator – Object Diagram
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 3 - 31
Loan Calculator – Development Steps
1,Start with a program skeleton.
2,Add code to accept three input values.
3,Add code to output the results.
4,Add code to compute the monthly and total
payments.