220 likes | 285 Views
Learn to handle user input, convert data types, store data and perform arithmetic operations efficiently in Java. This chapter covers using BufferedReader, numeric expressions, and error-free constructions in Java.
E N D
Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2
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
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
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
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
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
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);
Assignment Statements • General syntax: location = value
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
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
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
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
Compute the Body Mass Index • Now that we have the data we can compute the body mass index:
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
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
Rest of Today • Complete, compile and run the console version of the program. • Use the test data: • 5’7 man • 145 pounds • Should have a body mass index of 23 • Show me the results when complete