1 / 34

Java Programming, Second Edition

Java Programming, Second Edition. Chapter Five Input and Selection. 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 if…else structures Use compound statements in an if or if…else structure

bobby
Download Presentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 if…else structures • Use compound statements in an if or if…else structure • Nest if and if…else 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. 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

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

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

  7. 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.*;

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

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

  10. Input Dialog Boxes showInputDialog() 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

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

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

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

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

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

  16. Making Decisions with the if and if…else 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 if…else statements to make a decision

  17. if and if…else 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

  18. Using Compound Statements in an if or if…else 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

  19. Nesting if and if…else statements • Nesting if and if…else statements- Statements with an if inside another if • Nested if statements are useful when two conditions must be met before some action can occur

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

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

  22. 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 if…else structure • testExpression ? true Result : false Result

  23. Using the Conditional and NOT Operators • NOT operator- To negate the result of any Boolean expression • Written as the explanation point (!)

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

More Related