Copyright © 2007 SaiGon Tech 1. 2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Calculations In Everyday Contexts.
Subject: Information Technology Grade: 10
Calculations In Everyday Contexts.. Wage Rises Example 1. (a) The new annual wage. (b) The new monthly wage. Solution (a)The new annual wage = old wage.
CS Sept 2006 Pick ups from chapters 4 and 5.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6 Payroll Section 2 Gross Earnings: Piecework and Commissions © 2012 Pearson Education, Inc. All rights reserved.
Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26,
1 Equations and Inequalities © 2008 Pearson Addison-Wesley. All rights reserved Sections 1.1–1.4.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1.In which year was the amount of rainfall highest? How many inches fell that year. 2.What was the average number of inches of rainfall in 2006? 3.What.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Starting Out with Java: From Control Structures through Objects
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Problem Solving with Decisions
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare.
Flow of Control Part 1: Selection
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.
Text Processing and More about Wrapper Classes
Mini-Lesson MA.912.A.3.5 Solve linear equations and inequalities
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
A Second Look at Classes and Objects. Contents A.Stock Purchase Problem B.Employee Problem C.Drawing Lines D.Exercises.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Methods and Data Programming with Alice and Java First Edition.
Jeopardy Q $100 Q $100 Q $100 Q $100 Q $100 Q $200 Q $200 Q $200
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Copyright © 2015, 2008, 2011 Pearson Education, Inc. Section 2.4, Slide 1 Chapter 2 Modeling with Linear Functions.
1 Original Source : and Problem and Problem Solving.ppt.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Chapter 3 - Percents. per cent Percents - Definition percent 100 out of for every.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
WARM UP Solve: 1. 3x – 5 = (3x -5) = x – 3 + 4x = (2x – 4) = 6.
1 Copyright © Cengage Learning. All rights reserved.
CIS 115 ALL EXERCISES DEVRY UNIVERSITY (DEVRY)  CIS 115 All Exercises Devry University CIS 115 ALL ILABS DEVRY UNIVERSITY (DEVRY)  CIS 115 All iLabs.
The switch Statement, and Introduction to Looping
Lecture 3- Decision Structures
Chapter 4 Loops DDC 2133 Programming II.
Topics The if Statement The if-else Statement Comparing Strings
if-else-if Statements
SELECTION STATEMENTS (2)
Chapter 3:Decision Structures
Chapter 4: Decision Structures and Boolean Logic
Topics The if Statement The if-else Statement Comparing Strings
SELECTIONS STATEMENTS
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Copyright © 2007 SaiGon Tech 1

2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples

Copyright © 2007 SaiGon Tech 3 A. Overview of the Sequential Structure Statement 1 Statement 2

Copyright © 2007 SaiGon Tech 4 B. Decision Structures 1.The if statement 2.The if – else statement 3.The switch statement

Copyright © 2007 SaiGon Tech 5 1. The if statement if (BooleanExpression){ statements; } Boolean Expression Statements True False Start Exit

Copyright © 2007 SaiGon Tech 6 2. The if – else statement if (BooleanExpression) { true_statements; } else { false_statements; } Boolean Expression False false_statements true_statements True Start Exit

Copyright © 2007 SaiGon Tech 7 3. The switch statement switch (SwitchExpression) { case CaseExpression_A: statements for CaseExpression_A; break; case CaseExpression_ B: statements for CaseExpression_B; break; default: default statements; } SwitchExpression = Expression A SwitchExpression = Expression B Exit Statements A TrueFalse True Statements B Default Statements False Start

Copyright © 2007 SaiGon Tech 8 C. Decision Structure Problems I.The Grade Problem II.The Loan Verifying Problem III.The Speed of Sound Problem IV.The Guess Word Game Problem V.The Pet Food Problem VI.The Sales Commission Problem VII. The Consulting Charge Problem

Copyright © 2007 SaiGon Tech 9  Test result of a student includes 3 parts: math, essay and interview  The average score of a test result is calculated by following formula:  Calculate the grade of a test result based on following rules: Average Score = (Math score + Essay score + Interview score ) / 3 I. The Grade Problem

Copyright © 2007 SaiGon Tech 10 Score AverageLetter Grade A 80-89B 70-79C 60-69D Below 60F

Copyright © 2007 SaiGon Tech 11 Solution 1.Problem Description (WHAT) 2.Preliminary Class Design (WHAT) 3.Examples (WHAT) 4.Developing the Test Class (WHAT) 5.Developing the Main Class (HOW) 6.Testing

Copyright © 2007 SaiGon Tech Problem Description  Calculate the grade of a test result  Input: math, essay, and interview scores  Output: Grade

Copyright © 2007 SaiGon Tech Preliminary Class Design 2.1. Defining behavior 2.2. Defining properties and remove parameters if necessary 2.3. Defining class name

Copyright © 2007 SaiGon Tech Defining behavior

Copyright © 2007 SaiGon Tech Defining properties and remove parameters if necessary

Copyright © 2007 SaiGon Tech Defining class name

Copyright © 2007 SaiGon Tech Examples  Average score < 60  ‘F’  Average score == 60  ‘D’  Average score < 70  ‘D’  Average score == 70  ‘C’  Average score < 80  ‘C’  Average score == 80  ‘B’  Average score < 90  ‘B’  Average score == 90  ‘A’  Average score > 90  ‘A’

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech Testing 

Copyright © 2007 SaiGon Tech 21 II. Loan Verifying Problem  A loan request consists of annual salary and number of years on current job of the customer  Determine whether a loan request is qualified or not  To qualify, a customer must earn at least $30,000 per year, and must have been on his or her current job for at least two years

Copyright © 2007 SaiGon Tech 22 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

Copyright © 2007 SaiGon Tech Problem Description  Determine whether a loan request is qualified or not  Input: annual salary, number of years on current job  Output: true/false

Copyright © 2007 SaiGon Tech Preliminary Class Design 2.1. Defining behavior 2.2. Defining properties and remove parameters if necessary 2.3. Defining class name

Copyright © 2007 SaiGon Tech Defining behavior

Copyright © 2007 SaiGon Tech Defining properties and remove parameters if necessary

Copyright © 2007 SaiGon Tech Defining class name

Copyright © 2007 SaiGon Tech 28  Annual Salary: | , |  Years on Job: | |  Thus, we should have 9 examples: 1. annual salary < 30,000 && nYears on current job < 2  false 2. annual salary < 30,000 && nYears on current job == 2  false 3. annual salary 2  false 4. annual salary == 30,000 && nYears on current job < 2  false 5. annual salary == 30,000 && nYears on current job == 2  true 6. annual salary == 30,000 && nYears on current job > 2  true 7. annual salary > 30,000 && nYears on current job < 2  false 8. annual salary > 30,000 && nYears on current job == 2  true 9. annual salary > 30,000 && nYears on current job > 2  true 3. Examples

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech 31 III. Speed Of Sound Problem  A sound meter holds information about speed of sound in air, water, and steel (see the table below)  Given the distance and the medium that a sound wave will travel, calculate the amount of time it takes that sound to travel in that medium with the following formula Time = Distance / Speed MediumApproximate speed (feet/second) Air 1,100 Water 4,900 Steel 16,400

Copyright © 2007 SaiGon Tech 32 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

Copyright © 2007 SaiGon Tech Problem Description  Calculate the amount of time that takes a sound to travel in a given medium  Input: distance (feet), medium (air, water, or steel)  Output: time (seconds)

Copyright © 2007 SaiGon Tech Preliminary Class Design

Copyright © 2007 SaiGon Tech Examples  , “air”  1.0  , “water”  1.0  , “steel”  1.0

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class 5.1. Template 5.2. Implementation 5.3. Improving the Code

Copyright © 2007 SaiGon Tech Template

Copyright © 2007 SaiGon Tech Implementation

Copyright © 2007 SaiGon Tech Improving the Code Caring about the case of Strings Removing the Magic Numbers Using static Methods

Copyright © 2007 SaiGon Tech Caring about the case of Strings

Copyright © 2007 SaiGon Tech Removing the Magic Numbers

Copyright © 2007 SaiGon Tech Using static Methods

Copyright © 2007 SaiGon Tech 44

Copyright © 2007 SaiGon Tech 45 IV. The Guess Word Game Problem  The game hides a secret word. Develop a program to check whether the user’s guess is correct or not  Comparison should be case-insensitive

Copyright © 2007 SaiGon Tech 46 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

Copyright © 2007 SaiGon Tech Problem Description  Check whether a guess about a secret word is correct or not  Input: a guess word  Output: true/false

Copyright © 2007 SaiGon Tech Preliminary Class Design

Copyright © 2007 SaiGon Tech Examples Secret Word : SAIGONTECH NoInputOutput 1sAIgONteCHFalse 2SAIGONTECHTrue 3PROSPEROFalse

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech 52 V. The Pet Food Problem  A pet food store has a price table for different food grades as is follows  Given a grade, determine its price Pet Food GradePrice per lb. (cents) A30.0 B20.0 C10.0

Copyright © 2007 SaiGon Tech 53 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class 6.Removing Bad Smell: Magic Numbers

Copyright © 2007 SaiGon Tech Problem Description  Determine the food price  Input: grade  Output: price

Copyright © 2007 SaiGon Tech Preliminary Class Design

Copyright © 2007 SaiGon Tech Examples  ‘A’  30.0  ‘B’  20.0  ‘C’  10.0

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech Removing Bad Smell: Magic Numbers

Copyright © 2007 SaiGon Tech 60 VI. The Sales Commission Problem  Hal’s Home Computer Emporium is a retail seller of home computers. Hal’s sales staff works strictly on commission. At the end of the month, each salesperson’s commission is calculated according to table below: Monthly SalesCommission Rate less than $10,0005% $10,000–14,99910% $15,000–17,99912% $18,000–21,99915% $22,000 or more16%  For example, a salesperson with $16,000 in monthly sales will earn a 12% commission ($1,920). Another salesperson with $20,000 in monthly sales will earn a 15% commission ($3,000).

Copyright © 2007 SaiGon Tech 61  Because the staff gets paid once per month, Hal allows each employee to take up to $1,500 per month in advance. When sales commissions are calculated, the amount of each employee’s advanced pay is subtracted from the commission. If any salesperson’s commissions are less than the amount of their advance, they must reimburse Hal for the difference.  Here are two examples: Beverly and John have $21,400 and $12,600 in sales, respectively. Beverly’s commission is $3,210 and John’s commission is $1,260. Both Beverly and John took $1,500 in advance pay. At the end of the month, Beverly gets a check for $1,710, but John must pay $240 back to Hal.  Calculate the amount of pay by using the formula Pay = Commission – Advanced pay

Copyright © 2007 SaiGon Tech 62 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

Copyright © 2007 SaiGon Tech Problem Description  Calculate the payment of an employee  Input: monthly sales, advanced pay  Output: payment

Copyright © 2007 SaiGon Tech Preliminary Class Design

Copyright © 2007 SaiGon Tech Examples SalesPersonInputOutput SalesAdvanced PayCommissionPayment 1$ 16,000$0$1,920 2$ 20,000$0$3,000 3$ 21,400$1,500$3,210$1,710 4$12,600$1,500$1,260-$240

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech 68 VII. The Consulting Charge Problem  A consulting session records the number of hours on discussion with the customer. The rule is that if the number of hours is less than 5, it is recorded as 5.0  Calculate the total charge of a consulting session, provided that an hour costs $50

Copyright © 2007 SaiGon Tech 69 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

Copyright © 2007 SaiGon Tech Problem Description  Calculate the total charge of a consulting session  Input: number of consulting hours  Output: total charge

Copyright © 2007 SaiGon Tech Preliminary Class Design

Copyright © 2007 SaiGon Tech Examples  4.0   5.0   10.0  500.0

Copyright © 2007 SaiGon Tech Developing the Test Class

Copyright © 2007 SaiGon Tech Developing the Main Class

Copyright © 2007 SaiGon Tech 75 References 1.Tony Gaddis – Starting Out with Java. From Control Structures through Data Structures – Pearson Addison Wesley, 2010, Chapter 3