CSC110

advertisement
CSC110
Chapter 5 Notes
Numeric Processing
Text Objectives:
To explore the similarities among and differences between
primitive types and reference types.
To examine integer data types, including int, short, byte, and
long
To examine real data types, including double and float
To examine the order in which numeric expressions are
evaluated at execution time
To explore mixed-type numeric expressions and the use of
casting
To introduce the Math library class and some of its more
useful methods and constants
To demonstrate how to create numeric constants and
nonvoid primitive methods in Java
To examine a few common numeric expression/instruction
patterns
“Numbers have always played a key role in computer
programming.”
5.1 Primitive Types
Reference type variables belong to classes. A variable of
this type refers (is bound) to its object.
There are eight java types that are too simple to require a
class. These are called primitive types.
They are atomic – no attributes (instance variables)
They are built into the Java language (no external class
required)
Primitive variable represents a value rather than an object
binding.
boolean
byte
char
double
float
int
long
short
Can be used to specify the types of instance variable, local
variable, parameters, and methods.
5.2 Primitive Integer Types
The int data type can store an integer from –2,147,483,648
to 2,147,483,647. (What can you do in 32 bits?)
int can be used to declare the type of an instance variable, a
local variable, a parameter, or the return type of a method.
Figure 5.2
prefix operators
such as autoincrement and autodecrement
infix operators
infix relational operators
postfix operators
A constant or literal signed or unsigned integer without
commas.
73
-35
Note the – in -35 is a prefix operator whereas the + in
73 + 35 is an infix operator.
Integer expressions
17 + 3 / 2
17 % 3 is the remainder when 17 is divided by 3
(evaluates to 2)
used in assignment statements
int a, b;
a = 6;
b = (a + 1) * 45;
Note that there is a precedence for int operators.
highest
-postfix
++
-- and ++ prefix
/, *,%
+, -
second
mid
lowest
byte
short
int
long
8 bits
16 bits
32 bits
64 bits
left to right
right to left
left to right
left to right
– 128 to 127
-32,768 to 32,767
5.3 Differences between Primitives and References
You do not use the new operation with a primitive type.
int a;
int b = 8;
//creates a cell
// usually initialize the value of a variable.
Primitive variable within an expression evaluates to it cell
contents or value.
a = 3;
int c = a + b;
// c now has the value of 11
Parameter passage of primitive data is done by pass by
value. The formal parameter receives a copy the value of its
corresponding argument when the method is called.
Every reference variable is also associated with its own cell
of memory.
The cell of the reference variable stores the address of
the object that is bound to the variable.
In reference parameter passage a copy of the arguments
value (an object address) is assigned to the formal
parameter.
5.4 Real Numbers (Float and Double Types)
Real numbers are numbers with fractional parts. These are
expressed with a decimal point.
Examples are 3.2, 6.555, and 3.0
Can also use the Eformat such as 3.92E+5
A float is 32 bits and a double occupies 64 bits.
A double is a real number with 15 significant digits of
accuracy.
(Significant digits are all digits excluding any leading or
trailing zeros.)
Double operators are basically the same as int with the
exception of %, the mod operator
5.5 System.Out.Println Revisited
A System.out.println call accepts any data type for an
argument, and executing System.out.println displays
information regarding the state of its argument.
e.g. System.out.println( a + b + c);
System.out.println( 25 + 6);
public class cubetest {
public static int cubed(int x)
{
return x * x * x;
}
public static void main (String argv[])
{
int y = cubed(3);
System.out.println("The value of y cubed is ");
System.out.println(y);
}
}
5.6 Mixed-Type Numeric Expressions
A mixed-type numeric expression is any expression that
invokes two or more different numeric data types.
Heart of Java’s handling of mixed-type expressions is
widening:
byte  short  int  long  float  double

char
A wider type variable can represent all the values from a
narrower type.
Narrower to wider is called a safe conversion.
e.g. a float can be passed to a double.
7.0 / 2 evaluates to 7.0 / 2.0
Narrowing is not permitted.
Java provides a mechanism so you can force unsafe
conversions to occur.
You cast the variable (not all casts are allowed)
Syntax: (type) expression
(int) 24.8 truncates the real number to an integer
intVar = (int) (25.2 + 16.9);
//evaluates to 41
5.7 Primitive Methods (Including Math)
Many methods return a primitive type
methods to return attributes of aLibrary object
Using the example of AOval, the getter methods return the
attributes of the class. (Calls them querys.)
public int getX()
{
return x;
{
methods to perform common math operations
Methods from the Math class – page 145
e.g.
Math.pow( double b, double e)
// raises b to the power e
double Math.random()
// retruns a random value between 0 and 1.
methods you create to return primitive values
private double average( double d1, double d2)
{
return (d1 + d2) / 2.0;
}
5.8 Constants (Final)
A constant is used for a value that does not change.
Syntax is final type variable name = expression;
e.g. final double perfect = 6.0;
A constant can have scope of the entire class (in the private
area) or as a local variable within a method.
There are also common constant defined within the Math
library such as Math.PI
5.9 Numeric Expression Patterns
Expresion pattern for double division with int expression
(double) integerExpression / integer expression
Assignment pattern for assigning double to int
intVar = (int) (doubleExpression);
Expression pattern for rounding
intVar = (int) Math.round(doubleExpression);
Two expression patterns for incrementing a variable by 1
integerVar = IntegerVar + 1;
integerVar++;
Assignment pattern for incrementing a variable by 1 within
the range 0 through n
integerVar = (integerVar + 1) % (n + 1);
Expression pattern for generating a random integer from low
through high
(int)(Math.random() * (high – low + 1) + low)
5.10 Design Example – Dynamic Histogram
Download