Download presentation

Presentation is loading. Please wait.

Published byZoey Dike Modified about 1 year ago

1
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2

2
Chapter 3: Manipulating Data Using Methods 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

3
Chapter 3: Manipulating Data Using Methods 3 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

4
Chapter 3: Manipulating Data Using Methods 4 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

5
Chapter 3: Manipulating Data Using Methods 5 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

6
Chapter 3: Manipulating Data Using Methods 6 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

7
Chapter 3: Manipulating Data Using Methods 7

8
8 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);

9
Chapter 3: Manipulating Data Using Methods 9 Getting Input for the Program

10
Chapter 3: Manipulating Data Using Methods 10 Assignment Statements General syntax: location = value

11
Chapter 3: Manipulating Data Using Methods 11 Arithmetic Operators

12
Chapter 3: Manipulating Data Using Methods 12 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

13
Chapter 3: Manipulating Data Using Methods 13 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

14
Chapter 3: Manipulating Data Using Methods 14 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

15
Chapter 3: Manipulating Data Using Methods 15 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

16
Chapter 3: Manipulating Data Using Methods 16 The Math Class

17
Chapter 3: Manipulating Data Using Methods 17 Compute the Body Mass Index Now that we have the data we can compute the body mass index:

18
Chapter 3: Manipulating Data Using Methods 18 Using Variables in Output

19
Chapter 3: Manipulating Data Using Methods 19 Complete Program

20
Chapter 3: Manipulating Data Using Methods 20 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

21
Chapter 3: Manipulating Data Using Methods 21 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

22
Chapter 3: Manipulating Data Using Methods 22 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google