Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

22
Using Java's Math & Scanner class

Transcript of Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Page 1: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Using Java's Math & Scanner class

Page 2: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Java's Mathematical functions (methods) (1)

Page 3: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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 !!!

Page 4: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 5: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 6: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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:

Page 7: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 8: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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:

Page 9: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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 !!!

Page 10: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Reading input from the console input (6)

• steps to read in a floating point number

Page 11: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 12: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Different Types of

Numerical Information

Page 13: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 14: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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.

Page 15: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 16: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 17: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 18: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Very important phenomenon in computer programming: lost of accuracy

Page 19: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 20: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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

Page 21: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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.

Page 22: Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

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.