Solving Problems that involve decisions

90
Solving Problems that involve decisions

description

Solving Problems that involve decisions. Topics. Normal execution order. Conditional statements. Relational operators & Boolean expressions. Enumerations. Objectives. At the completion of this topic, students should be able to:. Describe the normal flow of control through a C# program - PowerPoint PPT Presentation

Transcript of Solving Problems that involve decisions

Page 1: Solving Problems that involve decisions

Solving Problems thatinvolve decisions

Page 2: Solving Problems that involve decisions

Topics

Normal execution orderConditional statements

Relational operators & Boolean expressionsEnumerations

Page 3: Solving Problems that involve decisions

ObjectivesAt the completion of this topic, students should be able to:

Describe the normal flow of control through a C# programCorrectly use if statements in a C# programExplain how to use relational operators to write a Boolean expression and use them correctly in a C# programCorrectly use if/else statements in a C# programCorrectly use blocks in if/else statementsCorrectly use a switch statement in a C# programDescribe enumerations and use them in a C# programCorrectly use the logical operators to make complex Boolean expressions

Page 4: Solving Problems that involve decisions

The order in which statements in a program areexecuted is called the flow of control.

Page 5: Solving Problems that involve decisions

Methods that we have written so far beginwith the first line in the method, andcontinue line by line until control reaches the end of the method.

Page 6: Solving Problems that involve decisions

using System;class Program{ static void Main() { const int MINS_PER_HOUR = 60, SPLITTER = 100, PERCENT_BETTER = 1.25;;

int startTime, endTime; int endTime;

// Prompt for input and get start time WriteLine("Enter in the start time for this trip in 24 hour clock time"); Write("in the form hhmm: "); startTime = int.Parse(ReadLine());

// Prompt for input and get end time WriteLine("Enter in the end time for this trip in 24 hour clock time"); Write("in the form hhmm: "); endTime = int.Parse(ReadLine());

// calculate the start and end time with minutes as integers int startMmm = startTime / SPLITTER * MINS_PER_HOUR + startTime % SPLITTER; int endMmm = endTime / SPLITTER * MINS_PER_HOUR + endTime % SPLITTER;

…etc

} // end of Main}

Flow of control

Example of Sequential Flow

Page 7: Solving Problems that involve decisions

There are many problems that cannotbe solved using methods that executein this line-by-line fashion from beginningto end.

Some problems require the program to takedifferent actions, depending upon conditionsthat exist in the program at the time.

Page 8: Solving Problems that involve decisions

Examples

Page 9: Solving Problems that involve decisions

A More Accurate GoodGuys Program

GoodGuy's Delivery service operates a fleet of delivery vehicles that operate between Provo and Salt Lake City. With the I-15 construction work scheduled for Utah County, Goodguys wants you to create a program for them that will compute the new arrival times for deliveries. Once the work on the interstate begins, GoodGuys estimates that their delivery times will increase based on the following table:

midnight to 6:00am no increase6:01am to 9:30am 30% increase9:31am to 3:00am 10% increase3:01pm to 7:00pm 30% increase7:01pm to 10:00pm 10% increase10:01pm to midnight no increase

Page 10: Solving Problems that involve decisions

The U.S. Widget company runs itspayroll program at the end of every two week period.

If an employee gets paid by the hour, that employee’s gross pay is equal to

hoursWorked * hourlyWage;

However, if an employee gets paid asalary, that employee’s gross pay iscalculated differently

yearlySalary / 26.0;

Page 11: Solving Problems that involve decisions

In a computer chess game, the computermight calculate its move in a number of different ways, depending upon where allof the various pieces on the chessboard.

Page 12: Solving Problems that involve decisions

How can you tell if a problem requiresa program that includes this kind of logic?

Page 13: Solving Problems that involve decisions

When the problem statement includes the word “if”

If this employee gets an hourly wageIf the King is in checkIf today is a weekday

If the balance in the account is less than $1,000

Page 14: Solving Problems that involve decisions

Sometimes the problem statement will use the word “when”

When an employee gets an hourly wageWhen the King is in checkWhen the day is a weekday

When the balance in the account is less than $1,000

Page 15: Solving Problems that involve decisions

If the delivery truck Then the deliveryleaves between time increases by

midnight and 6:00am 0%6:01am and 9:30am 30%9:31am and 3:00am 10%3:01pm and 7:00pm 30%7:01pm and 10:00pm 10%10:01pm and midnight 0%

GoodGuys

Page 16: Solving Problems that involve decisions

These are all examples of Conditional Statements,i.e. do something if a condition is true. In programming we often take a different action based on whether or not a condition is true.

Page 17: Solving Problems that involve decisions

Conditional statements are shown in anactivity diagram by using a diamond.

( balance < LIMIT ) balance <LIMIT

?

Page 18: Solving Problems that involve decisions

Conditional statements are shown in anactivity diagram by using a diamond.

Arrows show the execution path that the program takes when the statement is true and when it is false.

if ( balance < LIMIT ) balance <LIMIT

?

true

false

Page 19: Solving Problems that involve decisions

Conditional statements are shown in anactivity diagram by using a diamond.

Arrows show the execution path that the program takes when the statement is true and when it is false.

if ( balance < LIMIT ) balance <LIMIT

?

true

falseThe “if” statement tests the condition.

Page 20: Solving Problems that involve decisions

The if StatementThe if statement allows us to execute a

statement only when the condition is true.

if ( balance < LIMIT ) balance = balance – CHARGE;

balance <LIMIT

?

true balance = balance – CHARGE;

false

note how we have indentedthe statement to be executedwhen the condition is true. Althoughthe compiler doesn’t care about indentation,it makes it easier to read the code.

Page 21: Solving Problems that involve decisions

The if StatementThe if statement allows us to execute a

statement only when the condition is true.

if ( balance < LIMIT ){ balance = balance – CHARGE;}

balance <LIMIT

?

true balance = balance – CHARGE;

false

Use curly braces to clearly define what actionsAre taken when the condition is true

Page 22: Solving Problems that involve decisions

Problem Statement

Write a program that prompts the user to enter in his or herage. the person is under 21, print a message that says

“Youth is a wonderful thing … enjoy it.”

If

Page 23: Solving Problems that involve decisions

Prompt user toEnter in their

age

age < 21? Print

“Youth is a …”

true

false

Store inputin the variable

age

end

Page 24: Solving Problems that involve decisions

using System;

class Program{ const int MINOR = 21;

static void Main() { int age = 0; Write("How old are you? "); age = int.Parse(ReadLine());

if (age < MINOR) { WriteLine("Youth is a wonderful thing ... enjoy it."); } WriteLine(“Goodbye!”); ReadKey(true); }//End Main()}//End class Program

Notice how this block of code isexecuted if the condition is true,but the block is skipped if the condition is false.

Page 25: Solving Problems that involve decisions

The expression inside the if( … )statement is called a Boolean expression,because its value must be either true or false.

Page 26: Solving Problems that involve decisions

Relational Operatorsrelational operators are used to write Boolean expressions

Operator Meaning

== equal != not equal < less than <= less than or equal > greater than >= greater than or equal

(not greater than)

(not less than)

Note: The precedence of relational operators is lower than arithmetic operators, so arithmeticoperations are done first.

Page 27: Solving Problems that involve decisions

Note that the following expressionwill not compile in C#…

if ( age = myNewAge){ . . .}

This is an assignment,not a comparison

Warning: Don’t confuse = and ==

Page 28: Solving Problems that involve decisions

The if/else statementThis construct allows us to do one thing if a conditionis true, and something else if the condition is false.

if (height > MAX) adjustment = MAX – height;else adjustment = 0;

Console.WriteLine(adjustment);

height > MAX?

adjustment =MAX - height

adjustment= 0

true

false

Page 29: Solving Problems that involve decisions

Problem Statement

Write a program that prompts the user to enter in his or herage. the person is under 21, print a message that says

“Youth is a wonderful thing … enjoy it.”

Otherwise, print a message that says

“Old age is a state of mind.”

If

Page 30: Solving Problems that involve decisions

Prompt user toEnter in their

age

age < 21?

Print“Youth is a …”

true

false

Store inputin the variable

age

endPrintOld age is a…”

Page 31: Solving Problems that involve decisions

using System;

class Program{ const int MINOR = 21;

static void Main() { int age; Write("How old are you? "); age = int.Parse(ReadLine());

if (age < MINOR) { WriteLine("Youth is a wonderful thing ... enjoy it."); } else { WriteLine("Old age is a state of mind...!"); }

ReadKey(true); }//End Main()}//End class Program

Page 32: Solving Problems that involve decisions

Executing a Block of Statements

Sometimes we want to execute more than one statement when a condition is true ( or false ).

In C#, we can use a block of statements, delimitedby { and } anyplace where we would normally use asingle statement.

if ( a < b ){ a = 0; b = 0; WriteLine(“Sorry …”);}. . .

Page 33: Solving Problems that involve decisions

using System;

class Program{ const int MINOR = 21;

static void Main() { int age; Write("How old are you? "); age = int.Parse(ReadLine());

if (age < MINOR) { WriteLine("**********************************"); WriteLine("* Youth is a wonderful thing *"); WriteLine("* Enjoy it! *"); WriteLine("**********************************"); }

else { WriteLine("***********************************"); WriteLine("* Old age is a *"); WriteLine("* state of mind...! *"); WriteLine("***********************************"); }

ReadKey(true); }//End Main()}//End class Program

Page 34: Solving Problems that involve decisions

Nested if/else StatementsIn C#, the statement to be executed as theresult of an if statement could be another ifstatement.

In a nested if statement, an else clause isalways matched to the closest unmatched if.

Page 35: Solving Problems that involve decisions

using System;

class Program{ static void Main() { int a, b, c, min = 0;

WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(ReadLine()); b = int.Parse(ReadLine()); c = int.Parse(ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

WriteLine($"The smallest value entered was {min}"); ReadKey(true); }//End Main()}//End class Program

keep track of which if an else goes with!

Page 36: Solving Problems that involve decisions

using System;

class Program{ static void Main() { int a, b, c, min = 0;

Console.WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(ReadLine()); b = int.Parse(ReadLine()); c = int.Parse(ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

WriteLine($"The smallest value entered was {min}“); ReadKey(true); }//End Main()}//End class Program

Hmmm … it’s hard without indenting code

Page 37: Solving Problems that involve decisions

using System;

class Program{ static void Main() { int a, b, c, min = 0;

Console.WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(ReadLine()); b = int.Parse(ReadLine()); c = int.Parse(ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

WriteLine($"The smallest value entered was {min}"); ReadKey(true); }//End Main()}//End class Program

Hmmm … it’s worse if you indent wrong

Page 38: Solving Problems that involve decisions

Another Example

char direction = ‘n’;

WriteLine("Press a direction key(n, s, e, or w) and Enter"); direction = char.Parse(Console.ReadLine()); if (direction == 'n') WriteLine("You are now going North."); else if (direction == 's') WriteLine("You are now going South."); else if (direction == 'e') WriteLine("You are now going East."); else if (direction == 'w') WriteLine("You are now going West."); else WriteLine("You don't know where you are going!");

ReadKey(true);

Page 39: Solving Problems that involve decisions

if (direction == 'n') WriteLine(“…going North."); else if (direction == 's') WriteLine(“…going South."); else if (direction == 'e') WriteLine(“…going East."); else if (direction == 'w') WriteLine(“…going West."); else WriteLine("You don't know…!");

if (direction == 'n') WriteLine(“…going North.");else if (direction == 's') WriteLine(“…going South.");else if (direction == 'e') WriteLine(“…going East."); else if (direction == 'w') WriteLine(“…going West.");else WriteLine("You don't know…!");

These are exactlythe same. The onlydifference is how

the code is indented.

Page 40: Solving Problems that involve decisions

if (direction == 'n') WriteLine(“…going North.");else if (direction == 's') WriteLine(“…going South.");else if (direction == 'e') WriteLine(“…going East."); else if (direction == 'w') WriteLine(“…going West.");

if (direction == 'n') WriteLine(“…going North.");if (direction == 's') WriteLine(“…going South.");if (direction == 'e') WriteLine(“…going East.");if (direction == 'w') WriteLine(“…going West.");

What’s thedifference?

Page 41: Solving Problems that involve decisions

if (direction == 'n') WriteLine(“…going North.");else if (direction == 's') WriteLine(“…going South.");else if (direction == 'e') WriteLine(“…going East."); else if (direction == 'w') WriteLine(“…going West.");

if (direction == 'n') WriteLine(“…going North.");if (direction == 's') WriteLine(“…going South.");if (direction == 'e') WriteLine(“…going East.");if (direction == 'w') WriteLine(“…going West.");

Suppose that ‘n’was entered.

Page 42: Solving Problems that involve decisions

if (direction == 'n') WriteLine(“…going North.");else if (direction == 's') WriteLine(“…going South.");else if (direction == 'e') WriteLine(“…going East."); else if (direction == 'w') WriteLine(“…going West.");

Suppose that ‘n’was entered.

This statement is executed andall of the others are skipped.

Page 43: Solving Problems that involve decisions

if (direction == 'n') WriteLine(“…going North.");if (direction == 's') WriteLine(“…going South.");if (direction == 'e') WriteLine(“…going East.");if (direction == 'w') WriteLine(“…going West.");

Suppose that ‘n’was entered.

The first output statement is executed,then each of the other if statementsis executed in turn. There is no elseclause to cause control to skip these.

Page 44: Solving Problems that involve decisions

Dangling else clauses

An else is always matched to the closest unmatched if

Page 45: Solving Problems that involve decisions

const int R_LIMIT = 40;const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT) if (overTime > O_LIMIT) Console.WriteLine(“Overtime hours exceed the limit.”);else WriteLine(“no overtime worked.”);

Will this code do what you expect?

If an employee works more than 40 hoursa week, then make sure that they don’twork more than 20 hours of overtime

Page 46: Solving Problems that involve decisions

Will this code do what you expect?

This else goes with The second if statement

. . . If regTime were 50 and overTime were 10, the message “no overtime worked” would be displayed.

const int R_LIMIT = 40;const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT) if (overTime > O_LIMIT) WriteLine(“Overtime hours exceed the limit.”);else WriteLine(“no overtime worked.”);

Page 47: Solving Problems that involve decisions

The code should be written this way.

const int R_LIMIT = 40;Const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT){ if (overTime > O_LIMIT) WriteLine(“Overtime hours exceed the limit.”);}else WriteLine(“no overtime worked.”);

Page 48: Solving Problems that involve decisions

Practice:Write a program that takes a temperature in degreesCelsius. Then output whether or not water is a solid, liquid, or vapor at that temperature at sea level.

Example:Input: -5Output: Water is solid

Page 49: Solving Problems that involve decisions

Designing programs that use conditional statements

Page 50: Solving Problems that involve decisions

1. Carefully inspect the problem statement. If word “if” is used or the word “when” is used to describe different situations that might exist, then the program probably will need to use conditional statements.

Page 51: Solving Problems that involve decisions

2. After determining that several different situations might exist, try to identify each unique situation. Write down the condition that makes this situation unique.

Carefully check that each condition is unique - there should be no overlaps. Be sure that the boundary conditions are correctly stated.

Page 52: Solving Problems that involve decisions

3. Write down exactly what the program should do differently in each of the unique situations that you have identified.

Page 53: Solving Problems that involve decisions

4. Formulate the if/else statements that reflect this set of conditions. Make them as simple as possible. Watch for dangling else’s.

Page 54: Solving Problems that involve decisions

Example

Slick Guys Used Car Sales gives their sales people acommission, based on the value of the sale. If the salewas made at or above the sticker price, the sales person gets a 20% commission. If the sale was less than the full sticker price, but greater than the blue book price, the sales commission is 10%. If the sale price is equal to theblue book price, the sales person gets a 5% commission. Slick Guys never sells a car for less than the blue book price.

Page 55: Solving Problems that involve decisions

How many conditions are there?

What are the conditions?

Page 56: Solving Problems that involve decisions

1. The sales price is GREATER than or EQUAL to the sticker price.

2. The sales price is LESS than the sticker price but GREATER than the blue book price.

3. The sales price EQUALS the blue book price.

4. The sales price cannot be less than blue book.

4 Conditions

Page 57: Solving Problems that involve decisions

1. The sales price is GREATER than or EQUAL to the sticker price.

2. The sales price is LESS than the sticker price but GREATER than the blue book price.

3. The sales price EQUALS the blue book price.

4. The sales price cannot be less than blue book

What do you do for each situation?

Set commission = 20% of sales price

Set commission = 10% of sales price

Set commission = 5% of sales price

Message: Cannot sell for less than blue book

Page 58: Solving Problems that involve decisions

Sticker PriceBlue Book Price

5%

20%10%

Boundary Conditions

no sale

Page 59: Solving Problems that involve decisions

Sticker PriceBlue Book Price

5%

20%10%

Boundary Condition

Put your conditions in order … start with the most likely

no sale

Page 60: Solving Problems that involve decisions

issale >= sticker

?commission

= 0.20

issale > bluebk

?commission

= 0.10

isSale = bluebk

?commission

= 0.05

No sale

true

true

true

false

false

false

Page 61: Solving Problems that involve decisions

issale >= bluebk

?commission

= 0.10

issale > sticker

?commission

= 0.20

issale = bluebk

?commission

= 0.05

No sale

true

true

true

false

false

false

What happens if you putthe conditions in the wrongorder?

Suppose that the sticker price = 20,000, the blue book price = 19,000, and the sale price = 21,000. What will the commission be?What should it be?

Page 62: Solving Problems that involve decisions

if ( salesPrice >= STICKER_PRICE) commissionRate = 0.20;else if ( salesPrice > BLUE_BOOK) commissionRate = 0.10;else if (salesPrice == BLUE_BOOK) commissionRate == 0.05;else Console.WriteLine(“No Sale!”);

Page 63: Solving Problems that involve decisions

The Switch StatementThe switch statement allows a program to take one of several different paths, based on the value given to the switch statement.

switch ( numValues ){ case 0: WriteLine(“No values were entered”); break;

case 1: WriteLine(“One value was entered”); break;

case 2: WriteLine(“Two values were entered”); break;

default: WriteLine(“Too many values were entered”); break;}

must be an integer or a character literal

without the break statement, you will geta compile error.

if numValues is not0, 1, or 2, execution goes here.

Page 64: Solving Problems that involve decisions

EnumerationsAn enumeration type is a user defined data type whose valid values are defined by a list of constants of type int.

enum DayOfWeek { SUN=0, MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6 };

if values are not explicitly given, then the first constant is assigned zero, and then each is incremented by one. So the following is equivalent to the above example:

enum DayOfWeek { SUN, MON, TUE, WED, THU, FRI, SAT };

Page 65: Solving Problems that involve decisions

Enumerations are often used to label the cases in a switch statement:enum Days { MON = 1, TUE = 2, WED = 3};{ - - -

switch ( (Days)whichDay ) { case Days.MON: WriteLine("Today is Monday"); break;

case Days.TUE: WriteLine("Today is Tuesday"); break;

case Days.WED: WriteLine("Today is Wednesday"); break;

default: WriteLine("What day is it?"); break; } ReadKey(true); }

Page 66: Solving Problems that involve decisions

Handling Complex Conditions

Page 67: Solving Problems that involve decisions

Consider the following situation:

If it is Friday night and you have no homeworkthen go to the movies.

These two conditions can be combined using logical operators.

Page 68: Solving Problems that involve decisions

Logical OperatorsOperator Description Example

! logical NOT !a && logical AND a && b || logical OR a || b

the results of logical operations are defined in truth tables

Page 69: Solving Problems that involve decisions

a !a

false true

true false

Logical NOT

Page 70: Solving Problems that involve decisions

Logical AND

a b a && b

false false false

false true false

true false false

true true true

the evaluation of &&is short circuited if ais false.

Page 71: Solving Problems that involve decisions

Logical OR

a b a || b

false false false

false true true

true false true

true true true

the evaluation of ||is short circuited ifa is true.

Page 72: Solving Problems that involve decisions

Consider the following situation:

If it is Friday night and you have no homeworkthen go to the movies.

if ( today == FRIDAY && homework == NO) goToMovie( );

Page 73: Solving Problems that involve decisions

Simplifying complex relationshipsusing logical operators

Page 74: Solving Problems that involve decisions

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

Page 75: Solving Problems that involve decisions

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

We could write the conditions as follows:

if ( y > x) if ( y < z) WriteLine(“\n the value of y lies between x and z.”); else WriteLine( “\n the value of y does not lie between x and z.”);else WriteLine( “\n the value of y does not lie between x and z.”);

Page 76: Solving Problems that involve decisions

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

But the following is simpler and eliminates one else clause:

if ( y > x && y < z ) WriteLine( “\n the value of y is between x and z.”);else WriteLine( “\n the value of y is not between x and z.”);

Page 77: Solving Problems that involve decisions

Operator Precedence (again)

+ unary plus- unary minus! Not

* multiplication/ division% remainder

+ addition- subtraction

higher precedence

lower precedence

Page 78: Solving Problems that involve decisions

< less than> greater than<= less than or equal>= greater than or equal

== equal!= not equal

&& and

|| or

higher precedence

lower precedence

= assignment+= add and assign-= subtract and assign*= multiply and assign/= divide and assign%= modulo and assign

Page 79: Solving Problems that involve decisions

Comparing CharactersCharacters in the ASCII standard are arranged so that they compare in alphabetical order (collating sequence). Lower case characters are not equal to their upper case counterparts. As a matter of fact lower case characters aregreater in value than upper case characters.

Page 80: Solving Problems that involve decisions

The ASCII Code Table

Page 81: Solving Problems that involve decisions

c a t c a r99 97 116 99 97 114

C a t CAR67 97 116 67 65 82

Page 82: Solving Problems that involve decisions

Comparing Real NumbersBecause of the way that real numbers are representedin the computer, you cannot be guaranteed that tworeal numbers are exactly equal to one another.

The best way to handle this situation is to subtract oneReal number from the other and see if the absolute value oftheir difference is smaller than some tolerance value.

Page 83: Solving Problems that involve decisions

const double EPS = 0.0001;

. . .

if ( (a – b) < EPS) ) WriteLine(“\nThey are equal…”);

Testing to see if two floating point values are equal

Page 84: Solving Problems that involve decisions

PracticeWhat is the value of the following, given that the value of count = 0,and the value of limit = 10? We don’t know what x and y are.

(count == 0 ) && (limit < 20) true

(count == 0 && limit < 20) true

(limit > 20 || count < 5) true

!(count == 12) true

(count == 1 && x < y) false

(count < 10 || x < y) true

(limit / count > 7 && limit < 0) divide by zero error!

(limit < 20 || limit / count > 7) true

Page 85: Solving Problems that involve decisions

Not all problems requiring decisions will use the word“if” or “when” in the problem description.

Learn to identify problems that may contain choices.For example …

A triangle whose sides are all of equal length is called an equilateral triangle.

A triangle with two equal sides is called an isosceles triangle.

A triangle whose sides are all of differentlengths is called a scalene triangle.

Write a program that asks for the lengths of the three sides of a triangle, and thentells the user what kind of triangle it is.

Page 86: Solving Problems that involve decisions

Groups of four

* Design and code these programs

Page 87: Solving Problems that involve decisions

Practice

Problem: Allow the user to type in two integers.Print out the value of the highest number. Numbersmay be negative. If they are equal, say so.

Page 88: Solving Problems that involve decisions

Practice

Obe Wan Kenobi is in charge of reviewing the applicationsof this years candidates for Jedi Knight school. Candidatesmust be at least 3’ high, but less than 8’. However, an exception has been made for Federation pilots, who can beof any height. Candidates under 3’ can go to Jr. Jedi Knight School.

Write a program that * gets a candidate’s height * asks if they are a federation pilot, * Then prints one of the following messages, as appropriate: - This candidate is accepted into Jedi Knight School - This candidate is accepted into Jr. Jedi Knight School - This candidate is not accepted.

Page 89: Solving Problems that involve decisions

PracticeThe DeepPockets Savings Bank computes the monthly interest rate on an account, based on the following:

Super Saver Account Standard Account

5% interest on the balance Always pay 3% interest as long as it is more than on the balance. $5,000. Otherwise pay 3%.

Write a program that computes the interest on an account,given the balance on the account and the type of account.

Page 90: Solving Problems that involve decisions

Write a tollbooth program that calculates the toll requiredto use the highway, based on the class of vehicle.

Passenger cars $2.00Busses $3.00Trucks $5.00

Use a switch statement and an enumeration.

Practice