NUS CS101 - Lecture 1
NUS CS101 - Lecture 1
NUS CS101 - Lecture 1
http://www.comp.nus.edu.sg/~cs1101
Heng Aik Koan
Office : s15-05-13
email: [email protected]
1
Lecture
Notes
2
Text Book
3
Important
4
Chapter 1: Program Development
CONTENTS:
5
1.0. Introduction
6
q Although the problems themselves and
specific techniques used in their
solutions vary, there are several phases
or steps that are common in the
software development process:
7
q In this chapter we begin by describing
and illustrating the first four steps of
this software life cycle, using three
problems, each of which can be solved
with a simple program.
8
1.1. Problem Analysis and
Specification
9
q This specification must include:
11
INPUT:
Basic service charge: $25.00
Unit cable cost: $2.00
Number of installations: 27
Yards of cable: 263
OUTPUT:
Revenue generated
12
q One important aspect of problem
analysis is generalization.
13
INPUT:
Basic service charge
Unit cable cost
Number of installations
Yards of cable:
OUTPUT:
Revenue generated
14
Problem 2: Pollution indices
OUTPUT:
Pollution index
Condition: safe or hazardous
16
Problem 3: Mean Time to failure
17
INPUT
A collection of numeric values
(number unknown)
OUTPUT
The number of values
The mean of the values
18
1.2. Design
20
q Structured algorithms and programs are
designed using three basic methods of
controls:
22
It is therefore natural to describe
algorithms in a language that
resembles that used to write computer
programs. That is, in a
pseudoprogramming language or
pseudocode
23
Pseudocode is a mixture of natural
language and symbols, terms and
other features commonly used in
high-level languages.
24
Algorithm for Revenue Calculation
Sequential Structure
(* Input : A basic ServiceCharge,
a UnitCost for cable,
number of Installation, and
YardsOfCable used.
Purpose: This algorithm calculates the
revenue generated by the
installation of a certain number of
yards of cable at a number of
locations. For each installation
there is a fixed basic service
charge and an additional charge for
each foot of cable.
Output: Revenue generated. *) 25
1. Enter ServiceCharge, UnitCost,
Installation,and YardsOfCable.
2. Calculate
FeetOfCable = 3 * YardsOfCable
3. Calculate
Revenue= Installation * ServiceCharge
+ UnitCost * FeetOfCable
4. Display Revenue
26
Algorithm for Pollution Index
problem : Selection Structure
(* Input : Three pollution levels and
a cutoff value.
28
Algorithm to Calculate Mean Time to
Failure Repetition Structure
29
1. Initialize NumTimes to 0 and Sum to
0.0.
2. Enter the first value for FailTime.
3. While FailTime is not the end-of-data-
flag, do the following:
a. Increment NumTimes by 1.
b. Add FailTime to Sum.
c. Enter next value for FailTime.
4. If NumTimes 0 then
a. Calculate
MeanFailTime=Sum/NumTimes
b. Display MeanFailTime and
NumTimes.
else
display a No Data message.
30
1.3. Coding
31
For Example, in Java:
Variables
Type
Operations
Input/Output
Comments
Program Composition
32
import java.io.*;
34
// perform the calculations
feetOfCable = 3 * yardsOfCable;
revenue = serviceCharge * installations
+ unitCost * feetOfCable;
}
}
35
import java.io.*;
36
class Failure {
static final int end_of_data = 999;
BufferedReader stdin =
new BufferedReader
(new InputStreamReader(System.in));
String my_string;
int FailTime;
double Sum = 0.0;
int NumTimes = 0;
double MeanFailTime;
37
System.out.print ("enter first value ");
System.out.print (" of fail time: ");
System.out.flush();
my_string = stdin.readLine();
FailTime = Integer.parseInt (my_string);
if (NumTimes != 0)
{ MeanFailTime = Sum / NumTimes;
System.out.println ("The number of +
failure times = " +
NumTimes);
System.out.println ("The mean of " +
" failure = " +
MeanFailTime);
}
else
System.out.println("No Data Input");
}
}
39
import java.io.*;
40
class Pollution {
41
// Input three pollution levels
42
System.out.print("Pollution Level 3 : ");
System.out.flush( );
my_string = stdin.readLine( );
Level3 = Integer.parseInt (my_string);
43
// To determine the index
44
1.4. Verification and Validation
45
q The detection and correction of errors
is an important part of software
development and is known as validation
and verification.
Validation is concerned with checking
that the algorithms and the programs
meet the problems specification.
Verification refers to checking that
they are correct and complete.
46
q The three types of errors are Syntax
error or Compilation error, Run-Time
error and Logical error.
47
q Run-time errors occur during program
execution. They cause the program to
terminate abnormally. E.g, division by
zero will cause a run-time error.
q When you have a logical error, the
program compiles and executes
without complaint, but it produces
incorrect results. E.g. A + B is typed in
as A * B.
48
q Errors that are detected by the
computer system are relatively easy to
identify and correct, but not logical
errors.
49
1.5. Software Engineering
51
Problem Analysis and Specification
52
E.g. Computerize the payroll system.
54
Many other questions must be
answered before the specification of
the problem is complete and the design
of the algorithms and programs can
begin:
56
q Top-down approach
The top-down approach to software
development allows the programmer to
design and test an algorithm and the
corresponding program module for
each subproblem independently of the
others.
57
Payroll System
Obtain
Obtain perform
Perform display
Display
input
input calculations
calculation result
results
data
data
58
Second Level Structure Diagrams
Obtain
Input
data
59
Perform
Calculation
60
Display
Result
61
Third Level Strucure Diagrams
Calculate
Gross Pay
Salaried Hourly
62
Program Coding
63
q Integration is the process of combining
a number of subprograms into a
complete program or system of
programs.
64
One principle is that program should
be well structured. There are two
guidelines:
1. Use a top-down approach when
developing a program for a
complex problem.
65
A second principle is that each
program unit should be documented.
In particular
1. Each program unit should include
opening documentation.
69
Program Execution and Testing
Statement of Final
Specification System
Designing System
the system testing
System Integrated
plan modules
71
Unit testing
Maintenance
72