340 likes | 514 Views
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
E N D
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 • Nest if and if…else statements • Use AND and OR operators • Use the switch statement • Use the conditional and NOT operators • Understand precedence
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
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
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
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
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.*;
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
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
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
Message Dialog Boxes • Message Dialog Boxes- Uses a simple window to display information • Created with the showMessageDialog() method • Parent component • String component
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
Confirm Dialog Boxes • showConfirmDialog() method- To create a confirm dialog box which displays the options Yes, No, and Cancel
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
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
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
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
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
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
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 ||
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
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
Using the Conditional and NOT Operators • NOT operator- To negate the result of any Boolean expression • Written as the explanation point (!)
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