Module 2-1
Module 2-1
Programming 1
Information Technology
1st Semester, School Year 2023-2024
INTRODUCTION
A computer is a useful tool for solving a great variety of problems. To make
a computer do anything (i.e. solve a problem), you have to write a computer
program. In a computer program you tell a computer, step by step, exactly what
you want it to do. The computer then executes the program, following each step
mechanically, to accomplish the end goal.
This module presents the typical programming cycle of Java and the
platform-independent architecture of Java. Part of this module is the
downloading and installation of NetBeans 8.0 or 8.1.
In this module you will also learn more about Flowcharting and Algorithm.
The number of hours allotted for this module shall be for 5 hours.
LEARNING OUTCOMES
At the end of the module, you should be able to:
a. analyze a problem and identify the computing requirements
appropriate for its solution;
b. design, implement, and evaluate an algorithm to meet desired needs;
c. apply design and development principles in the construction of program
of varying complexity.
d. apply current techniques, skills, and tools necessary for computing
practice.
LESSON 1: Algorithm
Objectives:
At the end of the lesson, you should be able to:
1. understand the basics and usefulness of an algorithm,
2. analyzed various algorithmic solutions to the problem; and,
3. implement the algorithmic solutions to a given problem.
Algorithms are judged not only by how well they solve a problem, but also
by how effectively they use resources like time and space. We will learn
techniques for analyzing time and space complexity of algorithms and will use
these to evaluate tradeoffs between different algorithms. We will also see that
problems can be organized into a hierarchy that measures their inherent
difficulty by the efficiency of the best possible algorithm for solving them.
It is possible to create algorithms for the things that we do every day such
as making breakfast, brushing teeth, taking a bath and washing your clothes.
Before we start our lesson, let’s try doing this activity. How do you cook
adobo?
Write your answers on a short bond paper. Put numbers next to your
response to indicate the order of the steps.
Recipe
1
2 lbs pork belly
2 tablespoons garlic minced or crushed
5 pieces dried laurel leaves
4 tablespoons vinegar
1/2 cup soy sauce
1 tablespoon black pepper
2 cups water
Salt to taste
Algorithm 1
When your bus arrives at the bus station
Wait for a tricycle
Get in a tricycle
Tell the driver my location
2
Algorithm 2
When your bus arrives at the bus station, call me
Wait for me at the bus station.
All these algorithms accomplish the same goal, but each algorithm does it
in a different way. Each algorithm also has a different cost and a different travel
time. Taking a taxi, for example, is the fastest way, but also the most expensive.
Waiting for your friend is definitely less expensive, but a whole lot slower. You
choose the algorithm based on the circumstances.
In computer programming, there are often many different algorithms to
achieve any given task. Each algorithm has advantages and disadvantages in
different circumstances. Sorting is one place where a lot of research has been
done, because computers spend a lot of time sorting lists.
Three reasons for using algorithms are efficiency, abstraction and
reusability.
Expressing Algorithms
Algorithms can be expressed in many different notations, including
natural languages, pseudocode, flowcharts and programming languages.
Natural language expressions of algorithms tend to be verbose and ambiguous,
and are rarely used for complex or technical algorithms. Pseudocode and
flowcharts are structured ways to express algorithms that avoid many
ambiguities common in natural language statements, while remaining
independent of a particular implementation language. Programming languages
are primarily intended for expressing algorithms in a form that can be executed
by a computer, but are often used to define or document algorithms.
3
Sometimes it is helpful in the description of an algorithm to supplement
small flowcharts with natural language and/or arithmetic expressions written
inside block diagrams to summarize what the flowcharts are accomplishing.
Algorithm using natural language statements:
Start
Assume that the first number is 10
Assume that the second number is 5
Add 10 and 5
Display the output
Stop
Start
Algorithm using pseudocode
Num1=10;
Num2=5;
Sum=Num1 + Num2;
Print Sum;
Stop
4
Constructs for developing algorithms:
Sequence
Sequence involves simple steps to be performed successively.:
Steps are executed one at a time and not in parallel,
Each step is executed only once;
Execution order is the order in which steps are written
Execution of the last step terminates the algorithm.
Self-Assessment
1. Given the number of commodities bought, create an algorithm that computes
the total amount purchased in a fast food center. Assume the following unit price
of each commodity:
Spaghetti - P65.00
Cheeseburger - P45.00
Soft drinks - P20.00
Fried Chicken - P45.00
5
2. Create an algorithm that computes the average grade of a student, given his
grade four subjects: Filipino, Math, English and Science. Assume that all
subjects are 1 unit except for science with 2 units.
3. Create an algorithm that will compute the weekly salary of an employee. The
algorithm will ask for the number of days worked and the rate per day.
Selection
It refers to a decision based on a certain condition. If the condition is true,
one of the two branches is explored otherwise the other alternative is taken or
executed. This is usually presented in if-else construct in programming.
An algorithm that reads to values, determines the larger value and prints the
larger value.
Start
Input first value
Input second value
If first value is greater than second value, then
Larger value is the first value, otherwise
Larger value is the second value
Stop
Below is an algorithm that will ask the user to enter to enter his/her name
and sex. Then displays a message on the screen whether he/she is a boy or a
girl.
Start
Input name
Input sex
If sex is equal to "Male"
Display "You are a Boy" otherwise
Display "You are a Girl" otherwise
Stop
Another example of algorithm using the select construct that ask a
person's name and age, and display if he/she is a minor or not.
Start
Input name
Input age
If age is lesser or equal to 17
Display "Minor" otherwise
Display "Not a Minor" otherwise
Stop
Self-assessment
1. Create an algorithm that will determine and display on the screen if an entered
number is a positive or a negative.
6
2. Create an algorithm that will display the amount to be paid by a customer if
he/she is given 10% discount rate for buying an item that costs more than
P1000.00 otherwise only 3%.
Iteration
Iteration is the repetition of a process in a computer program. Iterations of
functions are common in computer programming, since they allow several blocks
of data to be processed in sequence. This is normally done using a looping
statement in programming. These loops will repeat a process until a certain
number, case or condition is reached.
7
LESSON 2: Flowchart
Objectives
At the end of the lesson, you should be able to:
1. translate algorithms to flowchart; and,
2. design a solution for a certain problem using flowchart.
LET’S ENGAGE
Flow charts are a useful tool in these situations, as they make a process
easy to understand at a glance. Using just a few words and some simple
symbols, they show clearly what happens at each stage and how this affects
other decisions and actions.
The blue flowchart is meant to show the steps on how to stop working
and shutting down the computer.
Place the instruction below in the flowchart. Some of the instructions are
not required, include only those are relevant to the task.
Start
Stop
8
LET’S TALK ABOUT IT.
Flowchart
A Flowchart is a type of diagram (graphical or symbolic) that represents an
algorithm or process. Each step in the process is represented by a different
symbol and contains a short description of the process step. The flow chart
symbols are linked together with arrows showing the process flow direction. A
flowchart typically shows the flow of data in a process, detailing the
operations/steps in a pictorial format which is easier to understand than reading
it in a textual format.
A flowchart describes what operations (and in what sequence) are required
to solve a given problem. A flowchart can be likened to the blueprint of a building.
As we know a designer draws a blueprint before starting construction on a
building. Similarly, a programmer prefers to draw a flowchart prior to writing a
computer program.
Flowcharts are a pictorial or graphical representation of a process. The
purpose of all flow charts is to communicate how a process works or should work
without any technical or group specific jargon.
Flowcharts are used in analyzing, designing, documenting or managing a
process or program in various fields.
Flowcharts are generally drawn in the early stages of formulating computer
solutions. Flowcharts often facilitate communication between programmers and
business people. These flowcharts play a vital role in the programming of a
problem and are quite helpful in understanding the logic of complicated and
lengthy problems. Once the flowchart is drawn, it becomes easy to write the
program in any high level language. Often we see how flowcharts are helpful in
explaining the program to others. Hence, it is correct to say that a flowchart is
a must for the better documentation of a complex program.
For example, consider that we need to find the sum, average and product
of 3 numbers given by the user.
Algorithm for the given problem is as follows:
Start
Read num1, num2, num3
Compute sum as num1+num2+num3
Computer average as sum/3
Compute product as num1 x num2 x num3
Write/Display the sum, average and product
Stop
Flowchart for the above problem will look like
9
Start
Read num1,num2,num3
sum= num1+num2+num3
average = S/3
product=X * Y * A
Write
sum,average,product
Stop
Now that we have seen an example of a flowchart let us list the advantages
and limitations of using flowcharts.
10
Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
Alterations and Modifications: If alterations are required the flowchart may
require re-drawing completely.
Reproduction: As the flowchart symbols cannot be typed, reproduction of
flowchart becomes a problem.
The essentials of what is done can easily be lost in the technical details of how
it is done.
11
Process: A rectangular flow chart shape indicates a normal/generic process flow
step. For example, “Add 1 to X”, “M = M*F” or similar.
Decision: A diamond flow chart shape indicates a branch in the process flow.
This symbol is used when a decision needs to be made, commonly a Yes/No
question or True/False test.
Connector: A small, labelled, circular flow chart shape used to indicate a jump
in the process flow. Connectors are generally used in complex or multi-sheet
diagrams.
On-page off-page
Data: A parallelogram that indicates data input or output (I/O) for a process.
Examples: Get X from the user, Display X.
Arrow: used to show the flow of control in a process. An arrow coming from one
symbol and ending at another symbol represents that control passes to the
symbol the arrow points to.
These are the basic symbols used generally. Now, the basic guidelines for
drawing a flowchart with the above symbols are that:
In drawing a proper flowchart, all necessary requirements should be listed
out in logical order.
The flowchart should be neat, clear and easy to follow. There should not
be any room for ambiguity in understanding the flowchart.
The flowchart is to be read left to right or top to bottom.
A process symbol can have only one flow line coming out of it.
12
For a decision symbol, only one flow line can enter it, but multiple lines
can leave it to denote possible answers.
The terminal symbols can only have one flow line in conjunction with
them.
Start
Read LFeet
LCenti = LFeet * 30
Write LCenti
Stop
13
Start
Area = Width *
Length
Write Area
Stop
Self-Assessment.
1. Given the number of commodities bought, create an algorithm that computes
the total amount purchased in a fast food center. Assume the following unit price
of each commodity:
Spaghetti - P65.00
Cheeseburger - P45.00
Soft drinks - P20.00
Fried Chicken - P45.00
2. Create an algorithm that computes the average grade of a student, given his
grade four subjects: Filipino, Math, English and Science. Assume that all
subjects are 1 unit except for science with 2 units.
3. Create an algorithm that will compute the weekly salary of an employee. The
algorithm will ask for the number of days worked and the rate per day.
Selection
Once the condition is evaluated, the control flows into one of two paths.
Once the conditional execution is finished, the flows rejoin before leaving the
structure. Once again the single entry, single exit nature of a structure is evident.
Either process could be replaced with any structure (sequence, selection, or
repetition) or it could be omitted (for example the process on the "NO" side might
not exist).
As an alternative, the "Question" could be replaced with a Boolean
expression and the branches labelled "TRUE" and "FALSE".
A flowchart that reads to values, determines the larger value and prints
the larger value.
14
Start
Input first value
Input second value
If first value is greater than second value then
Larger value is the first value, otherwise
Larger value is the second value
Stop
Start
N
If val1 > val2
Stop
Start Start
Input name
Input sex Read name Read sex
If sex==Male
Display "You are a Boy" otherwise
Y
Display "You are a Girl" If sex==
"Male"
Stop
N
Display "You
are a Boy" Display "You
are a girl"
Stop
15
Another example of flowchart using the select construct that ask a
person's name and age, and display if he/she is a minor or not.
Input name
Input age
If age is lesser than or equal to 17
Display "You are a minor" otherwise
Display "You are not a m"
Start
Y
If <= 17
Display "You
Display "You
are a minor"
are not a
minor"
Stop
Self-Assessment
a. Create an algorithm that will determine and display on the screen if an
entered number is a positive or a negative.
b. Create an algorithm that will display the amount to be paid by a customer
if he/she is given 10% discount rate for buying an item that costs more
than P1000.00 otherwise only 3%.
Repetition
Either the processing repeats or the control leaves the structure. (Again,
the process could be replacing another structure).
Notice that the return line joins the entry line before the question. Never
try to insert processing in the entry line between the join and the question.
The flowchart below will display "Hello World" 5 times.
Set counter to 1
While counter is lesser than 5
Display "Hello World"
Increment counter with 1
16
End while
Start
N
While counter
Counter = 1
<=5
Y
Display
"Hello
World"
Counter = Counter
+1
Stop
Another example below is an algorithm that will display numbers from 1 to 10.
Start
Set counter to 1
While counter is lesser than 10
Display counter
Increment counter with 1
End while Start
Stop
N
While counter
Counter = 1
<=10
Y
Display
counter
Counter = Counter
+1
Start
Set counter to 1
While counter is lesser than or equal to 10
Then Input a number
Increment counter with 1
End while
17
Stop
Start
N
While counter
Counter = 1
<=10
Input
number
Counter = Counter
+1
Stop
Self-Assessment
a. Create a flowchart that will add numbers from 1 to 10
b. Create a program that will accept the quiz scores of a student and
display his average, given the total number of quizzes he has taken.
If you want to know more about algorithm, visit the following:
The friendship algorithm Retrieved from
https://www.youtube.com/watch?v=k0xgjUhEG3U
REFERENCES
https://www.java-made-easy.com/java-output.html#ixzz6Tsh9bQn8
https://java.com/en/download/help/download_options.xml
Programming Knowledge. Retrieved from
https://www.youtube.com/watch?v=pgr-Bzkc4zg
Atiwong Suchato. Learning Computer Programming using Java with 101
Samples.
www.computerhope.com
https://www.tutorialspoint.com/computer_fundamentals/computer_output_de
vices.htm
http://www.btechsmartclass.com/c_programming/C-Program-Development-
Life-Cycle.html
Programming Languages: Low Level, machine language, Assembly, High Level
Language (HLL); Algorithm and Flow chart: Concept and Uses
What is a computer language. Retrieved from
https://www.tutorialspoint.com/what-is-a-computer-language
18
Jason Killian (2012). Number Systems: An Introduction to Binary, Hexadecimal,
and more. Retrieved from https://code.tutsplus.com/articles/number-
systems-an-introduction-to-binary-hexadecimal-and-more--active-10848
D.S. Malik (2012). Java Programming: Problem Analysis to Program Design 5th
Edition.
19