0% found this document useful (0 votes)
27 views82 pages

Session 4 - Programming Layer

The document discusses algorithms and problem solving techniques. It covers topics like asking questions to understand problems, looking for familiar things, dividing problems into smaller subproblems, using pseudocode, and different types of algorithms including sequential, conditional, and iterative operations. Various examples of algorithms are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views82 pages

Session 4 - Programming Layer

The document discusses algorithms and problem solving techniques. It covers topics like asking questions to understand problems, looking for familiar things, dividing problems into smaller subproblems, using pseudocode, and different types of algorithms including sequential, conditional, and iterative operations. Various examples of algorithms are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

Introduction to Computing

Section 4 – Programming Layer

Communication
Application
Operating System
Programming
Hardware
Information
Problem Solving
SCSE

 G. Polya wrote How to Solve It:


A New Aspect of Mathematical
Method
 His “How to Solve It” list is
quite general
 Written in the context of solving
mathematical problems
 The list becomes applicable to all
types of problems

HCM International University - SCSE Section 4 – Programming Layer 2


Ask Questions...
SCSE

 …to understand the problem


 What do I know about the problem?
 What is the information that I have to process in
order the find the solution?
 What does the solution look like?
 What sort of special cases exist?
 How will I recognize that I have found
the solution?

HCM International University - SCSE Section 4 – Programming Layer 3


Look for Familiar Things
SCSE

 You should never reinvent the wheel


 In computing, you see certain problems again
and again in different guises
 A good programmer sees a task,
or perhaps part of a task (a subtask),
that has been solved before and plugs
in the solution

HCM International University - SCSE Section 4 – Programming Layer 4


Divide and Conquer
SCSE

 Break up a large problem into smaller units


that we can handle
 Applies the concept of abstraction
 The divide-and-conquer approach can be applied
over and over again until each subtask is
manageable

HCM International University - SCSE Section 4 – Programming Layer 5


Algorithms
SCSE

Algorithm A set of instructions for solving a


problem or sub-problem in a finite amount of
time using a finite amount of data
 The instructions must be unambiguous

HCM International University - SCSE Section 4 – Programming Layer 6


Computer Problem-Solving
SCSE

Figure 6.2 The computer problem-solving process

HCM International University - SCSE Section 4 – Programming Layer 7


Methodology SCSE
for designing algorithms

 Analyze the problem


 List the main Tasks
 Write the remaining Modules
 Re-sequence and revise as necessary

HCM International University - SCSE Section 4 – Programming Layer 8


Top-Down Design
SCSE

Figure 6.5
An example
of top-down
design

 This process continues for as many levels as it takes to


expand every task to the smallest details
 A step that needs to be expanded is an abstract step
HCM International University - SCSE Section 4 – Programming Layer 9
Pseudocode
SCSE

 Uses a mixture of English and formatting to


make the steps in the solution explicit

HCM International University - SCSE Section 4 – Programming Layer 10


Algorithms
SCSE

An algorithm:

 an ordered sequence of precisely defined instructions


that performs some task in a finite amount of time.

must have ability to alter the order of its instructions


using a control structure.

Algorithm operations: sequential operations,


conditional operations, iterative operations (loops)

HCM International University - SCSE Section 4 – Programming Layer 11


Algorithms
SCSE

Sequential operations: executed in order.

Conditional operations: first ask a question to be


answered with a true/false answer and the select the
next instruction based on the answer.

Iterative operations (loops): repeat the execution of a


block of instruction

HCM International University - SCSE Section 4 – Programming Layer 12


Algorithms
SCSE

Sequential Operations
Compute the perimeter p and the area A of a triangle whose
sides are a, b, c. The formulas are:
A  ss  a s  b s  c 
p
p  abc s
2
1. Enter the side lengths a, b, and c.
2. Compute the perimeter p: p=a+b+c
3. Compute the semi perimeter s: s = p/2
4. Compute the area A.
5. Display the results p and A.
6. Stop
HCM International University - SCSE Section 4 – Programming Layer 13
Conditional Operations
Given the (x,y) coordinates of a point, compute its polar SCSE

coordinates (r,), where


1  y 
r x y
2 2
  tan  
x
1. Enter the coordinates x and y.
2. Compute the hypoteneuse r. r  x2  y2

3. Compute the angle 


1  y 
3.1. If x0:   tan  
 
x
 y
3.2. Else:   tan 1    pi
x

4. Convert the angle to degrees.    *180 / pi


5. Display the results r and .
6. Stop
Section 4 – Programming Layer
Iterative Operations
Determine how many terms are required for the sum of theSCSE
series 10k2 - 4k +2, k=1, 2, 3, … to exceed 20,000. What is the
sum for this many terms.
Because we do not know how many times we must evaluate
the expression 10k2 - 4k +2, we use a “while” loop.
1. Initialize the total to zero.
2. Initialize the counter to zero.
3. While the total is less than 20,000 compute the total.
3.1. Increment the counter by 1: k=k+1;
3.2. Update the total: total  10 * k  4 * k  2  total
2

4. Display the current value of the counter.


5. Display the value of the total.
6. Stop.
Section 4 – Programming Layer
Algorithms with Simple Variables
SCSE

 An algorithm with selection


Ex: What dress is appropriate for a given outside
temperature with four options:
 Shorts if it is hot
 Short sleeves if it is nice but not too hot
 A light jacket if the temperature is chilly
 Heavy coat if it is cold
 If the temperature is below freezing, stay inside
HCM International University - SCSE Section 4 – Programming Layer 16
Algorithms with Simple Variables
SCSE

The top-level (main) module:


1. Write “Enter the temperature”
Not need further decomposing
2. Read the temperature
3. Determine dress:
 List all cases and define the corresponding temperatures
hot: >90, nice: >70, chilly: >50, cold: >32
 Write the pseudocode for “Determine dress”

HCM International University - SCSE Section 4 – Programming Layer 17


Algorithms with Simple Variables
SCSE

Determine dress (pseudocode)

IF (temperature >90)
Write “so hot: wear shorts”
ELSE IF (temperature >70)
Write “Ideal temperature: short sleeves are fine”
ELSE IF (temperature >50)
Write “A little chilly: wear a light jacket”
ELSE IF (temperature >32)
Write “so cold: wear a heavy coat”
ELSE
Write “Stay inside”

HCM International University - SCSE Section 4 – Programming Layer 18


Algorithms with Simple Variables
SCSE

 An algorithm with repetition (count controlled and


event controlled)
 Count controlled loops: repeats a process a
specified number of times.
 Event controlled loops: the number of repetition is
controlled by an event that occurs within the body
of the loop itself.
HCM International University - SCSE Section 4 – Programming Layer 19
Algorithms with Simple Variables
SCSE

Count controlled loops:


Three distinct parts:
1. Initialization: loop control variable
2. Testing: loop control variable reaches a
predetermined value?
3. Incrementation: loop variable is incremented by a
value?
HCM International University - SCSE Section 4 – Programming Layer 20
Algorithms with Simple Variables
SCSE

Read limit //Input data


Set count to 0 //Initialize count to 0
WHILE (count < limit) //Test
…….. //Body of the loop
Set count to count + 1 //Increment
……… //Statement(s) following loop

HCM International University - SCSE Section 4 – Programming Layer 21


Algorithms with Simple Variables
SCSE

Example: A class of ten students took a quiz. The grades (integers in the
range 0 to 100) for this quiz are available to you. Determine the class
average on the quiz

Set total to zero


Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average

HCM International University - SCSE Section 4 – Programming Layer 22


Algorithms with Simple Variables
SCSE

Event controlled loops:


Three distinct parts:
1. The event must be initialized
2. The event must be tested
3. The event must be updated

HCM International University - SCSE Section 4 – Programming Layer 23


Algorithms with Simple Variables
SCSE

Read and sum data values until a negative value is read:


1. What is the event?
 Reading a positive value.
2. How do we initialize the event?
Reading the first data value, testing the value to determine whether its
is positive and enter the loop if it is.
3. How do we update the event?
Reading the next data value.

HCM International University - SCSE Section 4 – Programming Layer 24


Algorithms with Simple Variables
SCSE

Example:
Develop a class-averaging program that will process an
arbitrary number of grades each time the program is run.
◦ Unknown number of students
◦ How will the program know to end?

 Use sentinel value


◦ Also called signal value, dummy value, or flag value
◦ Indicates “end of data entry.”
◦ Loop ends when user inputs the sentinel value
◦ Sentinel value chosen so it cannot be confused with a
regular input (such as -1 in this case)

HCM International University - SCSE Section 4 – Programming Layer 25


Algorithms with Simple Variables
SCSE

Nested structures:
A structure in which one control structure is
embedded within another

HCM International University - SCSE Section 4 – Programming Layer 26


Algorithms with Simple Variables
SCSE

 Problem
◦ A college has a list of test results (1 = pass, 2 = fail)
for 10 students
◦ Write a program that analyzes the results
 If more than 8 students pass, print "Raise Tuition"
 Notice that
◦ The program must process 10 test results
 Counter-controlled loop will be used
◦ Two counters can be used
 One for number of passes, one for number of fails
◦ Each test result is a number—either a 1 or a 2
 If the number is not a 1, we assume that it is a 2

HCM International University - SCSE Section 4 – Programming Layer 27


Algorithms with Simple Variables
SCSE

 Top level outline


Analyze exam results and decide if tuition should be raised
 First Refinement
Initialize variables
Input the ten quiz grades and count passes and failures
Print a summary of the exam results and decide if tuition
should be raised
 Refine Initialize variables to
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one

HCM International University - SCSE Section 4 – Programming Layer 28


Algorithms with Simple Variables
SCSE

 Refine Input the ten quiz grades and count


passes and failures to
While student counter is less than or equal to ten
Input the next exam result
If the student passed
Add one to passes
else
Add one to failures
Add one to student counter
 Refine Print a summary of the exam results and
decide if tuition should be raised to
Print the number of passes
Print the number of failures
If more than eight students passed
Print “Raise tuition”

HCM International University - SCSE Section 4 – Programming Layer 29


Algorithms with Simple Variables
SCSE
PRACTICE

1. Write an algorithm to input a number and find its square root if it is


positive and power 2 in case of negative.

2. Write an algorithm to input a number and find its power 2 if it


belongs to [0,5], square root if it is greater than 5 and no change in
case of negative.

3. Write an algorithm to solve the first order equation.

4. Write an algorithm to calculate sum of all even numbers from 1 to n, n


is optional.

HCM International University - SCSE Section 4 – Programming Layer 30


C Program:
Basic concepts SCSE

Comments
Text surrounded by /* and */ is ignored by computer
Used to describe program

#include <stdio.h>
Preprocessor directive
Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations

HCM International University - SCSE Section 4 – Programming Layer 31


C Program:
Basic concepts SCSE

int main()

C++ programs contain one or more functions, exactly one of


which must be main

Parenthesis used to indicate a function

int means that main "returns" an integer value

Braces ({ and }) indicate a block: the bodies of all functions must


be contained in braces

HCM International University - SCSE Section 4 – Programming Layer 32


C Program:
Basic concepts SCSE

int integer1, integer2, sum;


Declaration of variables
Variables: locations in memory where a value can be stored
int means the variables can hold integers (-1, 3, 0, 47)
Variable names (identifiers)
integer1, integer2, sum
Identifiers: consist of letters, digits (cannot begin with a digit)
and underscores( _ )
Case sensitive
Declarations appear before executable statements
If an executable statement references and undeclared
variable it will produce a syntax (compiler) error

HCM International University - SCSE Section 4 – Programming Layer 33


C Program:
Basic concepts SCSE

printf( “Enter the first integer : \n" );


Instructs computer to perform an action
Specifically, prints the string of characters within quotes (“ ”)
Entire line called a statement
All statements must end with a semicolon (;)
Escape character (\)
Indicates that printf should do something out of the ordinary
\n is the newline character

HCM International University - SCSE Section 4 – Programming Layer 34


C Program:
Basic concepts SCSE

scanf( "%d", &integer1 );


Obtains a value from the user
scanf uses standard input (usually keyboard)
This scanf statement has two arguments
%d - indicates data should be a decimal integer
&integer1 - location in memory to store variable
& is confusing in beginning – for now, just remember to include
it with the variable name in scanf statements
When executing the program the user responds to the scanf
statement by typing in a number, then pressing the enter (return) key

HCM International University - SCSE Section 4 – Programming Layer 35


A Simple C Program:
Printing a Line of Text SCSE

1 /* Fig. 2.1: fig02_01.c


2 A first program in C */
3 #include <stdio.h>
4
5 int main()
6 {
7 printf( "Welcome to C!\n" );
8
9 return 0;
10 }

HCM International University - SCSE Section 4 – Programming Layer 36


A Simple C Program:
Addition program SCSE

1 /* Fig. 2.5: fig02_05.c


2 Addition program */
3 #include <stdio.h>
4
5 int main()
6 {
7 int integer1, integer2, sum; /* declaration */
8
9 printf( "Enter first integer\n" ); /* prompt */
10 scanf( "%d", &integer1 ); /* read an integer */
11 printf( "Enter second integer\n" ); /* prompt */
12 scanf( "%d", &integer2 ); /* read an integer */
13 sum = integer1 + integer2; /* assignment of sum */
14 printf( "Sum is %d\n", sum ); /* print sum */
15
16 return 0; /* indicate that program ended successfully */
17 }

HCM International University - SCSE Section 4 – Programming Layer 37


Programming
SCSE
PRACTICE

1. Develop a C program to input a number and find its square root if it


is positive and power 2 in case of negative.

2. Develop a C program to input a number and find its power 2 if it


belongs to [0,5], square root if it is greater than 5 and no change in
case of negative.

3. Develop a C program to solve the first order equation.

4. Develop a C program to calculate sum of all even numbers from 1


to n, n is optional.

HCM International University - SCSE Section 4 – Programming Layer 38


Algorithms with Composite Variables
SCSE

 ARRAYS
A collection of homogeneous items in which individual items
are accessed by their place within the collection (index)
Most programming languages start at index 0.
EX: if the array is called numbers, we access each value by
numbers[position]
Position is also the index.

HCM International University - SCSE Section 4 – Programming Layer 39


Algorithms with Composite Variables
SCSE

The algorithm to put values into the places in an array


integer numbers[10]
//Declare numbers to hold 10 integer values
Write “Enter 10 integer numbers, one per line”
Set position to 0 //Set variable position to 0
WHILE (position <10)
Read in numbers[position]
Set position to position + 1
//Continue with processing

HCM International University - SCSE Section 4 – Programming Layer 40


Algorithms with Composite Variables
SCSE

Algorithms with arays:


1. Searching
2. Sorting
3. Processing

HCM International University - SCSE Section 4 – Programming Layer 41


Sequential search: [0] 60 [0] 60

Read in array of values [1] 75 [1] 65

Write “Enter value for which to search” [2] 95 [2] SCSE


75
Read searchItem [3] 80 [3] 80
Set found to TRUE if searchItem is there [4] 65 [4] 90
IF (found) [5] 90 [5] 95
Write “Item is found” … … … …
ELSE
[l-1] .. [l-1] …
Write “Item is not found”
Unordered array Sorted array
Set found to TRUE if searchItem is there
Read in array of values: Set index to 0
Write “How many values?” Set found to FALSE
Read length WHILE (index<length AND NOT found)
Set index to 0 IF (data[index] equals searchItem)
WHILE (index < length) Set found to TRUE
Read data[index] ELSE IF (data[index]>searchItem)
Set index to index+1 Set index to length
ELSE
Set index to index+1
HCM International University - SCSE Section 4 – Programming Layer 42
[0] ant
Binary search:
[1] cat
Looking for an item in an already sorted list by SCSE
[2] chicken
eliminating large portions of the data on each [3] cow
comparison. [4] deer
Boolean Binary Search
[5] dog
Set first to 0
Set last to length-1 [6] fish
Set found to FALSE
[7] goat
WHILE (first<=last AND NOT found)
Set middle to (first+last)/2 [8] horse
IF (item equals data[middle])
[9] rat
Set found to TRUE
ELSE [10] snake
IF (item<data[middle]) ….
Set last to middle - 1
ELSE
Sorted list,
Set first to middle+1
length=11
Return found
HCM International University - SCSE Section 4 – Programming Layer 43
Searching for cat [0] ant
First Last Middle Comparison
[1] cat
0 10 5 cat<dog SCSE
[2] chicken
0 4 2 cat<chicken
0 1 0 cat>ant [3] cow

1 1 1 cat=cat Return: TRUE [4] deer


Searching for fish [5] dog
First Last Middle Comparison
[6] fish
0 10 5 fish>dog
6 10 8 fish<horse [7] goat

6 7 6 fish=fish Return: TRUE [8] horse

Searching for zebra [9] rat

First Last Middle Comparison [10] snake


0 10 5 zebra>dog ….
6 10 8 zebra>horse
9 10 9 zebra>rat Sorted list,
10 10 10 zebra>snake length=11
11 10 first>last Return: FALSE
Selection sort:
SCSE
[0] Sue [0] Ann [0] Ann [0] Ann [0] Ann
[1] Cora [1] Cora [1] Beth [1] Beth [1] Beth
[2] Beth [2] Beth [2] Cora [2] Cora [2] Cora
[3] Ann [3] Sue [3] Sue [3] Sue [3] June
[4] June [4] June [4] June [4] June [4] Sure

Selection sort
Set firstUnsorted to 0
WHILE (firstUnsorted < length-1)
Find smallest unsorted item
Swap firstUnsorted item with the smallest
Set firstUnsorted to firstUnsorted+1

HCM International University - SCSE Section 4 – Programming Layer 45


Selection sort (cont):
Find smallest unsorted item SCSE
Set indexOfSmallest to firstUnsorted
Set index to firstUnsorted+1
WHILE (index<=length-1)
IF (data[index]<data[indexOfSmallest])
Set indexOfSmallest to index
Set index to index+1

Swap firstUnsorted with the smallest


Set tempItem to data[firstUnsorted]
Set data[firstUnsorted] to data[indexOfSmallest]
Set data[indexOfSmallest] to tempItem

HCM International University - SCSE Section 4 – Programming Layer 46


Bubble sort:
Starting with the last array element, compare SCSE

successive pairs of elements, swapping them


whenever the bottom element of pair is smaller
than the one above it.
First iteration
[0] Phil [0] Phil [0] Phil [0] Phil [0] Al
[1] Al [1] Al [1] Al [1] Al [1] Phil

[2] John [2] John [2] Bob [2] Bob [2] Bob
[3] Jim [3] Bob [3] John [3] John [3] John
[4] Bob [4] Jim [4] Jim [4] Jim [4] Jim

Remaining iteration
[0] Al [0] Al [0] Al [0] Al
[1] Phil [1] Bob [1] Bob [1] Bob
[2] Bob [2] Phil [2] Jim [2] Jim
[3] John [3] Jim [3] Phil [3] John
[4] Jim [4] John [4] John [4] Phil
47
Bubble sort (cont):
Bubble sort SCSE
Set firstUnsorted to 0
Set swap to TRUE
WHILE (firstUnsorted <length-1 AND swap)
Set swap to FALSE
“Bubble up” the smallest item in unsorted part
Set firstUnsorted to firstUnsorted+1

Bubble up
Set index to lenghth-1
WHILE (index>firstUnsorted+1)
IF (data[index]<data[index-1])
Swap data[index] and data[index-1]
Set swap to TRUE
Set index to index-1
Insertion sort:
[0] Al [0] Al [0] AlSCSE
[0] Phil [0] John
[1] John [1] Jim [1] Bob
[1] John [1] Phil
[2] Phil [2] John [2] Jim
[2] Al [2] Al
[3] Jim [3] Phil [3] John
[3] Jim [3] Jim
[4] Bob [4] Bob [4] Phil
[4] Bob [4] Bob

Insertion sort
Set current to 1 // current is the item being inserted into the sorted portion
WHILE (current < length)
Set index to current
Set placeFound to FALSE
WHILE (index>0 AND NOT placeFound)
IF (data[index] < data[index-1])
Swap data[index] and data[index-1]
Set index to index-1
ELSE
Set placeFound to TRUE
Set current to current+1
Recursive algorithms
SCSE

Recursion:
The ability of an algorithm to call itself
 Recursive factorial
 Recursive binary search

HCM International University - SCSE Section 4 – Programming Layer 50


Recursive factorial
SCSE
Factiorial of N:
N!=1.2.3.4.5.6…..N=N*(N-1)!
Factorial of 0 is 1.

Recursive factorial Factorial(N)

Write “Enter N” IF (N equals to 0)

Read N RETURN

Set result to Factorial(N) ELSE

Write result + “is the factorial of” +N RETURN N*Factorial(N)

HCM International University - SCSE Section 4 – Programming Layer 51


Recursive Binary Search
SCSE

BinarySearch (first, last)


IF (first>last)
RETURN FALSE
ELSE
Set middle to (first+last)/2
IF (item equals data[middle])
RETURN TRUE
ELSE
IF (item<data[middle]
BinarySearch (first, middle-1)
ELSE
BinarySearch(middle+1, last)

HCM International University - SCSE Section 4 – Programming Layer 52


Algorithms with Composite Variables
SCSE

 RECORDS
A named heterogeneous groups of items in which
individual items are accessed by name.
“Heterogeneous”: elements in the collection do not have
to be the same.
Collections: integers, real values, strings, other types of
data.

HCM International University - SCSE Section 4 – Programming Layer 53


RECORDS
Employee SCSE
Name
Age
hourlyWage

Store values into the fields of the record


Employee employee //Declare an Employee variable
Set employee.name to “Nguyen Van A”
Set employee.age to 32
Set employee.hourlyWage to 27.50

HCM International University - SCSE Section 4 – Programming Layer 54


A General Example
SCSE

 Planning a large party

HCM International University - SCSE Section 4 – Programming Layer 55


A Computer Example
SCSE

 Problem
 Create an address list that includes each person’s
name, address, telephone number, and e-mail
address
 This list should then be printed in alphabetical order
 The names to be included in the list are on scraps
of paper and business cards

HCM International University - SCSE Section 4 – Programming Layer 56


A Computer Example
SCSE

HCM International University - SCSE Section 4 – Programming Layer 57


A Computer Example
SCSE

HCM International University - SCSE Section 4 – Programming Layer 58


A Computer Example
SCSE

HCM International University - SCSE Section 4 – Programming Layer 59


A Computer Example
SCSE

HCM International University - SCSE Section 4 – Programming Layer 60


Testing the Algorithm
SCSE

 The process itself must be tested


 Testing at the algorithm development phase
involves looking at each level of the top-down
design

HCM International University - SCSE Section 4 – Programming Layer 61


Testing the Algorithm
SCSE

Desk checking Working through a design at a desk


with a pencil and paper
Walk-through Manual simulation of the design by the
team members, taking sample data values and
simulating the design using the sample data
Inspection One person (not the designer) reads the
design (handed out in advance) line by line while the
others point out errors

HCM International University - SCSE Section 4 – Programming Layer 62


Object-Oriented Design SCSE

 A problem-solving methodology that produces a


solution to a problem in terms of self-contained entities
called objects
Object A thing or entity that makes sense within the
context of the problem
For example, a student

HCM International University - SCSE Section 4 – Programming Layer 63


Object-Oriented Design SCSE

 A group of similar objects is described by an object


class, or class
 A class contains fields that represent the properties
and behaviors of the class
 A field can contain data value(s) and/or methods
(subprograms)
 A method is a named algorithm that manipulates
the data values in the object

HCM International University - SCSE Section 4 – Programming Layer 64


Relationships Between Classes SCSE

 Containment
 “part-of”
 An address class may be part of the definition of a
student class
 Inheritance
 Classes can inherit data and behavior from other
classes
 “is-a”

HCM International University - SCSE Section 4 – Programming Layer 65


Object-Oriented Design Methodology SCSE

 Four stages to the decomposition process


 Brainstorming
 Filtering
 Scenarios
 Responsibility algorithms

HCM International University - SCSE Section 4 – Programming Layer 66


CRC Cards SCSE

HCM International University - SCSE Section 4 – Programming Layer 67


Brainstorming SCSE

 A group problem-solving technique that involves the


spontaneous contribution of ideas from all members of
the group
 All ideas are potential good ideas
 Think fast and furiously first, and ponder later
 A little humor can be a powerful force
 Brainstorming is designed to produce a list of
candidate classes

HCM International University - SCSE Section 4 – Programming Layer 68


Filtering SCSE

 Determine which are the core classes in the problem


solution
 There may be two classes in the list that have many
common attributes and behaviors
 There may be classes that really don’t belong in the
problem solution

HCM International University - SCSE Section 4 – Programming Layer 69


Scenarios SCSE

 Assign responsibilities to each class


 There are two types of responsibilities
 What a class must know about itself (knowledge
responsibilities)
 What a class must be able to do (behavior
responsibilities)

HCM International University - SCSE Section 4 – Programming Layer 70


Scenarios SCSE

 Each class encapsulates its data but shares their


values through knowledge responsibilities.

Encapsulation is the bundling of data and actions in


such a way that the logical properties of the data and
actions are separated from the implementation details

HCM International University - SCSE Section 4 – Programming Layer 71


Responsibility Algorithms SCSE

 The algorithms must be written for the responsibilities

 Knowledge responsibilities usually just return the


contents of one of an object’s variables

 Action responsibilities are a little more complicated,


often involving calculations

HCM International University - SCSE Section 4 – Programming Layer 72


Computer Example SCSE

 Let’s repeat the problem-solving process for creating


an address list
 Brainstorming and filtering
 Circling the nouns and underlining the verbs

HCM International University - SCSE Section 4 – Programming Layer 73


Computer Example SCSE

 First pass at a list of


classes

HCM International University - SCSE Section 4 – Programming Layer 6-37


74
Computer Example SCSE

 Filtered list

HCM International University - SCSE Section 4 – Programming Layer 6-38


75
CRC Cards SCSE

HCM International University - SCSE Section 4 – Programming Layer 76


Responsibility Algorithms SCSE

HCM International University - SCSE Section 4 – Programming Layer 77


Information Hiding/Abstraction
SCSE

 Information Hiding and Abstraction are


two sides of the same coin.
Information Hiding The practice of hiding the
details of a module with the goal of controlling
access to the details of the module.
Abstraction A model of a complex system that
includes only the details essential to the viewer.

HCM International University - SCSE Section 4 – Programming Layer 78


Information Hiding/Abstraction
SCSE

 Abstraction is the result with the details


hidden
Data abstraction Separation of the logical view of
data from their implementation.
Procedural abstraction Separation of the logical
view of actions from their implementation.
Control abstraction Separation of the logical
view of a control structure from its
implementation.

HCM International University - SCSE Section 4 – Programming Layer 79


Programming Languages SCSE

 Instructions written in a programming language can


be translated into the instructions that a computer can
execute directly
Program A meaningful sequence of instructions for a
computer
Syntax The part that says how the instructions
of the language can be put together
Semantics The part that says what the instructions mean

HCM International University - SCSE Section 4 – Programming Layer 80


Review SCSE

Describe the computer problem-solving process.


Distinguish between a simple type and a composite type
Simple C programs
Describe three composite data-structuring mechanisms
Recognize a recursive problem and write a recursive
algorithm to solve it
Distinguish between an unsorted array and a sorted
array

HCM International University - SCSE Section 4 – Programming Layer 81


Review SCSE

Distinguish between a selection & an insertion sort


Describe Quicksort algorithm
Apply the selection sort, the bubble sort, insertion sort, and
Quicksort to an array of items by hand
Apply the binary search algorithm
Demonstrate your understanding of the algorithms in this
chapter by hand-simulating them with a sequence of items

HCM International University - SCSE Section 4 – Programming Layer 82

You might also like