50
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

Embed Size (px)

Citation preview

Page 1: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

Java Programming, 2EIntroductory Concepts

and Techniques

Chapter 4Decision Making and Repetition with Reusable Objects

Page 2: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

2Chapter 4: Decision Making and Repetition with Reusable Objects

Objectives

• Design a program using methods

• Code a selection structure to make decisions in code

• Describe the use of the logical AND, OR, and NOT operators

• Define exceptions and exception handling

Page 3: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

3Chapter 4: Decision Making and Repetition with Reusable Objects

Objectives

• Code a try statement and a catch statement to handle exceptions

• Create a user-defined method

• Code a repetition structure using the while statement

• Write a switch statement to test for multiple values in data

Page 4: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

4Chapter 4: Decision Making and Repetition with Reusable Objects

Objectives

• Format numbers using a pattern and the format() method

• Construct a Color object

• Use a Checkbox and a CheckboxGroup in the user interface

Page 5: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

5Chapter 4: Decision Making and Repetition with Reusable Objects

Introduction

• Control structures alter the sequential execution of code– Selection structure– Repetition structure

• User-defined methods break tasks into reusable sections of code

• Exception handling in Java allows for the testing of valid and accurate input

Page 6: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

6Chapter 4: Decision Making and Repetition with Reusable Objects

The Sales Commission Program

• Calculate sales commission for agents in a travel agency

• Coded as a console application and an applet• Input

– The user chooses from three types of commission codes

• The commission code identifies the type of sale and the commission rate

– The user enters a sales amount

• Calculate commission and display output

Page 7: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

7Chapter 4: Decision Making and Repetition with Reusable Objects

Page 8: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

8Chapter 4: Decision Making and Repetition with Reusable Objects

Program Development

• Problem analysis– Each user decision corresponds to a program task– Develop and test each task before adding it to the

program

• Design the solution– Design storyboards for the two user interfaces

• Program design– Design a flowchart consisting of required methods– Write related pseudocode for each method

• Validate Design

Page 9: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

9Chapter 4: Decision Making and Repetition with Reusable Objects

Page 10: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

10Chapter 4: Decision Making and Repetition with Reusable Objects

Coding the Program

• Code the program using program stubs – Stubs are incomplete portions of code that serve as a

template or placeholder for later code– Stubs allow ease in debugging through incremental

compilation and testing

• Import java.swing.JOptionPane• Import java.text.DecimalFormat

– Formats decimal output into Strings

• Declare variables• Compile and test the program stub

Page 11: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

11Chapter 4: Decision Making and Repetition with Reusable Objects

Coding the Program

Page 12: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

12Chapter 4: Decision Making and Repetition with Reusable Objects

Writing Methods

• A program with modules allows for clarity, reusability, and refinement

• The code for each module can be separated into programmer-defined methods

• The main() method transfers execution to the methods through a call statement

• The called method returns control to its caller with the return statement or the ending brace– The return statement returns any required data back

to the calling method

Page 13: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

13Chapter 4: Decision Making and Repetition with Reusable Objects

Page 14: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

14Chapter 4: Decision Making and Repetition with Reusable Objects

The if…else Statement

• Single: line 29, line 30• Block: lines 15-26, lines 19-20, lines 23-24• Nested: lines 17-25, lines 29-30

Page 15: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

15Chapter 4: Decision Making and Repetition with Reusable Objects

Page 16: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

16Chapter 4: Decision Making and Repetition with Reusable Objects

Testing with an if statement

• Testing a single condition– if (answer == null)– if (!done)

• Testing multiple conditions– if ((gender == “male”) && (age >= 18))– if ((age < 13) || (age > 65))– AND and OR expressions evaluate the right

operand only if the left operand is not sufficient to decide the condition

Page 17: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

17Chapter 4: Decision Making and Repetition with Reusable Objects

Exception Handling

• An exception is an event resulting from an erroneous situation which disrupts normal program flow

• Exception handling is the concept of planning for possible exceptions by directing the program to deal with them gracefully, without terminating

• Two kinds of exceptions– Run-time– Checked

• The compiler checks each method to ensure each method has a handler

Page 18: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

18Chapter 4: Decision Making and Repetition with Reusable Objects

Handling Exceptions

• The try statement identifies a block of statements that may potentially throw an exception

• The throw statement transfers execution from the method that caused the exception to the handler– Transfers execution to the catch statement if the throw is placed

within a try statement

• The catch statement identifies the type of exception being caught and statements to describe or fix the error

• The finally statement is optional and is always executed regardless of whether an exception has taken place– Placed after the catch statement

Page 19: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

19Chapter 4: Decision Making and Repetition with Reusable Objects

Page 20: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

20Chapter 4: Decision Making and Repetition with Reusable Objects

Page 21: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

21Chapter 4: Decision Making and Repetition with Reusable Objects

Catch an exception

Throw an exception

Page 22: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

22Chapter 4: Decision Making and Repetition with Reusable Objects

Testing methods

• Compile the program after coding each method and call statement

• Run the program with correct input• Run the program to test the exception handling

with invalid input– Alphabetic data– Negative values– Null or zero values

• Verify that the user is allowed to reenter data • Verify the program closes correctly

Page 23: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

23Chapter 4: Decision Making and Repetition with Reusable Objects

Repetition Structure

Page 24: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

24Chapter 4: Decision Making and Repetition with Reusable Objects

The getSales() method

Page 25: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

25Chapter 4: Decision Making and Repetition with Reusable Objects

The getCode() method

Page 26: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

26Chapter 4: Decision Making and Repetition with Reusable Objects

The Case Structure

• A type of selection structure that allows for more than two choices when the condition is evaluated

• Used when there are many possible, valid choices for user input

• The code evaluates the user choice with a switch statement and looks for a match in each case statement

• Each case statement contains a ending break statement which forces exit of the structure

Page 27: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

27Chapter 4: Decision Making and Repetition with Reusable Objects

Page 28: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

28Chapter 4: Decision Making and Repetition with Reusable Objects

Arguments and Parameters

• When a method is called, the calling method sends arguments; the called method accepts the values as parameters

• Different but related identifier names for the arguments and the parameters should be used for good program design– The variables are only visible in their respective

methods

• Arguments and parameters for a called method and the calling statement must be of the same number, order, and data type

Page 29: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

29Chapter 4: Decision Making and Repetition with Reusable Objects

The getComm() Method

Page 30: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

30Chapter 4: Decision Making and Repetition with Reusable Objects

Formatting Numeric Output

• The DecimalFormat class formats decimal numbers into Strings for output

• Supports different locales, leading and trailing zeros, prefixes/suffixes, and separators

• The argument is a pattern, which determines how the formatted number should be displayed

Page 31: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

31Chapter 4: Decision Making and Repetition with Reusable Objects

Page 32: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

32Chapter 4: Decision Making and Repetition with Reusable Objects

The output() method

Page 33: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

33Chapter 4: Decision Making and Repetition with Reusable Objects

The finish() method

Exits system when program completes successfully

Page 34: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

34Chapter 4: Decision Making and Repetition with Reusable Objects

Moving to the Web

• Create the host document to execute the applet

Page 35: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

35Chapter 4: Decision Making and Repetition with Reusable Objects

Coding an Applet Stub

• Enter general block comments• Import java.awt.*, java.applet.*, java.awt.event.*,

and java.text.DecimalFormat• Implement the ItemListener interface to listen for

the user choice on a Checkbox• Code the method headers for the init() and the

itemStateChanged() method– itemStateChanged() is an ItemListener method to

process user choices

• Declare variables and construct a Color object

Page 36: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

36Chapter 4: Decision Making and Repetition with Reusable Objects

Page 37: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

37Chapter 4: Decision Making and Repetition with Reusable Objects

Making Decisions in Applets

• Use a CheckboxGroup to allow user choices

Page 38: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

38Chapter 4: Decision Making and Repetition with Reusable Objects

Constructing Components

• Construct Labels for input and output• Construct a CheckboxGroup for user options

Page 39: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

39Chapter 4: Decision Making and Repetition with Reusable Objects

Adding Components

• Add Labels and CheckboxGroup to the applet

• Add an ItemListener to each Checkbox component with the addItemListener() method

• Add color with the setForeground() and the setBackground() methods

• Set the insertion point with the requestFocus() method

Page 40: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

40Chapter 4: Decision Making and Repetition with Reusable Objects

The init() Method

Page 41: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

41Chapter 4: Decision Making and Repetition with Reusable Objects

Handling Exceptions

• Check for valid data when the itemStateChanged() method is triggered, which happens when the user clicks an option button

Page 42: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

42Chapter 4: Decision Making and Repetition with Reusable Objects

The getSales() Method

• Parse the data from the TextField and return a valid sales amount or throw an exception to the init() method

Page 43: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

43Chapter 4: Decision Making and Repetition with Reusable Objects

The getCode() Method

• Initialize the code to 0• Use nested if statements to assess the boolean state of

the Checkboxes and return the code to init()

Page 44: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

44Chapter 4: Decision Making and Repetition with Reusable Objects

The getComm() Method

• Identical to the application• Return the commission value to init()

Page 45: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

45Chapter 4: Decision Making and Repetition with Reusable Objects

The getOutput() Method

• Send output to the Label using setText()• Construct and use DecimalFormat, using the special

character # to make a leading zero absent

Page 46: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

46Chapter 4: Decision Making and Repetition with Reusable Objects

The paint() Method

• Display a graphic

Page 47: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

47Chapter 4: Decision Making and Repetition with Reusable Objects

Testing the Applet

• Compile the applet• Execute the applet using AppletViewer • Test exception handling by clicking an option button and

then typing invalid data into the text box• Verify the error message and the data clearing

mechanisms• Test all options with valid data• Test in a browser window• Document the applet interface and source code

Page 48: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

48Chapter 4: Decision Making and Repetition with Reusable Objects

Summary

• Repetition Structures– while statement

• Selection Structure– if…else statement– switch and case statements

• Exception handling– Try and catch statements

• Modularity– Smaller segments of reusable code through methods– Program clarity and refinement

Page 49: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

49Chapter 4: Decision Making and Repetition with Reusable Objects

Summary

• Program stubs– Incremental testing

• CheckboxGroup – User options for user interfaces

• Responding to user options– ItemListener, addItemListener(), itemStateChanged()

• Data validation and error messages• Testing invalid data

Page 50: Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects

Java Programming, 2EIntroductory Concepts

and Techniques

Chapter 4 Complete