Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.

Slides:



Advertisements
Similar presentations
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Advertisements

JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The computer memory and the binary number system.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
How to Create a Java program CS115 Fall George Koutsogiannakis.
CMT Programming Software Applications
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Hello, world! Dissect HelloWorld.java Compile it Run it.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Using the Java programming language compiler. Review of relevant material from previous lectures From previous lectures: A computer can only execute machine.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
The computer memory and the binary number system.
Shorthand operators.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
The character data type char
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Mixing integer and floating point numbers in an arithmetic operation.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables, Expressions, and IO
IDENTIFIERS CSC 111.
The Boolean (logical) data type boolean
Java Intro.
The computer memory and the binary number system
Unit 3: Variables in Java
The for-statement.
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
The computer memory and the binary number system
Presentation transcript:

Floating point numerical information

Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number (See: Syllabus/01/binary.html)

Previously discussed (cont.) Although the computer stores binary numbers, we can interpret then as decimal number through a conversion operation:

Previously discussed (cont.) The computer can combine adjacent bytes in the RAM memory to form larger memory cells

Previously discussed (cont.) Although a computer memory can only store numbers, we can use an encoding scheme to represent any kind of information (E.g.: marital status information: 0 = single, 1 = married, and so on... gender information: 0 = male, 1 = female) We must have context information to interpret a number that is stored in the RAM memory (E.g., we cannot interpret the number 0 stored in RAM memory unless we know kind of information it is.)

Storing information inside a computer A Java computer program uses variables to store information There are different kinds (types) of variables In this webnote, we will study the floating point variables

Variables (in general) Variable in Java A variable in a computer program is in fact a memory cell A memory cell can store a number A variable in a computer program stores a number

Variables (in general) (cont.) Each variable in Java has a data type The data type allows the computer program to use the appropriate encoding (interpretation) scheme to interpret the number stored in the variable

Analogy of a variable A variable (in Computer Science) is like a piece of paper with a tag attached to it. The tag represents the data type of the variable (the value of the tag 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; but you can never write more than one number on the paper at any time.

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., it "floats") Examples: decimal digits after the decimal point decimal digits after the decimal point decimal digits after the decimal point

Floating point numbers (cont.) Floating point numbers can also be written in the exponent form: e1 (= ×10 1 = ) e-1 (= ×10 -1 = )

Floating point variables A double precision floating point variable is a variable that: uses 8 consecutive bytes of memory as a single 64 bit memory cell uses a modified binary number system as encoding scheme For more details on the floating point encoding method, please take CS255

Floating point variables (cont.) A double precision floating point variable can represent a floating point number: in range of from − to and with about 15 decimal digits accuracy This kind of variable is commonly used in scientific computations. A calculator uses double precision floating point variables. (I used a double precision floating point variable to avoid using the casting operation - casting will be explained later)

Floating point variables (cont.) Important fact: A floating point variable behaves like a piece of paper where you can record (exactly) one floating point number

Floating point variables (cont.) BTW, that's how you perceive a double typed variable. Inside the computer, the number is represented with bits. It looks something like this: The encoding method used is called the IEEE 754 Standard - See: point_format

Defining floating point variables Every variable in Java must be defined This rule applies to a floating point variable. Also, you must use the correct syntax construct to write a variable definition

Defining floating point variables (cont.) Syntax to define an floating point variable: Notes: double NameOfVariable ; 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 semi-colon ";"

Defining floating point variables (cont.) Example: public class Var01 { public static void main(String[] args) { double x; // Define floating point variable with name "x" System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x“ }

Defining floating point variables (cont.) Notes: The name of the Java program is Var01 Therefore, the UNIX filename that contain this Java program must be: Var01.java The statement "System.out.println(x);" will print the content of the variable x to the terminal Notice that the statement "System.out.println(x);" does not uses quotes ".." around the variable x The statement "System.out.println("x");" (with quotes ".." around x) will print the text x

Effect of a variable definition clause When a Java program starts to run inside the computer, it will use up a portion of the RAM memory to store various things A (large) portion of the RAM memory will remain unused

Effect of a variable definition clause (cont.) The effect of the definition: is the following: double x; The computer will find 8 consecutive bytes of RAM memory that is unused The computer will then reserve these memory bytes

Effect of a variable definition clause (cont.) It also associate the name x with the (8 bytes of) reserved memory Whenever the Java program uses the name x, the computer will translate that into read/write operations to these (8 bytes of) reserved memory

Effect of a variable definition clause (cont.) The computer can store (exactly) one floating point number in the reserved memory bytes. That is why: A floating point variable behaves like a piece of paper where you write down (exactly) one floating point number

Effect of a variable definition clause (cont.) When the Java program is run, it starts with the main() method: Example

Effect of a variable definition clause (cont.) A portion of the RAM memory is used. But a large portion will be unused

Effect of a variable definition clause (cont.) When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:

Effect of a variable definition clause (cont.) These 8 bytes of reserved memory can now be referred to by the name x !!! (How this is done exactly will be explained in CS255)

Update the value stored in a variable Assignment statement: Syntax of an assignment statement: The assignment statement in the Java programming language instructs the computer to update the value stored in a variable VariableName = Value ;

Update the value stored in a variable (cont.) Notes: 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 The assignment statement must be ended with a semi- colon ";"

Update the value stored in a variable (cont.) Meaning of the assignment statement: The expression on the RHS of the "=" operator is first computed The computed value is then assigned to the receiving variable

Update the value stored in a variable (cont.) Example: public class Var02 { public static void main(String[] args) { double x; // Define floating point variable with name "x" x = e1; // Assign to x System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" }

Update the value stored in a variable (cont.) Notes: The name of the Java program is now Var02 Therefore, the UNIX filename that contain this Java program must be: Var02.java (I will not make this comment anymore from this point on...)

Update the value stored in a variable (cont.) Example: When the variable definition "double x" is processed, it causes the computer to reserve consecutive 8 bytes of RAM memory:

Update the value stored in a variable (cont.) These 8 bytes of reserved memory can now be referred to by the name x !!!

Update the value stored in a variable (cont.) The assignment statement x= e1 will store the value into the memory cell identified by the name x:

Update the value stored in a variable (cont.) The Print statement System.out.println(x) will read the value stored at the memory cell identified by the name x and print it to the terminal:

Update the value stored in a variable (cont.) Example Program: (Demo above code) –Prog file: ar02.java How to run the program: Right click on link and save in a scratch directory To compile: javac Var02.java To run: java Var02

Update the value stored in a variable (cont.) Hello Class The variable x contains this number: Output:

Defining and initializing a variable at the same time When a variable is defined, it does not contain a legitimate value You can give an initial value to a variable when you define the variable Syntax to define an initialized double precision floating point variable: double varName = initialValue ;

Defining and initializing a variable at the same time (cont.) Example: public class Var03 { public static void main(String[] args) { double x = e1; // Define an initialized variable System.out.println("Hello Class"); System.out.println(" The variable x contains this number:"); System.out.println(x); // Print variable "x" }

Defining multiple variables of the same type You can define multiple variables of the same type with multiple clauses

Defining multiple variables of the same type (cont.) Example: defining 3 variables public class Var04 { public static void main(String[] args) { double x = e1; // <----- Focus here double y; double z = ; y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" }

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. Example: defines that same 3 variables with 1 clause Notice the comma (,) separating the variable names. double x = e1, y, z = ;

Defining multiple variables of the same type (cont.) Example: defines that same 3 variables with 1 clause public class Var04 { public static void main(String[] args) { double x = e1, y, z = ; // <----- Focus here y = 1.0; System.out.println(x); // Print variable "x" System.out.println(y); // Print variable "y" System.out.println(z); // Print variable "z" } Notice the comma (,) separating the variable names.