Chapter 3b - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 3b

Description:

Using System.out, we can output multiple lines of text to the standard output ... by adding the clause throws IOException to the method declaration whenever a ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 23
Provided by: amyh99
Learn more at: http://cs.boisestate.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3b


1
Chapter 3b
  • Standard Input and Output
  • Sample Development

2
Topics
  • Standard output
  • Standard input
  • GregorianCalendar class
  • LoanCalculator development

3
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.

4
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.

5
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
6
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.

7
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

8
Using BufferedReader
  • Declaration
  • BufferedReader kbd
  • Creation
  • kbd new BufferedReader(
  • new InputStreamReader(
  • System.in))
  • Use
  • String input kbd.readLine()

9
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()
  • ...

10
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

11
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.

12
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

13
DecimalFormat Example
  • Create the DecimalFormat with a String
  • DecimalFormat df
  • new DecimalFormat( "0.000")
  • Call the format method with the number as an
    argument
  • double fp
  • / assign a value to fp /
  • System.out.print( df.format(fp))

14
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 day
  • cal
  • new GregorianCalendar(2001, 8, 11)
  • Caution the first month of the year, January, is
    represented by 0.

15
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?

16
UML Diagram for Loan Calculator
17
Loan Calculator
  • Steps in implementation
  • Start with code to accept three input values.
  • Add code to output the results.
  • Add code to compute the monthly and total
    payments.
  • Update or modify code and tie up any loose ends.

18
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

19
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

20
Possible Display Formats
  • Only the computed values displayed
  • Monthly payment 143.47
  • Total payment 17216.50
  • Input values displayed.
  • For
  • Loan Amount 10000.00
  • Annual Interest Rate 12
  • Loan Period(years) 10
  • Monthly payment is 143.47
  • Total payment is 17216.50

21
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.

22
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.
Write a Comment
User Comments (0)
About PowerShow.com