Chapter 5 - Making Decisions
MIS 3370: Information Systems Development Tools 1
Instructor Information
Professor: Dr. Barry Ingram
Department: Decision and Information Sciences
College: Bauer College of Business
University: University of Houston
Chapter 5: Making Decisions
Planning Decision-Making Logic
Pseudocode
Use paper and a pencil.
Plan a program’s logic by writing plain English statements.
Accomplish important steps in a given task.
Use everyday language.
Flowchart
Visual representation of steps in diagram form.
A series of shapes connected by arrows.
Programmers use various shapes for different tasks:
Rectangle for any unconditional step.
Diamond for any decision.
Planning Decision-Making Logic Structures
Sequence Structure
One step follows another unconditionally.
Cannot branch away or skip a step.
Decision Structure
Involves choosing among alternative courses of action.
Based on some value within a program.
Flowchart Examples
Figure 5-1: Flowchart of a series of sequential steps.
Figure 5-2: Flowchart including a decision.
Note: All computer decisions are yes-or-no decisions.
Boolean values represent true and false values, used in every computer decision.
The if and if…else Structures
if Statement
Sometimes called a single-alternative decision.
Simplest statement to make a decision.
Contains a Boolean expression within parentheses.
No space between the keyword
ifand the opening parenthesis.Execution continues with the next independent statement.
Use a double equal sign (
==) to determine equivalency.Example:
java if (discount < .50) { discount = .50f; }This compares the discount to
.50, and if smaller, sets discount to50%.
Coding Example: Project Setup
Create a project/package in NetBeans for Chapter 5.
Copy
Orders.javaandCustOrders.java.Create a class called
UseConditions.Write code that sets an integer called
quizScoreto26.Write a conditional statement:
if (quizScore > 25) {
System.out.println("I made a 100% on my quiz!");
}
The Role of Semicolons in if Statements
Pitfall: Misplacing a Semicolon
There should be no semicolon at the end of the first line of the if statement; doing so creates an empty statement, causing execution to continue with the next independent statement.
Using the Assignment Operator vs. Equivalency Operator
Attempt to determine equivalency using a single equal sign (
=) is illegal.Correct usage requires double equal sign (
==).
Using Boolean Values as Conditional Statements
Example code change to
useConditions.java:
myQuiz += 19;
boolean isPerfectScore = (myQuiz == 45);
if (isPerfectScore) {
System.out.println("I made a perfect score on my quiz");
}
Attempting to Compare Objects
Use standard relational operators to compare values of primitive data types—not objects.
Use
equalsandnot equalscomparisons (==and!=) to compare memory addresses rather than values (e.g.,firstStudent > secondStudent).
The if…else Structure
Single-alternative if: Acts on one alternative.
Dual-alternative if: Two possible courses of action
if...elsestatement: Executes different actions based on the evaluation of a Boolean expression.
Example of if…else Code
if (quizScore == 10) {
System.out.println("The score is perfect");
} else {
System.out.println("No, it's not");
}
In-Class Assignment Example
In
UseConditions, write a dual alternative that sets an integer variable of choice to 0.Print out white the message depending on
myChoice:If
myChoiceis0: "I made my choice".Else: "I have not made my choice".
Make these changes in
useConditions.java.
Using Multiple Statements in if Conditions
To execute more than one statement, use a pair of curly braces
{}.Place dependent statements within this block, and ensure the correct placement of curly braces to avoid logical errors.
Example demonstrating issues with indentation/missing braces included.
Nesting if and if…else Statements
Nested if statements: Utilize when multiple conditions must be satisfied before performing an action.
Making Accurate and Efficient Decisions
Range Check: A series of if statements verifying if a value lies within a specified range.
Strategically order conditions for efficient evaluation.
Example:
if (score >= 90) {
System.out.print("A");
} else if (score >= 80) {
System.out.print("B");
} // etc.
Commission Rate Decision Example
Example illustrating commission rates set based on thresholds:
if(saleAmount < LOW_LIM) {
commissionRate = LOW_RATE;
} else if(saleAmount < MED_LIM) {
commissionRate = MED_RATE;
} else {
commissionRate = HIGH_RATE;
}
Logical Operators Overview
Logical NOT (
!): Negates any Boolean expression.Logical AND (
&&): Requires both conditions to be true.Logical OR (
||): Requires at least one condition to be true.
switch Statement Explained
An alternative to nested if statements.
Compares a single variable against a series of integer, character, or string values.
Syntax includes:
switch,case,break, anddefaultkeywords.
Example of switch Statement
switch(day) {
case "Monday":
System.out.println("Reserve room for Friday meeting");
break;
default:
System.out.println("Invalid day");
}
Operator Precedence
Indicates how expressions are evaluated.
Includes conventions like using parentheses to clarify intentions.
Example with incorrect vs correct assignment of extra premiums using AND/OR operators.
In-Class Assignment for Discounts
Update logic in
orders.javato ask for percentage discount input instead of a multiplier.Additional tasks include customer type handling and applying customer-specific discounts using a case statement, with final output generation.
Example of Final Receipt Output
A well-formatted receipt showcasing customer type, order number, total costs, and discounts applicable.