Java%20Programming,%20Second%20Edition - PowerPoint PPT Presentation

About This Presentation
Title:

Java%20Programming,%20Second%20Edition

Description:

The in object has access to a method named read() that retrieves data from the keyboard ... showConfirmDialog() method- To create a confirm dialog box which ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 35
Provided by: Dwigh9
Category:

less

Transcript and Presenter's Notes

Title: Java%20Programming,%20Second%20Edition


1
Java Programming, Second Edition
  • Chapter Five
  • Input and Selection

2
In this chapter, you will
  • Accept keyboard input
  • Use the JOptionPane class for GUI input and
    output
  • Draw flowcharts
  • Make decisions with the if and ifelse structures
  • Use compound statements in an if or ifelse
    structure
  • Nest if and ifelse statements
  • Use AND and OR operators
  • Use the switch statement
  • Use the conditional and NOT operators
  • Understand precedence

3
Accepting Keyboard Input
  • Run time - When the program is executing
  • A program that accepts values at run time is
    interactive because it exchanges communications
    with the user
  • Providing values during run time requires input,
    usually through the keyboard
  • The in object has access to a method named read()
    that retrieves data from the keyboard

4
(No Transcript)
5
Accepting Keyboard Input
  • An exception is an error situation
  • There are many different error situations
  • For example
  • Keyboard issues
  • The user might enter the wrong data type

6
Accepting Keyboard Input
  • Let the compiler handle the problem by throwing
    the exception, or passing the error, to the
    operating system
  • Use throws Exception after the main() method
    header

7
Accepting Keyboard Input
  • Prompt- Message requesting user input
  • For example
  • The string Please enter a character
  • You are not required to supply a prompt but it is
    helpful for the user if you do and the user will
    be more likely to enter an appropriate response

8
Using the JOptionPane Class for GUI Input and
Output
  • Swing components- The classes found in the
    javax.swing package define GUI elements and
    provide alternatives to the System.in.read() and
    System.out.println() methods
  • Swing classes are part of the Java Foundation
    Classes, or JFC
  • To access the Swing components import the
    javax.swing package using javax.swing.

9
Using the JOptionPane Class for GUI Input and
Output
  • JOptionPane - Used to create standard dialog
    boxes
  • Three standard dialog boxes
  • InputDialog - prompts the user for text input
  • MessageDialog - displays a user message
  • ConfirmDialog - asks the user a question, with
    buttons for Yes, No, and Cancel responses

10
Input Dialog Boxes
  • showInputDialog() method- Creates an input dialog
    box
  • Asks a question and uses a text field for
    entering a response
  • Two components
  • The parent component
  • The string component- contains a string or icon
    to be displayed

11
Input Dialog Boxes
  • showInputDialog() method with four arguments
  • Parent component
  • String component (prompt)
  • The title to be displayed in the title bar
  • A class variable describing the type of dialog
    box
  • For example
  • ERROR_MESSAGE
  • INFORMATION_ MESSAGE
  • QUESTION_MESSAGE

12
Message Dialog Boxes
  • Message Dialog Boxes- Uses a simple window to
    display information
  • Created with the showMessageDialog() method
  • Parent component
  • String component

13
Message Dialog Boxes
  • showMessageDialog() method with four arguments
  • Parent component
  • String component
  • The title to be displayed in the title bar
  • A class variable describing the type of dialog
    box
  • For example
  • ERROR_MESSAGE
  • INFORMATION_ MESSAGE
  • QUESTION_MESSAGE

14
Confirm Dialog Boxes
  • showConfirmDialog() method- To create a confirm
    dialog box which displays the options Yes, No,
    and Cancel

15
Confirm Dialog Boxes
  • A confirm dialog box with 5 components
  • Parent component
  • String component
  • The title to be displayed in the title bar
  • An integer that indicates which option button
    will be shown
  • An integer that describes the kind of dialog box
    using the class variables ERROR_MESSAGE,
    INFORMATION_MESSAGE, PLAIN_MESSAGE,
    QUESTION_MESSAGE, or WARNING_MESSAGE

16
Drawing Flowcharts
  • Pseudocode- Programmers use a list of tasks that
    must be accomplished to help them plan a
    programs logic
  • Flowchart- The programmer writes the steps in
    diagram form as a series of shapes connected by
    arrows

17
Making Decisions with the if and ifelse
Structures
  • Making a decision involves choosing between
    alternate courses of action based on some value
    within a program
  • The value the decision is based on is always
    Boolean-true or false
  • You can use if or ifelse statements to make a
    decision

18
if and ifelse statements
  • Single alternative- You only perform an action
    based on one alternative
  • Dual alternative- Requires two options for a
    course of action
  • Provides the mechanism for performing one action
    when a Boolean expression evaluates as true and
    if it evaluates to false a different action
    occurs

19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
Using Compound Statements in an if or ifelse
Structure
  • To execute more than one statement that depends
    on the evaluation of a Boolean expression, use a
    pair of curly braces to place the dependent
    statements within a block

23
(No Transcript)
24
Nesting if and ifelse statements
  • Nesting if and ifelse statements- Statements
    with an if inside another if
  • Nested if statements are useful when two
    conditions must be met before some action can
    occur

25
(No Transcript)
26
(No Transcript)
27
Using AND and OR Operators
  • AND operator- Used to determine whether two
    expressions are both true
  • Written as
  • OR operator- Only one of two conditions is true
  • Written as
  • if (itemsSold gt 3 totalValue gt 1000)
  • bonus50

28
(No Transcript)
29
Using the Switch Statement
  • Switch statement- To test a single variable
    against a series of exact integer or character
    values
  • The switch statement uses four keywords
  • switch - starts the structure and is followed
    immediately by a test expression enclosed in
    parentheses
  • case - is followed by one of the possible values
    for the test expression and a colon
  • break - optionally terminates a switch structure
    at the end of each case
  • default - optionally is used prior to any action
    that should occur if the test variable does not
    match any case

30
(No Transcript)
31
Using the Conditional and NOT Operators
  • Conditional operator- Requires three expressions
    separated with a question mark and a colon
  • Is used as an abbreviated version of the ifelse
    structure
  • (testExpression) ? true Result false Result

32
Using the Conditional and NOT Operators
  • NOT operator- To negate the result of any Boolean
    expression
  • Written as the exclamation point (!)
  • boolean oldEnough (age gt 25)
  • if (!oldEnough)
  • System.out.println(Too young!)

33
Understanding Precedence
  • Operations have higher and lower precedences
  • The order in which you use operators makes a
    difference
  • You can always use parentheses to change
    precedence or make your intentions clear

34
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com