chapter 3b standard input and output sample development

22
Chapter 3b Standard Input and Output Sample Development

Post on 22-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 3b Standard Input and Output Sample Development

Chapter 3b

Standard Input and Output

Sample Development

Page 2: Chapter 3b Standard Input and Output Sample Development

Topics

• Standard output

• Standard input

• GregorianCalendar class

• LoanCalculator development

Page 3: Chapter 3b Standard Input and Output Sample Development

Standard Output

• The showMessageDialog method is intended for displaying short one-line messages, not for a general-purpose output mechanism.

• Using System.out, we can output multiple lines of text to the standard output window - the console.

Page 4: Chapter 3b Standard Input and Output Sample Development

System.out

• System.out is a PrintStream object.• Two useful methods

– the print method• takes a String as its argument and prints it to the console

– the println method• takes a String as its argument and prints it to the console with a

newline character appended to the end

• Both methods will continue printing from the end of the currently displayed output.

• Both methods will do the necessary type conversion if we pass numerical data.

Page 5: Chapter 3b Standard Input and Output Sample Development

Overloaded + Operator

• The + operator can be used in 2 ways– adding numbers– concatenating Strings

• What happens if we mix its arguments

int x = 1;

int y = 2;

String output = “test” + x + y;

String output = x + y + “test”;

Page 6: Chapter 3b Standard Input and Output Sample Development

Standard Input

• The technique of using System.in to input data is called standard input.

• Associating a BufferedReader object to the System.in object allows us to read a single line of text.

• Then we can use that String:– as it was input if what we need is a String– convert it to a value of a primitive data type

• Using the intermediate InputStreamReader object allows us to read a single character at a time.

Page 7: Chapter 3b Standard Input and Output Sample Development

Input classes

• How the sequence of I/O objects adds greater capabilities.– InputStream has the

capability to read bytes

– InputStreamReader has the capablity to read characters (2 bytes)

– BufferedReader can read an entire line

Page 8: Chapter 3b Standard Input and Output Sample Development

Using BufferedReader

• DeclarationBufferedReader kbd

• Creation kbd = new BufferedReader( new InputStreamReader( System.in));

• UseString input = kbd.readLine();

Page 9: Chapter 3b Standard Input and Output Sample Development

Handling Input Errors

• Calling the readLine method of a BufferedReader can result in an error condition called an exception.

• The programmer is required to handle this potential error condition.

• For now we choose to ignore these problems by adding the clause throws IOException to the method declaration whenever a method includes a call to the readLine method. public static void main(String [] args) throws IOException {

...String input = bufReader.readLine();

...}

Page 10: Chapter 3b Standard Input and Output Sample Development

Getting Numerical Input Values

• Java provides primitive data types for working with numbers.

• Wrapper classes exist that allow you to make an object out of a number.– There is a wrapper class for each primitive type:

Integer, Double, …

• These classes provide methods we can use to convert an appropriate String object to a numerical value.– Integer.parseInt converts a String like "23" to an int– Double.parseDouble converts a String like "2.3" to a

double

Page 11: Chapter 3b Standard Input and Output Sample Development

Example: Getting Numerical Input

BufferedReader in = new BufferedReader( new InputStreamReader( System.in)); System.out.print("Enter radius:"); radiusStr = in.readLine();

radius = Double.parseDouble(radiusStr);

• Similarly, you can use JOptionPane.showInputDialog to get a String to convert.

Page 12: Chapter 3b Standard Input and Output Sample Development

DecimalFormat

• Since floating point numbers can't be represented exactly in computer memory, they often print out with lost of digits after the decimal point.

• The DecimalFormat class lets you print numbers with a fixed number of digits after the decimal place.

• Use a "picture" string to show what you want the number to look like– "0.000" means 3 dgiits after the decimal point

• Use the format method with the number you want formatted as an argument

Page 13: Chapter 3b Standard Input and Output Sample Development

DecimalFormat Example

• Create the DecimalFormat with a StringDecimalFormat df = new DecimalFormat( "0.000");

• Call the format method with the number as an argumentdouble fp;/* assign a value to fp */System.out.print( df.format(fp));

Page 14: Chapter 3b Standard Input and Output Sample Development

The GregorianCalendar Class

• The GregorianCalendar class is useful in manipulating calendar information such as year, month, and day.

• You can create a GregorianCalendar for any date by giving the year, month and daycal = new GregorianCalendar(2001, 8, 11);– Caution: the first month of the year, January, is

represented by 0.

Page 15: Chapter 3b Standard Input and Output Sample Development

Sample Development: Loan Calculator

• Problem Statement– Write a loan calculator that computes monthly

payments and total payments for a loan based on loan amount, annual interest rate and loan period

• Program flow:– Get three input values: loanAmount,

interestRate, and loanPeriod.– Compute the monthly and total payments.– Output the results.

• What classes do we need?

Page 16: Chapter 3b Standard Input and Output Sample Development

UML Diagram for Loan Calculator

Page 17: Chapter 3b Standard Input and Output Sample Development

Loan Calculator

• Steps in implementation:1. Start with code to accept three input values.

2. Add code to output the results.

3. Add code to compute the monthly and total payments.

4. Update or modify code and tie up any loose ends.

Page 18: Chapter 3b Standard Input and Output Sample Development

Loan Calculator: Step 1

• Input Three Data Values• Choices:

– Call showInputDialog method for each of three input values:

– Use a BufferedReader

• Question: What format should the data be in?– loan amount - use double for dollars and cents– annual interest rate - percentage needs to be a double – loan period - usually an even number of years so use an

int

Page 19: Chapter 3b Standard Input and Output Sample Development

Step 2: Output Values

• We must determine an appropriate format to display the computed results, so that the results will be comprehensible to the user.– How much information do we need to show?

• For now, put placeholders in the position the actual results will go

Page 20: Chapter 3b Standard Input and Output Sample Development

Possible Display Formats

• Only the computed values displayed Monthly payment: $ 143.47Total payment: $ 17216.50

• Input values displayed. ForLoan Amount: $ 10000.00Annual Interest Rate: 12%Loan Period(years): 10

Monthly payment is $ 143.47Total payment is $ 17216.50

Page 21: Chapter 3b Standard Input and Output Sample Development

Step 3: Compute Loan Amount

• Complete the program by implementing the formula derived in the design phase.

• We must convert – the annual interest rate (input value) to a

monthly interest rate (per the formula)– the loan period to the number of monthly

payments.

Page 22: Chapter 3b Standard Input and Output Sample Development

Step 4: Finishing Up

• Finalize the program by making necessary modifications or additions.

• We will add a program description and format the monthly and total payments to two decimal places.