0% found this document useful (0 votes)
5 views

Lab Introduction

Uploaded by

mat337983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Introduction

Uploaded by

mat337983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Kingdom of Saudi Arabia Cybersecurity Diploma

Ministry of Education Introduction to Programming


Aljouf University - ACCS102 -
Applied College Lab Part

Program Development

A program is a set of instructions that can be used to solve a problem.

Often, a program contains many instructions, and the instructions are rather complicated.

Therefore, developing a successful program requires some effort. It requires careful planning,

careful implementation, and ongoing maintenance. Here is a list of typical steps involved in the

program development process:

 Requirements analysis
 Design
 Implementation
 Testing
 Documentation
 Maintenance

Requirements analysis is determining the program's needs and goals.

Design is writing a rough outline of the program.

Implementation is writing the program itself.

Testing is verifying that the program works.

Documentation is writing a description of what the program does.

Maintenance is making improvements and fixing errors later on.

The steps ordered in a reasonable sequence in that you'l1 normally perform requirements

analysis first, design second, and so on. But some of the steps should be performed throughout the

development process rather than at one particular time. For example, you should work on the

Introduction to programming Course Inst. Sultan Alfalah 1


documentation step throughout the development process, and you should work on the testing step

during and after the implementation step and also after the maintenance step. Be aware that you'll

often need to repeat the sequence of steps as needs arise. For example, if one of the program's goals

changes, you will need to repeat all of the steps in varying degrees.

This term, we will apply the first four steps. We will discuss the requirements analysis step and

the design step this week. Then, we will discuss the Implementation step later this term.

In the design step, we should use some analysis methods. Each programmer uses the methods

that he is aware of. This semester, we will use flow chart and pseudocode.

Introduction to programming Course Inst. Sultan Alfalah 2


Flowchart
Flowchart is a graphical representation of a computer program in relation to its sequence of
functions.

Pseudocode
In writing an algorithm, you should focus on organizing the flow of the instructions, and you

should try to avoid getting bogged down in details. To facilitate that focus, programmers often write

an algorithm’s instructions using pseudocode.

Pseudocode is an informal language that uses regular English terms to describe a program's

steps. With pseudocode, precise computer syntax is not required. Syntax refers to the words,

grammar, and punctuation that make up a language. Pseudocode syntax is lenient: pseudocode must

be clear enough so that humans can understand it, but the words, grammar, and punctuation do not

have to be perfect.

Introduction to programming Course Inst. Sultan Alfalah 3


Example-Using Pseudocode to Find Average Miles per Hour

Suppose you are asked to write an algorithm that finds the average miles per hour value for a

given car trip. Let's step through the solution for this problem. To determine the average miles per

hour, you will need to divide the total distance traveled by the total time. Let us assume that you

have to calculate the total distance from two given locations. To determine the total distance, you'1l

need to take the ending-point location, called "ending location," and subtract the starting-point

location, called "starting location,” from it. Let us assume that you have to calculate the total time in

the same manner, subtracting the starting time from the ending time. Putting it all together, the

pseudocode for calculating average miles per hour looks like this:

Calculate ending location minus starting location.


Put the result in total distance.
Calculate ending time minus starting time.
Put the result in total time.
Divide total distance by total time.

There are many ways to write a pseudocode, you do not have to write the same way that was

written in the above example.

Another way to write a pseudocode for the previous example:


TotalDistance = EndingLocation – StartingLocation
TotalTime = EndingTime – StartingTime
AverageMilerPerHour = TotalDistance / Total Time

Introduction to programming Course Inst. Sultan Alfalah 4


Exercises using pseudocode
Exercise#1: Hello java algorithm
 Write an algorithm that prints Hello Java on the screen.

Begin
Print “Hello, Java”
End

Exercise#2: Rectangle Algorithm


 Write an algorithm that calculate and display the rectangle area.

Before we solve this exercise, we should study a very important principle called variables. What

do variables mean and when do we use them.

A variable is a characteristic, number, or quantity that increases or decreases over time, or takes

different values in different situations.

In computer programming, a variable is a storage location paired with an associated symbolic

name (an identifier), which contains some known or unknown quantity or information referred to as

a value. The variable name is the usual way to reference the stored value; this separation of name

and content allows the name to be used independently of the exact information it represents. The

identifier in computer source code can be bound to a value during run time, and the value of the

variable may change during the course of program execution.

Variables in programming may not directly correspond to the concept of variables in

mathematics. The value of a computing variable is not necessarily part of an equation or formula as

Introduction to programming Course Inst. Sultan Alfalah 5


in mathematics. In computing, a variable may be employed in a repetitive process — assigned a

value in one place, then used elsewhere, then reassigned a new value and used again in the same

way (like iteration or looping – we’ll discuss looping later - ). Variables in computer programming are

frequently given long names to make them relatively descriptive of their use, whereas variables in

mathematics often have terse, one- or two-character names for brevity in transcription and

manipulation. For Example, if you want to define a variable that refers to rectangle length you

should write RectangleLength. In addition, if the variable contains two words or more, they

must not be separated by a space, they should be connected as one word and the first letter in each

word should be capitalized. Alternatively, you can separate those words using dashes

Rectangle_Length.

Compilers* have to replace variables' symbolic names with the actual locations of the data.

While a variable's name, type, and location often remain fixed, the data stored in the location may

be changed during program execution.

So, if you are asked to write an algorithm or a program, first of all, you should analyze any

variable that can solve the problem. Then, define them at the beginning. After that, think about

solving the problem.

*A compiler is a computer program (or set of programs) that transforms source code written in

a programming language (the source language) into another computer language (the target

language, often having a binary form known as object code). The most common reason for

converting a source code is to create an executable program.

The name "compiler" is primarily used for programs that translate source code from a high-level

programming language to a lower level language (e.g., assembly language or machine code).

Introduction to programming Course Inst. Sultan Alfalah 6


 Now, let’s go back and solve the exercise.

First, we should define anything that helps solving this problem. Any rectangle has 4 sides, 2

lengths and 2 widths. We should know one length and one width to calculate the rectangle area.

So, let’s assume that the first variable called RectangleLength and the second one called

RectangleWidth.

Assume that RectangleLength = 2, and RectangleWidth = 4

Now, we are ready to write the pseudocode after we analyze the problem.

Begin
Set RectangleLength to 2
Set RectangleWidth to 4
RectangleArea = RectangleLength * RectangleWidth
Print “Rectangle Area = “, RectangleArea
End

When you convert the previous pseudocode to a programming language such as java, you must

specify the amount of each variable. Which means, you should tell the computer how many bytes

you want to reserve in the memory.

In this exercise we used integer numbers. So, we should tell the computer to reserve 2 bytes in

the memory.

The computer is programmed that integer is 2 bytes, float is 4 bytes, and so on. The above

pseudocode called “Folrmal Pseudocode”. Nevertheless, if you want to write a “High-level

Pseudocode” you should define each variable, as shown bellow:

Introduction to programming Course Inst. Sultan Alfalah 7


Begin
Define RectangleLength as integer
Define RectangleWidth as integer
Define RectangleArea as integer
RectangleLength  2
RectangleWidth  4
RectangleArea  RectangleLength * RectangleWidth
Print “Rectangle Area = “, RectangleArea
End

As you can see, there is no rules to write a pseudocode, at the first solution we wrote

Set RectangleLength to 2. On the other hand, at the second solution we wrote

RectangleLength  2, to store two in the variable RectangleLength.

In the previous example we assumed the values for each RectangleLength and

RectangleWidth. Sometimes, the values should be entered by keyboard. Which means, you have

to ask the user to enter a value. For Example:

Begin
Define RectangleLength as integer
Define RectangleWidth as integer
Define RectangleArea as integer
Print “Enter the rectangle length please: “
Input RectangleLength
Print “Enter the rectangle width please : ”
Input RectangleWidth
RectangleArea  RectangleLength * RectangleWidth
Print “Rectangle Area = “, RectangleArea
End

So, every time you want a value to solve the problem, you ask the user to enter this value.

Introduction to programming Course Inst. Sultan Alfalah 8


Exercise#3: Math operations on 2 numbers Algorithm
 Write an algorithm that do a math operation on two numbers.

In this exercise, we need to define two variables. Also, we need to assume a math operation.

So, let’s us call the two variables: Number1 and Number2. Moreover, assume that we use

multiplication on these numbers.

Begin
Define Number1, Number2 as integer
Print “Enter the first number please: “
Input Number1
Print “Enter the second number please: “
Input Number2
Define Number3 as integer
Number3 = Number1 * Number2
Print “The result = ”, Number3

In this exercise, we defined two variables in one sentence. In addition, we recognized later that

we needed a new variable to store the result in. So, we defined the new variable where we knew

that.

In pseudocode, we don’t have to define variables. I mentioned it here so you got the idea when

we write java programs. In java or any other programming language, you must define any variable

you want to use.

Introduction to programming Course Inst. Sultan Alfalah 9


Exercises using pseudocode and flowchart

Condition and loops

 First: Condition

Exercise#1: Student grade

 Case#1
 Write an algorithm that prints succeeded if the student succeeded.

As we know, if the student got 60 or more, he will succeed.

Begin
Define Grade as integer
Print “Enter the student grade please: ”
Input Grade
If Grade is greater than or equal to 60 Then
Print “Succeeded”
End

Start

Read Grade

Yes
Grade >= 60? Print “Succeeded”
No

End

Introduction to programming Course Inst. Sultan Alfalah 10


 Case#2:
 Write an algorithm that determine whether the student succeeded or failed and prints the

reults.

Begin
Define Grade as integer
Print “Enter the student grade please: ”
Input Grade
If Grade is greater than or equal to 60 Then
Print “Succeeded”
Else
Print “Failed”
End

Start

Read Grade

No Yes
Print “Failed” Grade >= 60? Print “Succeeded”

End

Introduction to programming Course Inst. Sultan Alfalah 11


 Case#3:
 Write an algorithm that prints Excellent if the students got 90 or more, Very Good if he got

80 or more, Good if he got 70 or more, Pass if he got 60 or more or Failed if he got less than

60.

Begin
Define Grade as integer
Print “Enter the student grade please: ”
Input Grade
If Grade is greater than or equal to 90 Then
Print “Excellent”
Else If Grade is greater than or equal to 80 Then
Print “Very Good”
Else If Grade is greater than or equal to 70 Then
Print “Good”
Else If Grade is greater than or equal to 60 Then
Print “Pass”
Else
Print “Failed”
End

Introduction to programming Course Inst. Sultan Alfalah 12


Start

Read Grade

No No Grade >= No
Grade >=90? Grade >=80? 70?

Yes Yes Yes

Print ”Excellent” Print ”Very Good” Print ”Good”

No
Print ”Fail” Grade >=60?

Yes

Print ”Pass”

End

Introduction to programming Course Inst. Sultan Alfalah 13


 Second: Looping

Exercise#1: Happy Eid

 Case#1
 Write an algorithm that prints Happy Eid 100 times

Set counter to 1
While counter is less than or equal to 100
Print “Happy Eid”
Set counter to counter + 1

Start

counter = 1

No
counter < 100
Yes

Print “ Happy Eid “

End

Introduction to programming Course Inst. Sultan Alfalah 14


 Case#2
 Write an algorithm that prints Happy Eid 100 times. Each time it prints Happy Eid, let it

prints Eid Mubarrak 50 times.

Set counter1 to 1

Set counter2 to 1

While counter1 is less than or equal to 100

Print “Happy Eid”

While counter2 is less than or equal to 50

Print “Eid Mubarrak”

Set counter2 to counter2 + 1

Set counter1 to counter1 + 1

Introduction to programming Course Inst. Sultan Alfalah 15


Start

counter1 = 1

counter2 = 1

No
counter1 < 100

Yes
Print “ Happy Eid “

counter1 = counter1 + 1

No
counter2 < 50
Yes

Print “ Eid Mubarrak “

counter2 = counter2 + 1

End

Introduction to programming Course Inst. Sultan Alfalah 16

You might also like