Topic 3 - Numerical - DataAndExpression-part2
Topic 3 - Numerical - DataAndExpression-part2
NUMERICAL
COMPUTATIO
N&
EXPRESSION
(PART 2)
1
1. To learn about
data conversions
2. To learn complex
OBJECTIV arithmetic
ES computation
3. To learn about
accepting input
from the user
2
OUTLINE
Data Conversion
Interactive Programs
3
DATA CONVERSION
4
DATA CONVERSION
6
DATA CONVERSION
7
Assignment conversion
occurs when a value of one
type is assigned to a
ASSIGN variable of another.
MENT
Example:
8
Promotion happens
automatically when operators in
expressions convert their
operands.
Examples:
TION
result = sum /
count;
9
Casting is the most powerful, and
dangerous, technique for
conversion.
Both widening and narrowing
conversions can be accomplished
by explicitly casting a value.
CASTIN To cast, the type is put in
parentheses in front of the value
G being converted.
10
OUTLINE
Data Conversion
Interactive Programs
11
THE math CLASS
Math class: contains methods like sqrt
and pow
To compute xn, you write Math.pow(x,
n)
However, to compute x2 it is significantly
more efficient simply to compute x * x
To take the square root of a number, use
the Math.sqrt; for example,
Math.sqrt(x)
12
THE math CLASS
In Java,
can be represented as
13
MATHEMATICAL METHODS
IN JAVA
14
ANALYZING AN
EXPRESSION
15
OUTLINE
Data Conversion
Interactive Programs
16
INTERACTIVE PROGRAM
CUST A
RM 10.50 +
RM 12.00 =
RM22.50
CUST B
RM 10.00 +
RM 12.00 +
RM 30.00 =
RM 52.00
17
DEMO
INTERACTIVE
PROGRAM IN
JGRASP
18
CLASSES IN JAVA
String
Tokenizer
Scanner Class
…
Import Class Class
_______
_______ _______
_______ _______
CalculateRect
JAVA.util class library
Class
_______ CalculatePric
_______ e Class
_______
_______
READING INPUT
22
The Scanner class is part of the
java.util class library, and must be
imported into a program to be used
The nextLine method reads all of the
input until the end of the line is found
See Echo.java
READING INPUT
23
METHODS FOR
SCANNER
Methods Descriptions
nextByte() Reads an integer of the byte type
nextShort() Reads an integer of the short type
nextInt() Reads an integer of the integer type
nextLong() Reads an integer of the long type
nextFloat() Reads a number of the float type
nextDouble() Reads a number of the double type
next() Reads a string that ends before whitespace
character
nextLine() Reads a line of text
24
DESIGN OF INTERACTIVE PROGRAM:
PSEUDOCODE & FLOW CHART
Start
Start
Input message
Input message
Print "You entered: \"" + message +
"\“” Print "You entered: \""
+ message + "\""
End
End
25
//********************************************************************
// Echo.java Author: Lewis/Loftus
//
// Demonstrates the use of the nextLine method of the Scanner class
// to read a string from the user.
//********************************************************************
import java.util.Scanner;
message = scan.nextLine();
27
DESIGN OF INTERACTIVE
PROGRAM: PSEUDOCODE &
FLOW CHART
Start
Start
Input miles,
Input miles, gallons gallons
End
28
//********************************************************************
// GasMileage.java Author: Lewis/Loftus
//
// Demonstrates the use of the Scanner class to read numeric data.
//********************************************************************
import java.util.Scanner;
continue
29
continue
Sample Run
Enter the number of miles: 328
Enter the gallons of fuel used: 11.2
Miles Per Gallon: 29.28571428571429
30
TODAY’S TAKE
AWAY
Data conversions
- Widening & narrowing
conversions
- Assignment, promotion, casting
Complex arithmetic computation
- Math class
Accepting input from the user
- Importing Scanner class
- Creating Scanner object
- Reading input from keyboard
using Scanner methods,
e.g.: nextInt(), nextLine().
31