©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.

Slides:



Advertisements
Similar presentations
DECISIONS Chapter 5. The if Statement  Action based on a conditions  If the condition is true, the body of the statement is executed if (amount
Advertisements

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Math 130 Decisions II Wed, Sept 12, 2007 Notes: ref. C. Horstmann, Java Concepts.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Chapter 5  Decisions 1 Chapter 5 Decisions. Chapter 5  Decisions 2 Chapter Goals  To be able to implement decisions using if statements  To understand.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
If Statement if (amount
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
Chapter 5: Decisions. To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare.
Chapter 6 - Decisions. Characters char char primitive data type primitive data type stores a single character stores a single character operations (same.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Decision Structures and Boolean Logic
Chapter 5 Decisions. Assignments  Written exercises pp 217 – 220 R5.1, R5.3, R5.5, R5.6, R5.9 – R5.15, R5.17, R R5.1, R5.3, R5.5, R5.6, R5.9 –
Chapter Goals To implement decisions using if statements
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Computer Science Selection Structures.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Decisions CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 5, Horstmann*
Decisions: Handling Infinite Cases CS 21a: Introduction to Computing I First Semester,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Fall 2006Slides adapted from Java Concepts companion slides1 Decisions Advanced Programming ICOM 4015 Lecture 5 Reading: Java Concepts Chapter 6.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
If Statement if (amount
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Copyright © 2013 by John Wiley & Sons. All rights reserved. DECISIONS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 15,
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
1 CSC 221: Computer Programming I Fall 2005 Class design & strings  design principles  cohesion & coupling  class examples: Hurricane, TaxReturn  objects.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Decisions Bush decision making.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 5 - Decisions.
Introduction to Control Statements IT108 George Mason University.
Chapter Goals To implement decisions using if statements
5.3 Multiple Alternatives (Part 1)
Chapter 5 – Decisions.
Nested Branches Nested set of statements: Example: Federal Income Tax
Chapter 5/6 Decisions.
DECISIONS.
VB Decisions, Conditions & If
Programming 2 Decision-Making.
Control Structure Chapter 3.
Chapter 5 – Decisions Big Java by Cay Horstmann
Chapter 6 - Decisions.
PROGRAM FLOWCHART Selection Statements.
Chapter 5 – Decisions Big Java by Cay Horstmann
Control Structure.
Presentation transcript:

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 2 Chapter 5: Decisions 2 Figure 1 A Decision

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 3 Chapter 5: Decisions 3 if statement if(condition) statement if (amount <= balance) balance = balance - amount; statement block: if(amount <= balance) { double newBalance = balance - amount; balance = newBalance }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 4 Chapter 5: Decisions 4 Figure 2 Alternative Conditions

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 5 Chapter 5: Decisions 5 if/else statement if(condition) statement else statement if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 6 Chapter 5: Decisions 6 Relational operators like in math = correspond to  == != correspond to =  == is not the same as = : if (x == 5)... x = 5;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 7 Chapter 5: Decisions 7 Comparing floating-point numbers Roundoff errors: double r = Math.sqrt(2) r * r is , not 2 r * r == 2 is false Test if x and y are close enough: |x - y| ,  a small value (e.g ) Not good enough for very small or large values. Use |x - y|  max(|x|, |y|)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 8 Chapter 5: Decisions 8 String comparison Don't use == for strings! if (input == "Y") // WRONG!!! Use equals method: if (input.equals("Y")) == tests identity, equals tests equal contents Case insensitive test ( "Y" or "y" ) if (input.equalsIgnoreCase ("Y"))

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 9 Chapter 5: Decisions 9 Comparing Objects == tests for identity, equals for identical content Rectangle a = new Rectangle(5, 10, 20, 30); Rectangle b = new Rectangle(5, 10, 20, 30); a != b, but a.equals(b) Caveat: equals must be defined for the class (see chapter 9)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 10 Chapter 5: Decisions 10 Figure 4 Comparing Objects

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 11 Chapter 5: Decisions 11 Lexicographic Comparison s.compareTo(t) < 0 means: s comes before t in the dictionary "car" comes before "cargo" comes before x"cathode". All uppercase letters come before lowercase: "Hello" comes before "car"

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 12 Chapter 5: Decisions 12 Figure 3 Lexicographic Comparison

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 13 Chapter 5: Decisions 13 Multiple alternatives if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; else statement4; The first matching condition is executed. Order matters.

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 14 Chapter 5: Decisions 14 Program Richter.java public class Richter { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println ("Enter a magnitude on the Richter scale:"); double magnitude = console.readDouble(); Earthquake quake = new Earthquake(magnitude); System.out.println(quake.getDescription()); } class Earthquake { public Earthquake(double magnitude) { richter = magnitude; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 15 Chapter 5: Decisions 15 public String getDescription() { String r; if (richter >= 8.0) r = "Most structures fall"; else if (richter >= 7.0) r = "Many buildings destroyed"; else if (richter >= 6.0) r = "Many buildings considerably damaged, some collapse"; else if (richter >= 4.5) r = "Damage to poorly constructed buildings"; else if (richter >= 3.5) r = "Felt by many people, no destruction"; else if (richter >= 0) r = "Generally not felt by people"; else r = "Negative numbers are not valid"; return r; } private double richter; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 16 Chapter 5: Decisions 16 Nested branches if (condition1) { if (condition1a) statement1a; else statement1b; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 17 Chapter 5: Decisions 17 Figure 7 Income Tax Computation

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 18 Chapter 5: Decisions 18 Program Tax.java public class Tax { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println("Please enter your income:"); double income = console.readDouble(); System.out.println("Please enter S for single, " + "M for married:"); String status = console.readLine(); TaxReturn aTaxReturn = new TaxReturn(income, status); System.out.println("The tax is " + aTaxReturn.getTax()); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 19 Chapter 5: Decisions 19 class TaxReturn { public TaxReturn(double anIncome, String aStatus) { income = anIncome; status = aStatus; } public double getTax() { double tax = 0; final double RATE1 = 0.15; final double RATE2 = 0.28; final double RATE3 = 0.31; final double SINGLE_CUTOFF1 = 21450; final double SINGLE_CUTOFF2 = 51900;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 20 Chapter 5: Decisions 20 final double SINGLE_BASE2 = ; final double SINGLE_BASE3 = ; final double MARRIED_CUTOFF1 = 35800; final double MARRIED_CUTOFF2 = 86500; final double MARRIED_BASE2 = 5370; final double MARRIED_BASE3 = 19566; if (status.equalsIgnoreCase("S")) { if (income <= SINGLE_CUTOFF1) tax = RATE1 * income; else if (income <= SINGLE_CUTOFF2) tax = SINGLE_BASE2 + RATE2 * (income - SINGLE_CUTOFF1); else tax = SINGLE_BASE3 + RATE3 * (income - SINGLE_CUTOFF2); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 21 Chapter 5: Decisions 21 else { if (income <= MARRIED_CUTOFF1) tax = RATE1 * income; else if (income <= MARRIED_CUTOFF2) tax = MARRIED_BASE2 + RATE2 * (income - MARRIED_CUTOFF1); else tax = MARRIED_BASE3 + RATE3 * (income - MARRIED_CUTOFF2); } return tax; } private double income; private String status; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 22 Chapter 5: Decisions 22 The boolean type George Boole ( ): pioneer in the study of logic value of expression x < 10 is true or false. boolean type: one of these 2 truth values equals method has return type xboolean

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 23 Chapter 5: Decisions 23 Boolean operators && and || or ! not if (tday == bday && tmonth == bmonth)... if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11)... if (tmonth > bmonth || (tmonth == bmonth && tday > bday))...

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 24 Chapter 5: Decisions 24 Figure 8 Flowcharts for && and || Combinations

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 25 Chapter 5: Decisions 25 Truth tables A B A&&B true true true true false false false any false A B A||B true any true false true true false false false A !A true false false true

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 26 Chapter 5: Decisions 26 De Morgan's Law !(A && B) x is the same as !A || !B !(A || B) x is the same as !A && !B if (!(country.equals("USA") && !state.equals("AK) && !state.equals("HI")))... if (!country.equals("USA") || !!state.equals("AK) || !!state.equals("HI"))...

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 27 Chapter 5: Decisions 27 Boolean Variables boolean shipByAir = false; if (!country.equals("USA")) shipByAir = true; else if (state.equals("AK") || state.equals("HI")) shipByAir = true; if (shipByAir)... else... Boolean variables are sometimes called flags

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 28 Chapter 5: Decisions 28 Boolean do's and don'ts don't: if (shipByAir == true)... if (shipByAir != false)... do: if (shipByAir)... don't: if (balance < 0) return true; else return false; do: return balance < 0;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 29 Chapter 5: Decisions 29 Useful tips Brace layout Indentation and tabs Copy and paste Prepare test cases Make a schedule