Download presentation
Presentation is loading. Please wait.
Published byGiles Walker Modified over 9 years ago
1
Using Java's Math & Scanner class
2
Java's Mathematical functions (methods) (1)
3
Java's Mathematical functions (methods) (2) Unlike a calculator that always shows the result of a computation on its display.... A computer will only show the result of a computation when it is told !!!
4
Java's Mathematical functions (methods) (3) A computed value is not printed: If you want to print a computed value, use a print statement A computed value is not stored: If you want to store (save) a computed value, use an assignment statement
5
Reading input from the console input (1) The console is the terminal window that is running the Java program. When a Java program starts running, the Java runtime system will initialize many variables in support for the running program. Java system variable: System.in represents the console input A Java program can obtains inputs from the console through the keyboard
6
Reading input from the console input (2) Java's Scanner library class The class path of the Scanner class inside the Java library is: java.util.Scanner Before a Java program can read input from the keyboard, the program must " construct a Scanner object (explain later) Example:
7
Reading input from the console input (3) After having constructed the Scanner object named in, you can use the following expression to read a floating point number from the keyboard: You must save (store) the number read in by "in.nextDouble()" in a double typed variable with an assignment statement
8
Reading input from the console input (4) the method call in.nextDouble() will invoke (run) a method in Java's library. The task performed by in.nextDouble() is to read a floating point number from the keyboard:
9
Reading input from the console input (5) The return value will replace the method call: The input value 3.5 is then stored in the variable a !!!
10
Reading input from the console input (6) steps to read in a floating point number
11
Reading input from the console input (7) Good programming courtesy: When the program needs the user to enter input from the keyboard, it must print out a (short) prompt message
12
Different Types of Numerical Information
13
Recall Recall that the computer can combine adjacent bytes in the RAM memory to form larger memory cells To obtain a higher accuracy (= more significant digit of accuracy), we need to combine more memory cells
14
Trade-offs Memory usage Speed Arithmetic expressions that uses higher accuracy will take longer time to compute When a Java program needs to use higher accurate numbers, it will not only use more memory, but it will also take a longer time to complete.
15
Different kinds of floating point numbers in Java Java provides 2 different sizes of floating point numbers (and variables): 1. Single precision floating numbers (has lower accuracy and uses less memory) uses 4 consecutive bytes of memory as a single 32 bit memory cell 2. Double precision floating numbers (has more accuracy and uses more memory) uses 8 consecutive bytes of memory as a single 64 bit memory cell
16
Defining single and double precision floating point variables define double precision floating point variables: define single precision floating point variables: float and double are considered as different types
17
Converting (casting) to a single or a double precision representation single and double precision floating point numbers uses different encoding methods The computer has built-in machine instructions to convert between different encodings
18
Very important phenomenon in computer programming: lost of accuracy
19
Safe and unsafe conversion operations Safe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is no (or very little) loss in accuracy Unsafe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is significant loss in accuracy
20
Expressions containing values of different types A computer can only operate on data of the same data type In order to perform any operation on two values of differing types, the computer must: 1. convert one of the types into the other type 2. Perform the operation on the value
21
Automatic conversions: 1.During a calculation of a arithmetic expressions Arithmetic promotion of float to double: 2. Storing the result to a variable by an assignment operator If float value is assigned to a double variable, the float value is converted to double.
22
The general rule for automatic type conversion in the assignment operation If type1 is a higher accuracy type than type2, then: The type2 value is automatically converted to type1 before the assignment statement is executed. If type1 is a lower accuracy type than type2, then: The assignment statement is not allowed. You must use an casting operator to make the assignment statement valid.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.