CSE 112 RU File Handling and IO and IPO Project

Introduction to JavaChapter 3
Decision Structures and Boolean Logic
Introduction to Java: Chapter 3 Lecture Slides by Chris Simber are licensed under
a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.
All screenshots included on the basis of Fair Use.
Decision Structures and Boolean Logic
• Decisions Structures
– Determine the statements that will execute based upon a
condition
– A conditional expression is used to determine whether or
not a line or lines of code execute
– They provide multiple paths through a program based on
the status of a Boolean (true or false) condition
– If the condition is true, then a statement or statements
are executed, otherwise they are not executed
Chris Simber 2023 – OER Text Resource
2
Decision Structures and Boolean Logic
• Decisions Structures
– The decision structure is implemented using the if
statement which is typically referred to as an if clause
• Assume a Theater has seating for 400 participants.
• Once the Theater has sold 400 tickets, the show has been
sold out
• When this occurs, the Theater displays a “Sold Out” sign
• The decision to display the sign is made based upon
whether or not 400 tickets have been sold
Chris Simber 2023 – OER Text Resource
3
Decision Structures and Boolean Logic
• Decisions Structures
– The condition is a test to determine if 400 tickets been
sold, and if it is true, the “Sold Out” sign is displayed.
– If the condition is false and 400 tickets have not been
sold, then the sign will not be displayed
Chris Simber 2023 – OER Text Resource
4
Decision Structures and Boolean Logic
• Decisions Structures
– Conditional expressions are
represented in flowcharts as
diamonds
– The different paths that the
program can take are shown
using lines from the corners of
the diamond, arrows indicate
the direction, and text
indicates the result
Chris Simber 2023 – OER Text Resource
5
Decision Structures and Boolean Logic
• Decisions Structures
– These paths in the program
are often referred to as the
Flow of Control or the Order
of Operations
– If the condition is true, then
the flow of control follows
the path to display the sign
– If the condition is false, the
program continues
Chris Simber 2023 – OER Text Resource
6
Decision Structures and Boolean Logic
• The if Condition
– When programming a conditional expression in Java, the
syntax includes parenthesis around the conditional
expression, and braces to enclose the statement(s)
executed when the condition is true.
No semicolon
Chris Simber 2023 – OER Text Resource
7
Decision Structures and Boolean Logic
• The if Condition
– The statements to be executed are indented for
readability (most IDEs will automatically indent these
lines).
– Some Java developers place the opening brace on a
separate line following the conditional expression,
however most Java Style Guides prefer the format shown
here
Chris Simber 2023 – OER Text Resource
8
Decision Structures and Boolean Logic
• The if Condition
– The Theater example would test whether 400 tickets have
been sold and display “Sold Out” if it is true
Chris Simber 2023 – OER Text Resource
9
Decision Structures and Boolean Logic
• Decisions Structures
– When multiple statements are associated with a
condition, they form what is commonly referred to as a
block of code
– A block of code is a group of associated statements
typically enclosed in braces
Chris Simber 2023 – OER Text Resource
10
Decision Structures and Boolean Logic
• Decisions Structures
– If the condition is true, all of the statements (the block of
code) within the braces will be executed
– If the condition is false, all of the statements within the
braces (the block of code) will be skipped
Chris Simber 2023 – OER Text Resource
11
Decision Structures and Boolean Logic
• Decisions Structures
– Assume that when the theater show is sold out, in
addition to the sold out sign being displayed, the box
office is closed
– A standard practice is to indent in pseudocode just as the lines
would be indented in the actual code
Chris Simber 2023 – OER Text Resource
12
Decision Structures and Boolean Logic
• Decisions Structures
– The flowchart would be modified to include the added
operation.
Chris Simber 2023 – OER Text Resource
13
Decision Structures and Boolean Logic
• Decisions Structures
– The code would be modified to include the added
operation
Chris Simber 2023 – OER Text Resource
14
Decision Structures and Boolean Logic
• Boolean Expressions
– Conditional expressions are either true or false (Boolean)
– Named after the mathematician George Boole (18151864)
– Boolean expressions are implemented using Relational
Operators that resolve to either true or false by testing
relationships
Boolean expressions are either true or false
Chris Simber 2023 – OER Text Resource
15
Decision Structures and Boolean Logic
• Boolean Expressions
– The result of the expression determines the next step or
path for the program
– For example, one value can be greater than another, or
less than another, or equal to another
• One of these three cases must be true, and the others
would be false
Chris Simber 2023 – OER Text Resource
16
Decision Structures and Boolean Logic
• Boolean Operators
– Note that two equal signs are used to test for equivalence
(a single equal sign is the assignment operator)
Chris Simber 2023 – OER Text Resource
17
Decision Structures and Boolean Logic
• Boolean Operators
– Examples
Chris Simber 2023 – OER Text Resource
18
Decision Structures and Boolean Logic
• The else Condition
– The Theater example conditionally displays “Sold Out”
and “Box Office Closed” if exactly 400 tickets have been
sold
• Otherwise the program does nothing
– To provide for other statements to execute when the
condition is false, an else clause is implemented which
can be thought of as an “otherwise” condition for when
the relational expression is not true
Chris Simber 2023 – OER Text Resource
19
Decision Structures and Boolean Logic
• The else Condition
– When the “if” condition is true, the statements in the “if”
block will be executed and the “else” block will be
skipped
– When the “if” condition is false, the “if” block will be
skipped and the “else” block will execute
Chris Simber 2023 – OER Text Resource
20
Decision Structures and Boolean Logic
• The else Condition
– Continuing the Theater example, if 400 tickets have not
been sold, tickets will continue to be sold
Chris Simber 2023 – OER Text Resource
21
Decision Structures and Boolean Logic
• The else Condition
– A flowchart highlights the two paths taken as a result of the
conditional expression, and that only one path will be executed
Chris Simber 2023 – OER Text Resource
22
Decision Structures and Boolean Logic
• The else Condition
– The modified program.
Chris Simber 2023 – OER Text Resource
23
Decision Structures and Boolean Logic
• The Nested if Structure
– When two (or more) conditions are being tested, there
are several ways to implement the logic
– One of these is to use a nested if, which is an “if”
condition inside another “if” condition
Chris Simber 2023 – OER Text Resource
24
Decision Structures and Boolean Logic
• The Nested if Structure
– If condition1 below is true, then condition2 is tested, and if it is
also true, then the statements will be executed.
– If condition1 is false, then condition2 will not be tested and the
statements are skipped
Chris Simber 2023 – OER Text Resource
25
Decision Structures and Boolean Logic
• The Nested if Structure
– The Theater example now includes a balcony section in
addition to the main-floor seats
– The tickets sales are tracked separately for each floor
– To determine if the show is sold out requires testing both
areas separately for the tickets sold
Chris Simber 2023 – OER Text Resource
26
Decision Structures and Boolean Logic
• The Nested if Structure
Chris Simber 2023 – OER Text Resource
27
Decision Structures and Boolean Logic
• The if, else if, else Structure
– To handle situations where multiple conditions result in
different paths, an “if” and an “else” are inadequate
because there are only two paths
– Additional “if” statements may be appropriate, but more
often the if, else-if, else structure is a better solution
Chris Simber 2023 – OER Text Resource
28
Decision Structures and Boolean Logic
• The if, else if, else Structure
– Note that else-if is not an otherwise condition, but
another conditional test that is only tested if the test
before it is false
– Once a condition is found to be true, the others are
skipped over
– The else clause handles the situation when none of the
conditions is true
Chris Simber 2023 – OER Text Resource
29
Decision Structures and Boolean Logic
• The if, else if, else Structure
Chris Simber 2023 – OER Text Resource
30
Decision Structures and Boolean Logic
• Designing Conditional Structures
Any grade above 60 in the code on the left is a “D”
Chris Simber 2023 – OER Text Resource
31
Decision Structures and Boolean Logic
• Designing Conditional Expressions
– Designing conditional expressions for computer programs
is an important skill to develop
– Many difficult-to-find bugs are caused by incorrect
conditional expressions and relational evaluations, and
the use of break and continue which bypass conditional
logic
– Pseudocode and flowcharts are helpful design tools, and
careful testing during development can eliminate most
bugs
Chris Simber 2023 – OER Text Resource
32
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– When a password is created or changed it is typically
entered twice to confirm
– The two are compared to ensure that they match
– In computing, what is actually being compared is the
ASCII representation of the letters character by character
– Recall from Chapter 1 that each character has a binary
representation in the ASCII character set (Appendix A)
Chris Simber 2023 – OER Text Resource
33
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– To compare Strings in Java, the equivalence operator
cannot be used (it would compare the locations of the
strings in memory)
– The member function String.equals() is used
Chris Simber 2023 – OER Text Resource
34
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– To compare Plan and Play, the ASCII values for the letters
are 110 for ‘n’ and 121 for ‘y’
Chris Simber 2023 – OER Text Resource
35
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– Therefore, they are not equal since ‘y’ has a different
ASCII value than ‘n’
– In fact, Play would be greater than Plan because of the
ASCII value being higher
Play > Plan
True
Characters are compared using their ASCII value
Chris Simber 2023 – OER Text Resource
36
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There is no guarantee that a user will always enter
numeric values when required
– The methods nextInt() and nextDouble() will fail if the
value is not the correct data type
– One way to resolve this is by first reading the input as a
String and then parsing the input to the data type desired
A user may not enter a numeric value as requested
Chris Simber 2023 – OER Text Resource
37
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There are two methods for converting to numeric values
– The method Integer.parseInt() converts the data type
from a string to an integer
Chris Simber 2023 – OER Text Resource
38
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The method Double.parseDouble() converts the string to
a double
Chris Simber 2023 – OER Text Resource
39
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The code below fails and does not complete because the
parsing attempt fails
– The String cannot be parsed to an integer
Chris Simber 2023 – OER Text Resource
40
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The method hasNextInt() returns a Boolean value based
on the next input…it looks ahead
Chris Simber 2023 – OER Text Resource
41
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There are look-ahead methods for double, a line, and
anything
– The method hasNextDouble() will check for a double
– The method hasNext() will check for anything
– The method hasNextLine() will check for a line
Chris Simber 2023 – OER Text Resource
42
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– When a program needs to make decisions based on
complex conditions, multiple conditions can be combined
using the Logical Operators
“&&” for AND
“||” for OR
“!” for NOT
Relational Operators combine conditions
Chris Simber 2023 – OER Text Resource
43
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– With a logical AND, both sides of the expression must be
true for the condition to be true
– If either of them is false, then the expression is false
Both conditions must be true for the expression to be true
Chris Simber 2023 – OER Text Resource
44
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND truth table
Chris Simber 2023 – OER Text Resource
45
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND is often used to verify that a number is
within a range
– If the program requires a number between 0 and 10, the
conditions can be combined in a single expression
– This is referred to as range checking
– Both conditions must be true
Logical AND can be used for numeric range checking
Chris Simber 2023 – OER Text Resource
46
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND used to verify a number is within a range
Chris Simber 2023 – OER Text Resource
47
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The Theater Ticket Sales example required main-floor
seats and balcony seats to be sold out for the Theater to
display the “Sold Out” sign and close the box office
– Both conditions must be true and a nested “if” condition
was used in the example
Chris Simber 2023 – OER Text Resource
48
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The Logical AND combines the conditions
Both conditions must be true for the expression to be true
Chris Simber 2023 – OER Text Resource
49
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The logical OR is used to test an either-or condition
– If either of the conditions is true, then the expressions is
true
With logical OR, if either condition is true the expression is true
Chris Simber 2023 – OER Text Resource
50
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The logical OR truth table
Chris Simber 2023 – OER Text Resource
51
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Converting the previous example with logical OR tests
outside the range instead of inside
– If either condition is true, then the expression is true, and
the number is not within the range required
Chris Simber 2023 – OER Text Resource
52
Decision Structures and Boolean Logic
• Short-circuit Evaluation
– With the logical AND, both sides of the compound
condition must be true for the expression to be true
– So, if the left side is false then the right side is not
evaluated
– It wouldn’t matter if the right side were true since the
expression is already false
Logical AND short-circuit evaluation
Chris Simber 2023 – OER Text Resource
53
Decision Structures and Boolean Logic
• Short-circuit Evaluation
– With the logical OR, when either side is true the
expressions is true
– So, if the left side is true then the right side is not
evaluated
– It wouldn’t matter if the right side were false since the
expression is already true
Logical OR short-circuit evaluation
Chris Simber 2023 – OER Text Resource
54
Decision Structures and Boolean Logic
• Common Logic Errors
– Logic errors are those errors that do not halt execution of
the program but produce incorrect results
– Many of these occur due to incorrect logical expressions
– Pseudocode and flowcharts can help, but careful
consideration of the logic is required
Logic errors produce incorrect results
Chris Simber 2023 – OER Text Resource
55
Decision Structures and Boolean Logic
• Common Logic Errors
– Note the results in these examples
Chris Simber 2023 – OER Text Resource
56
Decision Structures and Boolean Logic
• Boolean Variables
– Variables that can only have a value of true or false
• The computer actually stores these as 1 (true) or 0 (false)
– They are often used to store the result of a condition, or
as a flag that is set as the result of some processing
Boolean variables can only be true or false
Chris Simber 2023 – OER Text Resource
57
Decision Structures and Boolean Logic
• Boolean Variables
– Below a Boolean variable is set to false and if the
condition is true, the variable is flipped to true
Chris Simber 2023 – OER Text Resource
58
Decision Structures and Boolean Logic
• Boolean Variables
– The variable is then used to determine the next step in
the program
Chris Simber 2023 – OER Text Resource
59
Decision Structures and Boolean Logic
• Boolean Variables
– Some programmers prefer to write out the conditional
expression for clarity
• The expressions are equivalent
– The expression on the left is preferred
Chris Simber 2023 – OER Text Resource
60
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– Returns the opposite of a Boolean expression or operand
– If the operand or expression is true, the NOT operator
returns false
– If the operand is false, the NOT operator returns true
Chris Simber 2023 – OER Text Resource
61
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– Use caution when using the NOT operator
– It often introduces bugs and confusion
– This expression reads “If x is greater than y is true, and x
is greater than z is true, then return false”
– It isn’t clear what condition is being tested
Chris Simber 2023 – OER Text Resource
62
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– It is usually easier to reverse the logic and remove the
NOT operator
– De Morgan’s Law provides two forms: one for negation of
an AND expression and one for an OR expression
De Morgan’s Law for opposite relationships
Chris Simber 2023 – OER Text Resource
63
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– When a Boolean variable is involved, the NOT operator
can be used without adding confusion
– Either of these is a correct implementation of the logic
Chris Simber 2023 – OER Text Resource
64
Decision Structures and Boolean Logic
• Test Cases
– As programs become more complex, testing becomes
more complex and involved
– Test Cases are actions executed to verify a portion of and
the complete software implementation
– All possible paths through a program must be verified
– Test Cases ensure complete test coverage
Test Cases document the accuracy of the program
Chris Simber 2023 – OER Text Resource
65
Decision Structures and Boolean Logic
• Test Cases
– Improve the quality of software
– Decrease maintenance and support costs
– Verify requirements
– Are retested when a change is made to the software to
verify that it still works correctly
– Document the validation of the program
Chris Simber 2023 – OER Text Resource
66
Decision Structures and Boolean Logic
• Test Cases
– The Theater Ticket conditional expression has two
Boolean conditions that need to be tested
Chris Simber 2023 – OER Text Resource
67
Decision Structures and Boolean Logic
• Test Cases
– There is one scenario in which the expression is true
Chris Simber 2023 – OER Text Resource
68
Decision Structures and Boolean Logic
• Test Cases
– Should be simple to execute
– Have the end user of the program in mind
– Ensure 100% coverage
– Be repeatable (the same results whenever run)
– Be numbered or labeled for tracking purposes and
regression testing
Test Cases improve the quality of the software
Chris Simber 2023 – OER Text Resource
69
Decision Structures and Boolean Logic
Chapter 3
Decision Structures and Boolean Logic
Creative Commons — Attribution-NonCommercial-ShareAlike 4.0 International — CC BY-NC-SA 4.0
Under the following terms: Attribution — You must give appropriate credit, provide a link to the license, and
indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests
the licensor endorses you or your use.
creativecommons.org
Chris Simber 2023 – OER Text Resource
70
Introduction to Computer Science II
CSE 112
Instructor: Chris Simber
1
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Developing complex algorithms
– Designing solutions





Sequence of operations
Required data and interfaces
Mathematical solutions
Human or program interaction
Test Cases
– Engineering Programmatics



Design reviews
Implementation
Code reviews
2
CSE 112 – Introduction to Computer Science II
Software Engineering:
1. Design Review

Peer/leadership review (acceptance) of the approach
2. Implementation


Writing the Code
Testing
– In a code repository
– Using drivers
3. Code Review


Peer/leadership review of the implementation
implement
4. Incorporation into the program
3
CSE 112 – Introduction to Computer Science II
In industry, Design Review approvals are often required prior to
code being written
– An IPO (Input, Processing, Output) design/description
document is often submitted and reviewed
– Essentially this ensures that the engineer:




has captured the requirements completely and accurately
has an understanding of the task
has developed a basic framework for the solution
follows a design methodology
4
CSE 112 – Introduction to Computer Science II
An IPO design/description document
Program IPO & Requirements Decomposition
Example:
– Modify the program (9-12) to use 6 months of sales data.
Create a function for getting input and a function for
totaling the sales amounts. Use pointers as parameters
for the address of the array as shown in the example.
5
CSE 112 – Introduction to Computer Science II
Program IPO & Requirements Decomposition
Section #1 – Program IPO (Input, Processing, Output)
Program Task: Explain the task that the program is to perform.
Program Input: Explain any input that is needed by the program, and how it
will be provided (file or user input).
Program Output: Explain the program results and output.
6
CSE 112 – Introduction to Computer Science II
Program IPO & Requirements Decomposition
Section #1 – Program IPO (Input, Processing, Output)
Program Task: Modify a program that accepts sales data by quarter to accept
the sales data by month, and display the amount, the memory location where
the data is stored, and the total of the values.
Program Input: Sales data for six (6) months from user via keyboard.
Program Output: Prompts for the sales data by month using three character
month names, and the memory location where the value was stored.
7
CSE 112 – Introduction to Computer Science II
Program IPO & Requirements Decomposition
Section #2 – Design Considerations
Explain the overall design and include any specific design considerations and
data types for this program.
Section #2 – Design Considerations
Two functions will be used: a function to obtain the input, and a function to
produce the output. An array of doubles is used to store the values, and is
passed as a pointer to the two functions. An array of strings is used to store
the month names and is passed as a pointer to the input function.
8
CSE 112 – Introduction to Computer Science II
Program IPO & Requirements Decomposition
Section #3 – Order of operations/flow of control
Explain the order of operations for the solution at a high level, step-by-step in
pseudo-code.
Section #3 – Order of operations/flow of control
Prompt for data input for the first month
Display the memory location where the value was stored
Continue until six months of data have been entered
Total the values and display the total
9
CSE 112 – Introduction to Computer Science II
Section #4 – Program Requirements
Explain what the program requires to complete the task.
Descriptions of any modifications/computations/equations performed on
input data, specific variables needed by the program, functions needed by the
program, any special output formatting, and any libraries needed.
Section #4 – Program Requirements
The program stores but does not modify the input data.
The program requires an array of doubles and an array of strings.
The program requires two functions (input and output).
The arrays will be passed to the functions as pointer variables.
The output will be formatted as a dollar value.
Libraries needed: iostream, and iomanip.
10
CSE 112 – Introduction to Computer Science II
Section #5 – Functions
Function IPO (Input, Processing, Output):
Function name: name_of_function
Declared as: void, double, int, etc.
Task: Explain the task that the function will perform.
External requirements: Explain what the function needs from the program
to perform the task.
Internal requirements: Explain what the function needs internally to
perform the task.
Internal operation: Explain what processing the function will perform.
Return Value: Describe the return value of the function (if any).
11
CSE 112 – Introduction to Computer Science II
Function IPO (Input, Processing, Output):
Function name: get_sales
Declared as: void
Task: The function prompts for six months of sales data and stores the
values in an array of doubles.
External requirements: The function needs a pointer to the array of
doubles, the size of the array, and a pointer to the array of strings.
Internal requirements: The function needs a controlled loop.
Internal operation: A controlled loop will be used to prompt for input and
store the values.
Return Value: The function is void and does not return a value. The input
values are stored in the array.
12
CSE 112 – Introduction to Computer Science II
Function IPO (Input, Processing, Output):
Function name: total_sales
Declared as: double
Task: The function totals the sales data values in the array of doubles.
External requirements: The function needs a pointer to the array of
doubles, and the size of the array.
Internal requirements: The function needs a controlled loop.
Internal operation: A controlled loop will access the elements in the array
and sum the values using a pointer.
Return Value: The function returns a double – the total of the sales
amounts in the array.
13
CSE 112 – Introduction to Computer Science II
Software Engineering:
– IPO’s differ by organization and the standards being used
– Some organizations utilize a criteria to determine the
level of oversight and required documentation/reviews
• Anticipated number of lines of code to be added
• Anticipated number of lines of code to be changed
• Number of modules affected
• Areas of the program requiring changes
– Critical vs non-critical
• Level of complexity (see next slide)
14
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Cyclomatic complexity measure
– aka the “McCabe Essential Complexity Metric”


Measures the depth and quantity of routines
or the number of linearly independent paths through a program’s
source code.
– Developed as an outgrowth of “basis path testing”


Test each linearly independent path through the program
The number of test cases is the cyclomatic complexity of the
program
Example on next slide
15
CSE 112 – Introduction to Computer Science II
• Paths
1
2
16
CSE 112 – Introduction to Computer Science II
• Paths
4
3
17
CSE 112 – Introduction to Computer Science II
• Paths
5
18
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Five possible paths in this case
– A cyclomatic complexity of five
19
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Assignment #1 example:
– (Input error checking added)
– Cyclomatic complexity of two
20
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Other metrics
• Number of Source Lines of Code (SLOCs)
• Number of Executable Source Lines of Code (ELOCs)
• Number of classes and interfaces
• Function Point Analysis
• Maintainability (ISO 9126)
– Maintainability Index (Hagemeister) – next slide
21
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Function Point Analysis




Systems are divided into five large classes and general system
characteristics
External Inputs, External Outputs and External Inquiries.
Logical Files and External Interface Files (where data is stored that
is combined to form logical information).
The general system characteristics assess the general functionality
of the system.
Based on what the user asked for, not what is delivered.
22
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Maintainability Index




Density of operators and operands (number of variables and how
they are used)
Logic complexity (how many execution paths in the code)
Size (number of lines)
Human insight ( comments in the code)
The general formulae for MI is the following:
MIwoc = 171 – 5.2 * ln(aveV) -0.23 * aveG -16.2 * ln(aveLOC)
MIcw = 50 * sin(sqrt(2.4 * perCM))
MI = MIwoc + MIcw
23
CSE 112 – Introduction to Computer Science II
Software Engineering:
– Many people can write code
– Fewer people can write maintainable code
Coding Techniques/Programming Practices
Rob Caron – Microsoft Corporation
Although readability and maintainability are the result of many
factors, one particular facet of software development upon
which all developers have an influence is coding technique.
24
CSE 112 – Introduction to Computer Science II
Microsoft Corporation
The coding techniques are primarily those that improve the
readability and maintainability of code.
Superior coding techniques and programming practices are
hallmarks of a professional programmer.
Microsoft doesn’t mention that the code will work…
25
CSE 112 – Introduction to Computer Science II
In Summary:
– An IPO (Input, Processing, Output) design/description
document ensures that the engineer:
• has captured the requirements completely and
accurately,
• has an understanding of the task,
• has developed a basic framework for the solution,
• follows a design methodology
26
CSE 112 Intro to Computer Science II
Program IPO & Requirements Decomposition
Engineer ______________________________
Section #1 – Program IPO (Input, Processing, Output)
Program Task: Explain the task that the program is to perform.
Program Input: Explain any input that is needed by the program, and how it will be provided (file or
user input).
Program Output: Explain the program results and output.
Section #2 – Design Considerations
Explain the overall design and include any specific design considerations and data types for this
program.
Section #3 – Order of operations/flow of control
Explain the order of operations for the solution at a high level, step-by-step in pseudo-code.
Section #4 – Program Requirements
Explain what the program requires to complete the task.
Descriptions of any modifications/computations/equations performed on input data, specific
variables needed by the program, functions needed by the program, libraries needed, and any
special output formatting.
CSE 112 Intro to Computer Science II
Section #5 – Functions (required for each function in the program)
Function IPO (Input, Processing, Output): complete an IPO section for each function in the program.
Function name: name_of_function
Declared as: void, double, int, etc.
Task: Explain the task that the function will perform.
External requirements: Explain what the function needs from the program to perform the task.
Internal requirements: Explain what the function needs internally to perform the task.
Internal operation: Explain what processing the function will perform.
Return Value: Describe the return value of the function (if any).
CSE 112 Introduction to Computer Science II
CSE 112 – Lab File Line Numbers – File Handling and I/O and IPO Document
Ref: Chapter 12 Programming Challenge #5 (page 714)
Write a program that requests a file name, and reads in the lines from the text file and displays them
with line numbers and a colon (file contents shown below). Limit the display to 24 lines at a time per the
text instructions (press a key). The program may require two (2) cin.get() functions to pause correctly.
The output of the line numbers will be right aligned as shown below. The program may be handled
solely in main. The text for the file is below. Include an IPO submission with the lab (10% of grade).
Grading will be based on meeting all of the lab requirements, adherence to programming standards,
operation and accurate output, and programming style including spacing and indentation.
Sample Screen Capture.
1
Assignments submitted after the due date will incur a grade penalty of three (3) points per day
after the first day, and will not be accepted more than one (1) week late.
CSE 112 Introduction to Computer Science II
File contents to copy and paste:
line 1 from the file
line 2
line 3
line 4
–Write a program that
reads in the
lines from a text file
and displays
them with line numbers
and a colon.
Limit the display to 24
lines at a time
per the instructions.
You can hard code the
file name if
needed.
–Write a program that
reads in the
lines from a text file
and displays
them with line numbers
and a colon.
Limit the display to 24
lines at a time
per the instructions.
You can hard code the
file name if
needed.
–2
Assignments submitted after the due date will incur a grade penalty of three (3) points per day
after the first day, and will not be accepted more than one (1) week late.
Chapter 12:
Advanced File Operations
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
12.1
File Operations
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
File Operations
• File: a set of data stored on a computer, often on
a disk drive
• Programs can read from, write to files
• Used in many applications:
• Word processing
• Databases
• Spreadsheets
• Compilers
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Files
1. Requires fstream header file



use ifstream data type for input files
use ofstream data type for output files
use fstream data type for both input, output
files
2. Can use >>, num;
// use the files
outfile

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER