Transcript lecture

Storing and manipulation Floating
point information
Recall (1)
• Although the computer stores binary numbers, we
can interpret then as decimal number through a conversion
operation:
Recall (2)
• The computer can combine adjacent bytes in the RAM
memory to form larger memory cells:
Variables (in general)
• A variable in a computer program is in fact a memory cell
• A variable in a computer program stores a number
• The tag represents the data type of the variable (cannot be
changed)
• You can only write one (binary) number on the piece of
paper.
• You can erase the number and write a new number on the
paper;
Floating point numbers
• A floating point number is a number where the number of
decimal digits before and after the decimal point is not
fixed (i.e., the decimal point "floats")
Floating Variable (1)
• A floating point variable behaves like a piece of paper where
you can record (exactly) one floating point number:
Floating Variable (2)
• encoding method used is called the IEEE 754 Standard
• Floating point numbers can also be written in the exponent
form:
Floating Variable (3)
• There are 2 kinds of floating point variables in Java
• A double precision floating point variable is a variable that
uses 8 consecutive bytes of memory as a single 64 bit
memory cell
• This kind of variable is commonly used in scientific
computations.
Defining Floating Point Variable (1)
• Every variable in Java must be defined, and you
must use the correct syntax construct to write a variable
definition
• The keyword double announces the variable definition
clause
• The NameOfVariable is an identifier which is the name of
the variable.
• The variable definition clause is must be ended with a semicolon ";"
Defining Floating Point Variable (2)
• The effect of the definition:
• The computer will find 8 consecutive bytes of RAM memory that
is unused
• The computer will then reserve these memory bytes
• It also associate the name x with the (8 bytes of) reserved memory
Update the value stored in a variable
(1)
• The assignment statement in the Java programming
language instructs the computer to update the value stored in
a variable
• The symbol "=" is called the assignment operator in Java
• The variable on left-hand-side of the "=" operator is
the receiving variable
• The expression on right-hand-side of the "=" operator is
the value that is assigned to the receiving variable
• This statement must be ended with a semi-colon ";"
Update the value stored in a variable
(2)
• The assignment statement x=0.31415e1 will store the
value 3.1415 into the memory cell identified by the name x:
Update the value stored in a variable
(3)
• System.out.println(x) will read the value stored at
the memory cell identified by the name x and print it to
the terminal:
Defining multiple variables of the
same type
• You can also define multiple variables of the same
type with one clauses by separating the different variable
names with a comma.
Floating point operators
• Floating point operators are arithmetic operators that
manipulate floating point values
Priority of the floating point
arithmetic operators
Java Library
Mathematical functions (1)
• Scientific calculations often involve Mathematical functions,
such as sin, cos, tan, log, and so on.
• Examples: sin and ln functions on a calculator
Mathematical functions (2)
• Programming languages allow you to embed Mathematical
functions within a arithmetic expression.
Class and Method
• Method = a collection of statements that performs a complex
(useful) task
• Class = a container for methods
Organization of the Java library (1)
• To find "things" more easily, humans usually organize (=
group) the "things" in a hierarchical manner
Organization of the Java library (2)
• Each class inside the Java library has a unique class path
name in the form:
java.PackageName.ClassName
Using classes in the Java library (1)
• One way is : use the class path
• we can refer to the sin() method stored in the Math class
(inside the package lang) as:
Using classes in the Java library (2)
• The other way is:
• first import the class (with an import clause)
Input parameters and output values
of a method (1)
• Consider a Mathematical function:
• A Mathematical function has a unique name
• A Mathematical function has a number of function
arguments (x, y and z). (Input)
• A Mathematical function will produce an answer. (Output)
Input parameters and output values
of a method (2)
• A method resembles a Mathematical function:
• A method has a unique "signature" (will learn more about
"signatures" later in the course)
• A method has a number of input parameters
• A method may return one output value (some methods do
not return any output values)
• you must store the return value in some variable (using
an assignment statement) or else, the returned value will
be lost !
How to invoke a method (1)
1. Syntax to invoke a method that does not return any value:
You can recognize such methods by the return type void.
How to invoke a method (2)
2. Syntax to invoke a method that returns a value:
You can recognize such methods by their return type which
is not equal to void.
Meaning of the method header:
• Parameters: You must pass the correct
number of parameter of the correct data type to a method
• Return: If the method returns a value, you must assign it to
a variable of the correct type
What happens when a method
is invoked (1)
• The following figure shows the situation just before the
program executes b = Math.sqrt(a);:
What happens when a method
is invoked (2)
• Pass the value of the variable a as parameter to
the sqrt method:
What happens when a method
is invoked (3)
• Execute the statements in the method body of sqrt:
What happens when a method
is invoked (4)
• When it completes, the execution returns to the location of
the method call
• The return value replaces the method call in the expression:
What happens when a method
is invoked (5)
• After the assigning the value to variable b, we have: