ch10 - Alice in Action with Java Chapter 10 Flow Control in...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Alice in Action with Java Chapter 10 Flow Control in Java
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 2 Objectives Learn how to use the if statement Learn how to use the switch statement Learn how to use the while loop Learn how to use the for loop Learn how to use the do loop
Background image of page 2
Alice in Action with Java 3 Flow Control In Java Purpose of using selective and repetitive execution Implement methods that produce complex behaviors Selective flow control statements: if and switch Repetitive flow control statements: while , for , do
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 4 Introductory Example: pH Background for the use of pH values pH: measures the acidity of a solution pH scale of values: 0 – 14 Labels for ranges in pH scale: acidic, neutral, alkaline Main elements in a user story built around pH values pH values of water samples have been recorded Given pH values of samples, provide correct labels Divide the problem into two subproblems Build a class to model pH values and operations Write a program that uses the class to label samples
Background image of page 4
Alice in Action with Java 5 Subproblem 1: A PH Class The PH class needs a private instance variable double type variable is named myValue myValue will be initialized by a constructor PH constructor Takes an argument that sets myValue If pH argument is invalid, an error message displays Otherwise, pH argument is assigned to myValue The label() method Returns error message if value is outside range Otherwise, returns one of three labels
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 6 Subproblem 1: A PH Class (continued)
Background image of page 6
Alice in Action with Java 7 Subproblem 2: Processing Multiple pH Values Objective: display labels for sequence of pH values Pseudocode for PhLabeler program Set the boolean variable named “done” to false While NOT done: Prompt the user to enter a pH value (-1 to quit) Read ph from the keyboard If ph < the minimum pH value: set done to true ; Else: display phLabel(ph) Observation: if statement nested in a while loop
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 8 Subproblem 2: Processing Multiple pH Values (continued)
Background image of page 8
Alice in Action with Java 9 Selective Execution Directing flow based on the value of a condition Two statements that provide selective execution if statement: general-purpose selection structure switch statement: multi-branch selection structure
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 10 Java’s if Statement General-purpose selection structure Selects from one or more groups of statements The else portion of the if statement is optional One-branch if : envisioned as branching flow Two-branch if : flow follows one of two branches
Background image of page 10
Alice in Action with Java 11 Java’s if Statement (continued)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 12 Java’s if Statement (continued)
Background image of page 12
Alice in Action with Java 13 Java’s if Statement (continued)
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2008 for the course INSY 3300 taught by Professor Wang during the Spring '08 term at UT Arlington.

Page1 / 48

ch10 - Alice in Action with Java Chapter 10 Flow Control in...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online