java programming, 3e concepts and techniques chapter 3 section 63 – manipulating data using...
TRANSCRIPT
Java Programming, 3eConcepts and Techniques
Chapter 3
Section 63 –
Manipulating Data Using Methods – Day 2
2Chapter 3: Manipulating Data Using Methods
Chapter Objectives
• Use the System class to create data streams
• Instantiate the BufferedReader class in code
• Use the readLine() method to handle user input
• Convert strings to numbers using the parse() method
3Chapter 3: Manipulating Data Using Methods
Chapter Objectives
• Use assignment statements to store data with proper identifiers
• Use operators and parentheses correctly in numeric and conditional expressions
• Round an answer using the round() method of the Math class
4Chapter 3: Manipulating Data Using Methods
User Input – Streams and the System Class• The act of data flowing in and out of a program
is called a stream• The System class creates three streams when a
program executes
5Chapter 3: Manipulating Data Using Methods
User Input – Streams and the System Class• Data from input streams are first sent to a buffer • The java.io package contains several stream
classes– InputStreamReader
• Decodes the bytes from the System.in buffer into characters
– BufferedReader• Increases efficiency by temporarily storing the input received
from another class, such as InputStreamReader• Aids in platform independence by simplifying the process of
reading text and numbers from various input sources
6Chapter 3: Manipulating Data Using Methods
Using the BufferedReader class
• Call the BufferedReader constructor to instantiate a BufferedReader object
• The argument of the BufferedReader() method instantiates an InputStreamReader
• BufferedReader() returns a reference to the input data from System.in
Instantiate: to represent (an abstraction) by a concrete instance
8Chapter 3: Manipulating Data Using Methods
User Prompts, Inputs, and Conversions• The readLine() method reads a line of input text
and returns a String containing the line • The returned String must be explicitly converted
if the data is to be used as another data type • Each primitive data type has a wrapper class
allowing the primitive to be treated as an object• The wrapper classes provides a parse() method
to convert Strings to primitives, and vice versa– Example: height = dataIn.readLine(); inches = Integer.parseInt(height);
10Chapter 3: Manipulating Data Using Methods
Assignment Statements
• General syntax: location = value
12Chapter 3: Manipulating Data Using Methods
Arithmetic Operators
• The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression
• Addition, subtraction, multiplication, and division can manipulate any numeric data type
• When Java performs math on mixed data types, the result is always the larger data type
• Casts allow programmers to force a conversion from one primitive type to another
13Chapter 3: Manipulating Data Using Methods
Numeric Expressions
• Numeric expressions evaluate to a number• Only numeric primitive data types may be used in a
numeric expression• A value and variable must be separated by an arithmetic
operator• Unless parentheses supercede, an expression is
evaluated left to right with the following rules of precedence:– Multiplication and/or division– Integer division– Modular division– Addition and/or subtraction
14Chapter 3: Manipulating Data Using Methods
Parentheses in Expressions
• Parentheses may be used to change the order of operations– The part of the expression within the parentheses is
evaluated first
• Parentheses can provide clarity in complex expressions– Numeric and conditional expressions should be
grouped with parentheses
• Parentheses can be nested– Java evaluates the innermost expression first and
then moves on to the outermost expression
15Chapter 3: Manipulating Data Using Methods
Construction of Error-Free Expressions• Java may not be able to evaluate a validly
formed expression due to the following logic errors:– Dividing by zero– Taking the square root of a negative value– Raising a negative value to a non-integer value– Using a value too great or too small for a given data
type– Comparing different data types in a conditional
expression
17Chapter 3: Manipulating Data Using Methods
Compute the Body Mass Index
• Now that we have the data we can compute the body mass index:
20Chapter 3: Manipulating Data Using Methods
Compiling, Running, and Documenting the Application• Compile the Body Mass Index Calculator
program
• Execute the program
• Test the program by entering the sample input data supplied in the requirements phase at the prompts
• Verify the results
21Chapter 3: Manipulating Data Using Methods
Summary
• Data input needs be buffered and interpreted before it can be used.– Use InputStreamReader and
BufferedReader to get data into your program.
– Use the parse method to convert it to a “number”
• Please Excuse My Dear Aunt Sally rules apply in Java