Week 4 - Numerical - DataAndExpression-part2
Week 4 - Numerical - DataAndExpression-part2
COMPUTATION &
EXPRESSION
(part 2)
Objectives
• To learn about data conversions
• To learn complex arithmetic
computation
• To learn about accepting input
from the user
Outline
Data Conversion
• Sometimes it is convenient to convert data
from one type to another.
• For example, in a particular situation we
may want to treat an integer as a floating
point value.
• These conversions do not change the type
of a variable or the value that's stored in it
– they only convert a value as part of a
computation.
Data Conversion
Two types of data conversion:
• Widening conversions are the
safest because they tend to go
from a small data type to a larger
one (such as a short to an int)
• Narrowing conversions can lose
information because they tend to
go from a large data type to a
smaller one (such as an int to a
short)
Data Conversion
Data Conversion
• In Java, data conversions can occur
in three ways:
• Assignment conversion
• Promotion
• Casting
Assignment Conversion
• Assignment conversion occurs when a
value of one type is assigned to a variable
of another.
• Example:
int dollars = 20;
double money = dollars;
• Only widening conversions can happen
via assignment.
• Note that the value or type of dollars
did not change.
Promotion
• Promotion happens automatically when
operators in expressions convert their
operands.
• Examples:
int count = 12;
double sum = 490.27;
result = sum / count;
• The value of count is converted to a
floating point value to perform the division
calculation.
Casting
• Casting is the most powerful, and
dangerous, technique for
conversion.
• Both widening and narrowing
conversions can be accomplished
by explicitly casting a value.
• To cast, the type is put in
parentheses in front of the value
being converted.
int total = 50;
float result = (float)
total / 6;
• Without the cast, the fractional part
of the answer would be lost.
Outline
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)
Mathematical Methods in
Java
Analyzing an Expression
Outline
Interactive Program
Demo Interactive Program
in JGRASP
Classes in JAVA
How to Create Interactive
Programs?
• Import Scanner class:
import java.util.Scanner;
• Create Scanner object:
Scanner scan = new Scanner
(System.in);
Scanner read = new Scanner
(System.in);
• Once created, the Scanner object can
be used to invoke various input
methods, such as:
answer = scan. nextLine();
answer = read.nextLine();
Interactive Programs
• Programs generally need input on
which to operate.
• The Scanner class provides
convenient methods for reading
input values of various types.
• A Scanner object can be set up to
read input from various sources,
including the user typing values on
the keyboard.
• Keyboard input is represented by
the System.in object.
Reading Input
• The following line creates a Scanner
object that reads from the keyboard:
Scanner scan = new Scanner
(System.in);
• The new operator creates the Scanner
object
• Once created, the Scanner object can be
used to invoke various input methods,
such as:
answer = scan.nextLine();
Reading Input
• 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
Design of Interactive
Program: Pseudocode &
Flow Chart
Start
Input miles, gallons
Calculate mpg = miles / gallons
Print “Miles per gallon” +mpg
End
Sample Code
Sample Code (Cont…)
nextLine() and next()
When Input Data Type is
a Combination of String
with int/double…
• Data conversions
- Widening & narrowing conversions
- Assignment, promotion, casting