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