Java Programming, Second Edition Chapter Five Input and Selection.

Slides:



Advertisements
Similar presentations
1.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 2 - Introduction to Java Applications
Chapter 2: Using Data.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
C++ for Engineers and Scientists Third Edition
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Java Programming Fifth Edition
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Objectives You should be able to describe: Interactive Keyboard Input
Chapter 4: Making Decisions.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Chapter 4: Making Decisions.
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
IF if (condition) { Process… }
Chapter 2 - Introduction to Java Applications
PROGRAM FLOWCHART Selection Statements.
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

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 (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

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 || if (itemsSold > 3 && totalValue > 1000) bonus=50;

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 exclamation point (!) boolean oldEnough = (age > 25); if (!oldEnough) System.out.println(“Too young!”);

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