2023 Lecture Note On CSC102a
2023 Lecture Note On CSC102a
This course introduces the student to a way of solving problems using computer program. It
will let them understand what “problem” is in this context and also know different strategies
that can be used for generating potential solutions to ''real-life'' problems.
1
Introduction
The Introduction to Programming course is a course that will give you a basic framework for
problem-solving using computer. Students will learn how to identify problems, define
problems, come up with a list of possible solutions, develop effective algorithms, model the
solution using Unified Modeling Language (UML) and also know how to write a good
computer program to solve the problems.
Problem
One of the creative thinker's fundamental insights is that most questions have more than one
right answer and most problems have more than one solution. In keeping with this insight, we
will offer more than one definition of a problem, in hopes of filling out its meaning as fully as
possible. Different definitions yield different attitudes and approaches.
Developing a positive attitude toward problems can transform you into a happier, saner, more
confident person who feels (and is) much more in control of life. Train yourself to respond to
problems with enthusiasm and eagerness, rising to the opportunity to show your stuff, and you
will be amazed at the result.
2. A problem is the difference between your current state and your goal state. A
problem can result from new knowledge or thinking. When you know where you are and
2
where you want to be, you have a problem to solve in getting to your destination. The solution
can and should be fun and exciting as you think over the various possible solution paths you
might choose. When you can identify the difference between what you have and what you
want, you have defined your problem and can aim toward your goal.
3. A problem results from the recognition of a present imperfect and the belief in the
possibility of a better future. Isn't it interesting here that hope produces problems? The belief
that your hopes can be achieved will give you the will to aim toward the better future. Your
hopes challenge you, and challenge is another definition of a problem.
Types of Problems
Well-defined Problems: Well-defined problem is one that has a clear goal, a specific path to
the solution and clearly visible obstacles based on the information given. For example,
calculating the sales tax and total cost of an item for purchase is a simple, clearly defined
process:
Ill-defined Problems: Ill-defined problems are not clear-cut. There is no obvious path to
the solution. These problems require investigation to define, understand and solve. No
simple formula can be used to solve an ill-defined problem. The problem solver must
gather and analyze information in order to find a solution. But ill-defined problems may
include sub-problems that are well-defined. So the overall solution may require a
combination of strategies.
Illustration
A lot of numeric types of word problems revolve around translating English statements into
mathematical form.
3
The following four steps will be followed in solving the given problem
Example 1: Twice the difference of a number and 1 is 4 more than that number. Find the
number.
Make sure that you read the question carefully several times.
x = a number
2(x-1) = x+4
4
Step 4: Look back (check and interpret).
If you take twice the difference of 6 and 1, that is the same as 4 more than 6, so this does
check.
FINAL ANSWER:
The number is 6.
Solution
In our ordinary discourse, we often think of "solving a problem" in the sense of making it go
away, so that the problem no longer exists. Some problems cannot be eliminated entirely: we
are never likely to eliminate trash, or the wear on automobile tires, or the occurrence of illness.
We can, however, create solutions or treatments that will make each of these problems less
harmful.
A solution is defined as the management of a problem in a way that successfully meets the
goals established for treating it. Sometimes the goal will be to eliminate the problem entirely;
sometimes the goal will be only to treat the effects of the problem.
Our ability to properly handle problems can lead to a life which is rich in success and low in
stress, failure, or anxiety. With current understanding, problem-solving is more of an art than a
science. There are no universally accepted methods which can be used to help people solve
problems.
Problem-Solving
The way in which people solve problems best depends largely on the unique situation. In some
cases, people are better off learning everything they can about the issue and then using factual
knowledge to come up with a solution. In other cases, creativity and insight are the best
options. One has to develop a good and sound skill for problem solving.
Computer
A computer is an electronic device that accepts data, as an input, processes the data and gives
out the result as information (Figure 1). It is a technology that is invented to solve problems.
The computer cannot do anything without a program. A program is a list of detailed
instructions that the computers carries out. The program is the driving force behind any job
that a computer does. It is the job of the programmer to design and write programs that directs
the computer to take raw data and transform that data into meaningful information. The end-
user (usually just called the user) of the computer is generally the nontechnical, non-
programming person who needs the results (information) that the program provides.
Storage
Computer works under program control, performs arithmetic and logical operations without
human intervention. To process data, a computer must be provided with two needs namely
program and data, as shown in figure 1. Without these two ingredients, a computer cannot
function.
6
Computer Program
Computers are involved in a variety of tasks that we do in our everyday lives. Some of these
tasks such as using a word processor or checking e-mail obviously use a computer. Less direct
examples occur when we use an ATM at a bank, pay at a supermarket checkout or use a phone.
A computer performs all of these tasks by following a predefined set of instructions. This set
of instructions is called a computer program. A computer program to a computer is like a
recipe to a chef; it specifies the steps needed to perform a certain task. But unfortunately,
unlike a recipe, you cannot give your instructions to a computer in plain English. For
instructions to be “intelligible” to a computer, they need to be expressed in a language
“understood” by the computer. The only language understood by a computer is its own
machine language, which consists of a series of binary ones and zeroes. Machine language is
very difficult to use directly and so instructions to a computer are given in a special language
called a programming language. The programming language is neither English nor machine
language, but somewhere in between. In fact, it is more like English than machine language.
Machine languages are known as low-level languages and programming languages are known
as high-level languages. Writing instructions in a high-level language is much easier than
writing them in low-level machine language, but is still not as easy as writing them in English.
For a computer to carry out the program instructions written in a high-level language, they
have to be translated from the high-level language to the machine language of the computer. A
compiler does this translation.
PROGRAMMIING LANGUAGES
Computers only comprehend binary digits, 0’s and 1’s. Therefore, writing instructions for a
computer to perform requires sending the CPU and peripheral devices a sequence of 0’s and
1’s in the pre-determined sequence (figure 2) that will cause the computer to perform what
tasks is required of it. 0’s and 1’s mean very little to most human beings, and putting together a
correct sequence of 0001101011 01101011011 is problematic because it is very easy to make a
mistake. A mistake of putting a 1 where a 0 should be is minor to a human but very
important/significant for computers. The result of this binary complications, led to the
development of a Programming languages.
7
To understand computer software, you need a basic knowledge of the role that programming
languages play in the development of computer programs. A programming language allows a
programmer to develop the sets of instructions that constitute a computer program. Many
different programming languages have been developed, each with its own unique vocabulary,
grammar, and uses.
There are some conventional features which a programming language must possess, these
features are:
- It must have a language structure, which consists of keywords, expressions and statements.
- Programming languages are written and processed by the computer for the purpose of
communicating data between the human being and the computer.
Translators
A translator is a program that translates another program written in any programming language
other than the machine language to an understandable set of codes for the computer and in so
8
doing produces a program that may be executed on the computer. The need for a translator
arises because only a program that is directly executable on a computer is the machine
language.
Translators exist to make programs understandable by the computer. There are different
translators for different levels and types of programming languages. Without the translator, the
programs cannot be executed.
All programs must be translated before their instructions can be executed. Computer languages
can be grouped according to which translation process is used to convert the instructions into
binary code:
• Assemblers
• Interpreters
• Compilers
2. Interpreters and Compilers: These consist of programs that convert programs in high level
programming language into machine language. The major difference between interpreters and
compilers is that a compiler converts the entire source program into object code before the
entire program is executed while the interpreter translates the source instructions line by line.
In the former, the computer immediately executes one instruction before translating the next
instruction.
Programming Process
Programming Process consists of series of steps needed to accomplish the programmers’ goal
of producing programs that are reliable, portable, readable and easily maintainable. These steps
are categorized into two phases; the Problem-solving and Implementation. The first phase is
carried out by the programmer while the second phase is carried out by the computer. It is only
9
after the programmer is satisfied that the solution he has arrived at truly solves the problem. It
is only then that he converts the solution into a form that the computer can follow and execute.
We will familiarize ourselves with some of the common strategies and techniques that we can
employ in solving problems using computer.
Analysis
General Solution
Test
Use
Problem solving phase is the first of the programming process and it consists of three ordered
steps:
10
Analysis
This is the first step of problem solving phase. This involves the definition of the problem. It is
at this stage that we have to understand clearly what the problem is. We are required to write a
narrative of what the problem is, that is, define the problem. Within this narrative, we
determine what data is to be input, what information is to be output, and any assumptions that
are made. We also create a functional problem description.
A functional problem description is a description that clearly states what the problem is. In
many cases, creating a functional problem description requires further dialogue between the
person with the problem and the programmer. For example, to create a functional problem
description for a problem that returns the sum of two numbers, we may need to dialogue with
the originator of the problem so as to determine if the numbers will always be positive or mix –
positive and negative, or if the numbers will always be whole numbers or fractional numbers.
General solution
When we are sure we have a definition of the problem, we then look for the solution. The
second step requires us to use of the problem solving strategies to solve the problem. The
strategy or approach used will vary depending upon the situation and unique preferences of the
individual.
The general solution answers the question of “how” the “what” is to be solved.
After a solution has been reached, it is important to evaluate the results to determine if it is the
best possible solution to the problem. In the third and last step in the problem solving phase,
we test our solution to look for any mistakes / errors. Testing the solution is to make certain
that the “how – solution” answers the “what – problem” correctly. In order to verify that our
solution represents the correct solution to the problem, it is necessary the solution using
suitable test data.
The test data must be chosen to cover all possibilities of finding errors. We must include both
good and bad data. An example of bad data is the inclusion of letters where numbers are
expected. Testing is the act of looking for errors in a proposed solution to a problem, therefore
11
there is need to invent simple test data set to check for errors. The reason for the choice of
simple test data set is to easily confirm that the observed result conforms to the expected result.
When we have to test using numerical data, then these data should be chosen for ease of
calculation. For example, expected result of 3 + 3 is 6; therefore the observed result should not
be (say) 9.
A great deal of effort should go into creating the test data sets. It is only after we are satisfied
that we have a workable test data, then and only then will it be time to use the test data to
follow the exact sequence of steps as outlined to see if the solution truly solves the problem.
Test of general solution is necessary so as to remove mistakes before the solution is translated
to specific solution – the first step in the implementation phase.
1. Identifying the Problem: While it may seem like an obvious step, identifying the
problem is not always as simple as it sounds. In some cases, people might mistakenly
identify the wrong source of a problem, which will make attempts to solve it inefficient or
even useless.
2. Defining the Problem: After the problem has been identified, it is important to fully
define the problem so that it can be solved.
3. Forming a Strategy: The next step is to develop a strategy to solve the problem. The
approach used will vary depending upon the situation and the unique preferences of the
individual. Problem solving is a set of activities designed to analyze a situation
systematically and generate, implement, and evaluate solution. A problem-solving strategy
is a plan of action used to find a solution. Different strategies have different action plans
associated with them.
5. Allocating Resources: Of course, we don't always have unlimited money, time and
other resources to solve a problem. Before you begin to solve a problem, you need to
12
determine how high priority it is. If it is an important problem, it is probably worth
allocating more resources to solving it. If, however, it is a fairly unimportant problem,
then you do not want to spend too much of your available resources into coming up with a
solution.
7. Evaluating the Results: After a solution has been reached, it is important to evaluate
the results to determine if it is the best possible solution to the problem. This evaluation
might be immediate, such as checking the results of a math problem to ensure the answer
is correct.
Requirements Analysis
The analysis for the system requirements are carried out to:
• to determine the data requirements of the Information system in terms of primitive objects.
• to determine the types of transactions (operations) that will be executed on the system and the
interactions between the data and the transactions.
The System analyst works with the end users of the system to determine the basic requirements
of the system. Information needed for the requirements analysis can be gathered in several
ways:
• review of existing documents - such documents include existing forms and reports,
written guidelines, job descriptions, personal narratives, and memoranda.
• interviews with end users - these can be a combination of individual or group meetings.
13
The requirements analysis is usually done at the same time as the data modeling. As
information is collected, data objects are identified and classified as entities, attributes, or
relationship; assigned names; and, defined using terms familiar to the end-users. The objects
are then modeled and analyzed using UML diagrams. The diagrams can be reviewed by the
modeler and the end-users to determine its completeness and accuracy. If the model is not
correct, it is modified, which sometimes requires additional information to be collected. The
review and edit cycle continues until the model is certified as correct.
Algorithm
Definitions of an Algorithm
Not every problem or task has a “good” algorithmic solution. There are
1. Unsolvable problems - no algorithm can exist to solve the problem (Halting Problem).
2. Hard (intractable) problems - algorithm takes too long to solve the problem.
14
3. Problems with no known algorithmic solution.
As shown in the above figure, after a problem has been identified, the problem is then carefully
analyzed in order to present a suitable algorithm. An algorithm is then designed and presented
to the computer in a particular programming language. The computer will then generate the
output, based on the input. An algorithm helps in the correctness and efficiency of a program.
Properties of an Algorithm
2. Definiteness: Each step in algorithm is unambiguous. This means that the action
specified by the step cannot be interpreted (explain the meaning of) in multiple ways
and can be performed without any confusion.
5. Effectiveness: It consists of basic instructions that are reliable. This means that the
instructions can be performed by using the given inputs in a finite amount of time.
15
How to devise an algorithm
The process of devising an algorithm is both an art and a science. This is one part that cannot
be automated fully. Given a problem description, one have to think of converting this into a
series of steps, which, when executed in a given sequence solve the problem. To do this, one
has to be familiar with the problem domain and also the computer domains. Most often, when
given a problem description, how a person proceeds to convert it into an algorithm becomes a
matter of his "style" - no firm rules become applicable here.
For the purpose of clarity in understanding, let us consider the following examples.
Example 1:
Steps :
1. Let the value of the first be the largest value denoted by BIG
3. If R not equal to 0 then it is implied that the list is still not exhausted. Therefore look
the next number called NEW.
5. If NEW is greater than BIG then replace BIG by the value of NEW
7. Print BIG
8. Stop
16
Example 2: Let us try to present the scenario of a man brushing his own shoes as an algorithm
as follows:
Steps:
1. Dust the shoe
2. Take the brush
3. Apply the polish
4. Start brushing
5. Stop
Example 3:
Problem: The task of computing the final price of an item after adding the sales tax.
Steps:
6. Stop
Example 4: Computing Weekly Wages: Gross pay depends on the pay rate and the number of
hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-
half for all hours worked over 40.
Steps:
3. Compute gross pay if hours worked ≤ 40, gross pay = pay rate x hours worked
4. Compute gross pay if hours worked > 40, gross pay = pay rate x 40 + 0.5 x pay rate x
(hours worked – 40)
17
5. Display gross pay
6. Stop
Once an algorithm has been devised, it becomes necessary to show that it works. i.e it
computes the correct answer to all possible, legal inputs. One simple way is to code it into a
program. However, converting the algorithms into programs is a time consuming process.
Hence, it is essential to be reasonably sure about the effectiveness of the algorithm before it is
coded. This process, at the algorithm level, is called "validation". Several mathematical and
other empirical methods of validation are available. Providing the validation of an algorithm is
a fairly complex process and most often a complete theoretical validation, though desirable,
may not be provided. Alternately, algorithm segments (e.g searching algorithms, sorting
algorithms, Euclidean algorithm, etc), which have been proved elsewhere, may be used and the
overall working algorithm may be empirically validated for several test cases.
If there are more than one possible ways of solving a problem, then one may think of more
than one algorithm for the same problem. Hence, it is necessary to know in what domains these
algorithms are applicable. Data domain is an important aspect to be known in the field of
algorithms. Once we have more than one algorithm for a given problem, how do we choose the
best among them? The solution is to devise some data sets and determine a performance profile
for each of the algorithms. A best case data set can be obtained by having all distinct data in
the set.
IMPLEMENTATION STRATEGIES
These can also be referred to as the programming aids in problem solving. Programmers use
different kinds of tools or aids which help them in developing programs faster and
b e t t e r . Such a i d s ar e studied in this topic. They help in constructing programs very
fast and easily. Some of the strategies are:
1. Algorithms
3. Programming
18
Object Oriented Modeling
UML is a standard language for specifying, visualizing, constructing, and documenting the
artifacts of software systems.
UML was created by the Object Management Group (OMG) and UML 1.0 specification draft
was proposed to the OMG in January 1997.
• UML is different from the other common programming languages such as C++, Java,
COBOL, Visual Basic Dot Net etc.
• Although UML is generally used to model software systems, it is not limited within
this boundary. It is also used to model non-software systems as well. For example, the
process flow in a manufacturing unit, etc.
UML is not a programming language but tools can be used to generate code in various
languages using UML diagrams. UML has a direct relation with Object Oriented Analysis and
Design. After some standardization, UML has become an OMG standard.
Goals of UML
Object-oriented concepts were introduced much earlier than UML. At that point of time, there
were no standard methodologies to organize and consolidate the object-oriented development.
It was then that UML came into picture.
There are a number of goals for developing UML but the most important is to define some
general purpose modeling language, which all modelers can use and it also needs to be made
simple to understand and use.
19
UML diagrams are not only made for developers but also for business users, common people,
and anybody interested to understand the system. The system can be a software or non-
software system. Thus it must be clear that UML is not a development method rather it
accompanies with processes to make it a successful system.
In conclusion, the goal of UML can be defined as a simple modeling mechanism to model all
possible practical systems in today’s complex environment.
Object-Oriented Concepts
UML can be described as the successor of Object-Oriented (OO) Analysis and Design.
An object contains both data and methods that control the data. The data represents the state
of the object. A class describes an object and they also form a hierarchy to model the real-
world system. The hierarchy is represented as inheritance and the classes can also be
associated in different ways as per the requirement.
Objects are the real-world entities that exist around us and the basic concepts such as
abstraction, encapsulation, inheritance, and polymorphism all can be represented using UML.
UML is powerful enough to represent all the concepts that exist in object-oriented analysis
and design.
Thus, it is important to understand the OO analysis and design concepts. The most important
purpose of OO analysis is to identify objects of a system to be designed. This analysis is also
done for an existing system. Now an efficient analysis is only possible when we are able to
start thinking in a way where objects can be identified. After identifying the objects, their
relationships are identified and finally the design is produced.
20
There are three basic steps where the OO concepts are applied and implemented. The steps
can be defined as
• During OO analysis, the most important purpose is to identify objects and describe
them in a proper way. If these objects are identified efficiently, then the next job of
design is easy. The objects should be identified with responsibilities. Responsibilities
are the functions performed by the object. Each and every object has some type of
responsibilities to be performed. When these responsibilities are collaborated, the
purpose of the system is fulfilled.
• The second phase is OO design. During this phase, emphasis is placed on the
requirements and their fulfilment. In this stage, the objects are collaborated according
to their intended association. After the association is complete, the design is also
complete.
• The third phase is OO implementation. In this phase, the design is implemented using
OO languages such as Java, C++, Visual Basic etc.
UML is a modeling language used to model software and non-software systems. Although
UML is used for non-software systems, the emphasis is on modeling OO software
applications. Most of the UML diagrams discussed so far are used to model different aspects
such as static, dynamic, etc. Now whatever be the aspect, the artifacts are nothing but objects.
If we look into class diagram, object diagram, collaboration diagram, interaction diagrams all
would basically be designed based on the objects.
Hence, the relation between OO design and UML is very important to understand. The OO
design is transformed into UML diagrams according to the requirement.
Purposes of UML
21
Types of UML Diagrams
UML is a visual language for developing software blueprints (designs). A blueprint or design
represents the model. For example, while constructing buildings, a designer or architect
develops the building blueprints. Similarly, we can also develop blueprints for a software
system. UML is the most commonly and frequently used language for building software
system blueprints. This visual language represents a language in which we can draw things to
represent the knowledge.
The current UML standards call for 13 different types of diagrams: Class, Activity, Object, Use
Case, Sequence, Package, State, Component, Communication, Composite structure, Interaction
Overview, Timing, and Deployment.
These diagrams are organized into two distinct groups: Structural diagrams and Behavioral or
Interaction diagrams.
Structural UML diagrams: Structural UML diagrams represent the framework for the system
and this framework is the place where all other components exist. These are:
• Class diagram
• Package diagram
• Object diagram
• Component diagram
• Deployment diagram
Behavioral UML diagrams: Behavioral UML diagrams describe the interaction in the system.
They represent the interaction among the structural diagrams. Behavioral UML diagrams show
the dynamic nature of the system. They consist of the following −
• Activity diagram
• Sequence diagram
• State diagram
• Communication diagram
22
• Interaction overview diagram
• Timing diagram
CLASS DIAGRAM
Class diagrams are the main building block of any object-oriented solution. It shows the classes
in a system, attributes, and operations of each class and the relationship between each class.
In most modeling tools, a class has three parts. Name at the top, attributes in the middle and
operations or methods at the bottom. In a large system with many related classes, classes are
grouped together to create class diagrams. Different relationships between classes are shown
by different types of arrows.
23
Class diagrams are the most common diagrams used in UML. Class diagram consists of
classes, interfaces, associations, and collaboration. Class diagrams basically represent the
object-oriented view of a system, which is static in nature.
Class diagram represents the object orientation of a system. Hence, it is generally used for
development purpose. This is the most widely used diagram at the time of system
construction.
ACTIVITY DIAGRAM
Activity diagrams represent workflows in a graphical way. They can be used to describe the
business workflow or the operational workflow of any component in a system.
Sometimes activity diagrams are used as an alternative to State machine diagrams.
Activity diagrams are mainly used as a flowchart that consists of activities performed by the
system. Activity diagrams are not exactly flowcharts as they have some additional
capabilities. These additional capabilities include branching, parallel flow, etc.
Before drawing an activity diagram, we must have a clear understanding about the elements
used in activity diagram. The main element of an activity diagram is the activity itself. An
activity is a function performed by the system. After identifying the activities, we need to
understand how they are associated with constraints and conditions.
• Activities
• Association
• Conditions
• Constraints
Once the above-mentioned parameters are identified, we need to make a mental layout of the
entire flow. This mental layout is then transformed into an activity diagram.
Figure 6 is an example of an activity diagram for order management system. In figure 6, four
activities are identified which are associated with conditions. One important point should be
clearly understood that an activity diagram cannot be exactly matched with the code. The
24
activity diagram is made to understand the flow of activities and is mainly used by the
business users.
The Activity diagram (figure 6) is drawn with the four main activities −
After receiving the order request, condition checks are performed to check if it is normal or
special order. After the type of order is identified, dispatch activity is performed and that is
marked as the termination of the process.
Activity diagram describes the flow of control in a system. It consists of activities and links.
The flow can be sequential, concurrent, or branched as shown in figure 6.
Activities are nothing but the functions of a system. Numbers of activity diagrams are
prepared to capture the entire flow in a system.
25
Activity diagrams are used to visualize the flow of controls in a system. This is prepared to
have an idea of how the system will work when executed.
OBJECT DIAGRAM
Object diagrams can be described as an instance of class diagram. Thus, these diagrams are
more close to real-life scenarios where we implement a system.
Object diagrams are a set of objects and their relationship is just like class diagrams. They
also represent the static view of the system.
The usage of object diagrams is similar to class diagrams but they are used to build prototype
of a system from a practical perspective.
Object Diagrams (figure 7), sometimes referred to as Instance diagrams are very similar
to class diagrams. Like class diagrams, they also show the relationship between objects but
they use real-world examples.
They show how a system will look like at a given time. Because there is data available in the
objects, they are used to explain complex relationships between objects.
COMPONENT DIAGRAM
Component diagrams represent a set of components and their relationships. These components
consist of classes, interfaces, or collaborations. Component diagrams represent the
implementation view of a system.
26
During the design phase, software artifacts (classes, interfaces, etc.) of a system are arranged
in different groups depending upon their relationship. Now, these groups are known as
components.
Finally, it can be said component diagrams are used to visualize the implementation.
SEQUENCE DIAGRAM
A sequence diagram is an interaction diagram. Sequence diagrams in UML show how objects
interact with each other and the order those interactions occur. It’s important to note that they
show the interactions for a particular scenario. The processes are represented vertically and
interactions are shown as arrows. From the name, it is clear that the diagram deals with some
sequences, which are the sequence of messages flowing from one object to another.
Interaction among the components of a system is very important from implementation and
execution perspective. Sequence diagram is used to visualize the sequence of calls in a system
to perform a specific functionality.
27
The sequence diagram (figure 9) has four objects (Customer, Order, SpecialOrder and
NormalOrder).
The figure 9 shows the message sequence for SpecialOrder object and the same can be used
in case of NormalOrder object. It is important to understand the time sequence of message
flows. The message flow is nothing but a method call of an object.
The first call is sendOrder () which is a method of Order object. The next call is confirm
() which is a method of SpecialOrder object and the last call is Dispatch () which is a method
of SpecialOrder object. The following diagram mainly describes the method calls from one
object to another, and this is also the actual scenario when the system is running.
To model a system, the most important aspect is to capture the dynamic behavior. Dynamic
behavior means the behavior of the system when it is running/operating.
Only static behavior is not sufficient to model a system rather dynamic behavior is more
important than static behavior. In UML, there are five diagrams available to model the
dynamic nature and use case diagram is one of them. These internal and external agents are
known as actors. Use case diagrams consists of actors, use cases and their relationships. The
28
diagram is used to model the system/subsystem of an application. A single use case diagram
captures a particular functionality of a system.
Hence to model the entire system, a number of use case diagrams are used.
The purpose of use case diagram is to capture the dynamic aspect of a system. However, this
definition is too generic to describe the purpose, as other four diagrams (activity, sequence,
collaboration, and Statechart) also have the same purpose. We will look into some specific
purpose, which will distinguish it from other four diagrams.
Use case diagrams are used to gather the requirements of a system including internal and
external influences. These requirements are mostly design requirements. Hence, when a
system is analyzed to gather its functionalities, use cases are prepared and actors are
identified.
Use case diagrams are considered for high level requirement analysis of a system. When the
requirements of a system are analyzed, the functionalities are captured in use cases.
We can say that use cases are nothing but the system functionalities written in an organized
manner. The second thing which is relevant to use cases are the actors. Actors can be defined
as something that interacts with the system.
Actors can be a human user, some internal applications, or may be some external applications.
When we are planning to draw a use case diagram, we should have the following items
identified.
• Actors
Use case diagrams are drawn to capture the functional requirements of a system. After
identifying the above items, we have to use the following guidelines to draw an efficient use
case diagram
• The name of a use case is very important. The name should be chosen in such a way so
that it can identify the functionalities performed.
29
• Give a suitable name for actors.
• Do not try to include all types of relationships, as the main purpose of the diagram is to
identify the requirements.
Figure 10 is a sample use case diagram representing the order management system. Hence, if
we look into the diagram then we will find three use cases (Order, SpecialOrder, and
NormalOrder) and one actor which is the customer.
The SpecialOrder and NormalOrder use cases are extended from Order use case. Hence, they
have extended relationship. Another important point is to identify the system boundary, which
is shown in the picture. The actor Customer lies outside the system as it is an external user of
the system.
30
Programming
Programming is the process of developing a set of instructions, which direct the computer to
perform a desired task. This may involve the solving of a complex problem, the handling of a
large amount of data, or the generation of reports or charts. Programs may be relatively short,
with only a few lines of coded instructions, or extremely long and complicated, requiring
thousands of lines of code. The programming process involves the analysis of the problem to
the actual coding of the program. It starts with an idea about the solution to a problem or tasks
and ends with a set of coded instructions in machine-readable form.
The translation of the tested flowchart or pseudo-code into a programming language of our
choice is called coding.
1. The program works and is readily observable: The most important quality of a
program is that it works. As programs and programming systems continue to grow more
complex in future, we may find that it is no longer sufficient for a program to simply
work; it may be necessary for the program (and possibly the programmer) to have some
way of verifying that the program is working correctly.
2. Minimize Testing Costs: The testing and debugging of a program is undoubtedly the
programmer’s greatest headache. Remember that if you write a program so clever, so
complicated that only you can understand it, then the program is worthless; if your
program makes use of undocumented features of the hardware, it is worthless. If your
program is not commented and documented, it is worthless. All of these practices lead to a
worthless program because they make it much more difficult to test. As a result, the
program becomes unnecessarily expensive in terms of time and money.
3. Minimize Maintenance Costs: Programs that are put into maintenance still have a
significant number of bugs, so maintenance is really a continuation of the testing effort.
There is the continuing problem of upgrading the program for new compilers, new
operating systems, and other new systems software. Most programs require continuing
maintenance to meet the needs of an evolving user community. There are very few
31
programs that are so well defined that they need never change.
6. Simplicity of Design: Simplicity implies that the most logical way, and in some cases,
the only way, to make a program easy to test, easy to maintain, easy to upgrade, and easy
for someone else to take over is to keep it simple and straightforward. Simplicity of
programming style is very important.
Once a program has been typed in, different types of errors may show
up. These include:
Syntax/semantic errors
Logic errors
32
C := A + B
C := A - B
Such a program will run properly but will give erratic result because the value
of C will not be the sum of A and B but it will be the difference of A and B
which is quite different. Such errors can only be detected with the help of test
data that will give the input values of A and B and the resultant value that
should come out of the program as a result of the operation performed on A
and B. If the result given by the computer and the result calculated manually
with the help of a calculator are the same, then you can say that there is no
logical error. Otherwise a logic error may be present in the program.
Runtime error
Runtime errors occur when a program is run on the computer and the results are
not achieved due to some misinterpretation of a particular instruction. This
could be something like dividing a number by zero which results in a very large
value of quotient. It may also be any other instruction which a computer is not
able to understand. To overcome this problem, there is a built-in error
detector in the language interpreter or compiler which will give the message
and that will reflect the reason for the run time error.
33
Control Structures
To control the flow of execution of programs, the ‘Basic’ language provides very powerful
facilities.
1. Sequence
2. Selection
3. Repetition or Iteration
Sequence
Sequence logic is used for performing instructions one after another in sequence.
Thus, for sequence logic, pseudo-code instructions are written in the order, or
sequence, in which they are to be performed. The logic flow of program is from the
top to the bottom. Figure 11 shows an example of sequence logic structure.
34
Figure 11: An example of sequence logic structure.
Selection
Selection logic, also known as decision logic, is used for making decisions. It is used
for selecting the proper path out of the two or more alternative paths in the program
logic. Selection logic is depicted as either an IF...THEN...ELSE or IF.....THEN
structure. The flowcharts shown in Figures 12 and 13 illustrate the logic of these
structures. Their corresponding pseudo-code is also given in these figures.
The IF...THEN...ELSE construct says that if the condition is true, then do process
1, else (if the condition is not true) do process 2. Thus, in this case either
process 1 or process 2 will be executed depending on whether the specified
35
condition is true or false. However, if we do not want to choose between two
processes and we simply want to decide if a process is to be performed or not,
then the IF...THEN structure is used.
The IF...THEN structure says that if the condition is true, then do process 1; and if
it is not true, then skip over process 1. In both the structures, process 1 and process 2
can actually be one or more processes. They are not limited to a single process. END
IF is used to indicate the end of the decision structures.
36
Figure 13: The Pseudo-code for IF-THEN Selection Structure
Iteration Logic
Iteration logic is used when one or more instructions may be executed several
times depending on some condition. It uses two structures called the
DO...WHILE and the REPEAT...UNTIL. Theyare illustrated by flowcharts in
Figure 14 and Figure 15 respectively. Their corresponding pseudocodes are also
given in these figures. Both DO...WHILE and REPEAT...UNTIL are used for
looping.
37
Figure 14: Pseudo-code for DO..WHILE Structure
38
Figure 15: Pseudo-code for REPEAT..UNTIL Structure
The differences between the two loops are that in the DO...WHILE, the
looping will continue as long as the condition is true. The l oopi ng s t o p s when the
c o n d i t i o n is not t rue . On the other hand, in case o f REPEAT...UNTIL, the
looping continues until the c o n d i t i o n becomes true. That is, the
e x e c u t i o n of the statements within the loop is repeated as long as the condition
39
is not t r u e . In b o t h the DO...WHILE and REPEAT...UNTIL, the loop must
contain a statement that will change the condition that controls the loop. Also the
condition is tested at the top of the loop in the D O ...WHILE loop structure and a t the
b o t t o m of the l o o p in the REPEAT...UNTIL structure. The "ENDDO" marks the end
of a DO...WHILE structure and U N T I L followed b y some condition marks the end
of the REPEAT...UNTIL structure.
Examples
Input
Example 2
This example illustrates the policy of a bank on a financial
insti- tution for giving a loan to an individual.
Input mortgage amount
IF amount < 25,000
THEN
down payment = 3% of amount
ELSE
payment1 = 3% of
25,000
payment2 = 5% of (amount - 25,000)
down payment = payment1 + payment2
END IF
print down payment
Example 3
The following program in pseudo-code to illustrates the
policy of a company to give the commission to a sales
person.
Input sales
IF sales < 500
THEN
Commission = 2% of sales.
ELSE
IF sales < 5000
THEN
Commission = 5% of sales
ELSE
Commission = 10% of sales
END IF
Print Commission
41