You are on page 1of 34

L1: AN INTRODUCTION TO

ALGORITHMS AND PROGRAMMING


LECTURE OVERVIEW

• 1.1 What is Computer Programming?


• 1.2 Algorithms
• 1.3 Computer Programming Languages
• 1.4 About Java
• 1.5 Integrated Development Environments
• 1.6 Unit Summary
1.1 WHAT IS COMPUTER PROGRAMMING?

• The general notion among people is that computers are incredibly


smart. You may be surprised to learn, however, that they are not.

• A computer is an ordinary machine, very much like a toaster or a


washing machine; what makes it different, though is the fact
that a computer can be programmed to do a whole lot of things.

• If a computer is programmed properly, it will do great things. If


it is not programmed, it will just sit there and do nothing.
WHAT IS COMPUTER PROGRAMMING? (2)
• So a computer is only as good as the programmes which it runs,
and it is these computer programs which give computers their
amazing capabilities.

• The people who make these programs are computer programmers,


people who, like you, decided to start learning about programming
at some stage, and over time became experts in their field

• But before you actually set out to become a computer programmer,


it would be important to know from the outset what computer
programming actually is.
WHAT IS COMPUTER PROGRAMMER?

• A very simple and concise definition of computer programming is:


• “Computer programming is creating a sequence of very precise instructions written in
a language a computer understands, to perform a specified task with a computer.”

• So computer programming is simply giving a computer a set of


instructions, and asking it to carry them out.

• Every program that runs on your computer, from your internet browser to
your word processor, spread sheet program, computer game and media
player, is essentially a set of instructions which have been input into the
computer by a programmer, or groups of programmers.
1.1.1 EXTREME PRECISION
• The above definition sounds simple enough. The reality, however, is that
programming is a little more complex than that.

• Consider the words “very precise instructions” in the definition of


programming given above. This means that computers need to be told
exactly what to do, and they will do exactly what they are told to do,
nothing more, and nothing less.

• Unlike humans, they understand only extremely precise instructions, and do


not possess the ability to accept vague instructions and then carry them
out correctly
EXTREME PRECISION (2)
• As an example, consider the following statement:
“I love programming

• If you were asked to count the number of words in the above sentence, you
would be able to do so without much thought. It is, in essence, a no-brainer.

• However, if you were to present the instruction to a computer, it would NOT


BE ABLE to carry out your instruction because, among other things, a
computer:
• 1. Does not know what a sentence is
• 2. Does not know what a word is
EXTREME PRECISION (3)

• So you would first have to “teach” a computer what a word is,


and what a sentence is, and then repeat your instruction.
• Putting it very crudely, your instructions would be something
like:
• 1. A word is a group of letters
• 2. A word is separated from another word by a space
• 3. A sentence is a group of words
• 4. Given the above sentence, count the number of words in it
EXTREME PRECISION (4)
• The above instructions present a great improvement on the original
instructions, BUT there are a few obvious flaws, which will call for
further refinement. One such flaw becomes apparent in the following
example:
• “I PURCHASED A SCREEN,A KEYBOARD AND A MOUSE”
• Notice the deliberate error: there is no space after the comma.
• To a human, the above sentence has nine words, but based on the
previous set of instructions, a computer will count just eight words.
• This is because a computer will consider the words “screen, a” as one
single word because there are no spaces separating the words.
EXTREME PRECISION (5)
• As you can see, in giving instructions to a computer, the programmer
needs to be very explicit and precise, and cannot take anything for
granted.

• He also needs to consider every possible scenario and be very


systematic, logical and organized.

• Which is not to say that computer programming is difficult; rather,


it can be fun and challenging, but must be approached in a
systematic manner.
1.2 ALGORITHMS
• An algorithm can be defined as: “A set of instructions for solving a
problem; it consists of actions to do, and the order in which to do
them”. In summary, an algorithm needs to be:

• (1) COMPLETE: it must cover every aspect of the problem that is


being solved, without taking anything for granted.

• (2) UNAMBIGUOUS: Each step must be explained in such a way


that it leaves no room for interpretation; the instructions must be
very precise
ALGORITHMS (2)

• (3) DETERMINISTIC: No matter how many times you run


the same algorithm with the same input, it must always
produce the same results

• (4) FINITE: Every algorithm should always have an end point


– in other words, it should not contain such instructions which
make it continue running forever
1.3 COMPUTER PROGRAMMING LANGUAGES

• The definition of computer programming makes mention of “a language


a computer understands”, but what language does a computer
understand??

• Deep down, computer only really understands BINARY MACHINE


CODE, which is made up of 1’s and 0’s.

• Binary machine code is extremely difficult for humans to understand,


let alone program in, and so HIGH-LEVEL PROGRAMMING
LANGUAGES were developed to make programming computers easier.
COMPUTER PROGRAMMING LANGUAGES (2)

• High-level languages are similar to human languages, and hence


much easier to learn.
• Examples of the major high-level programming languages in use
today are Java, Visual Basic, C#, PHP and Python.
• Typically, a programmer will create a computer program in a
high-level language, and then use specialized software to
translate the program into binary machine code which the
computer understands, in a process known as compiling.
• Once the code is compiled, it is sent to the processor of the
computer for execution.
1.4 ABOUT JAVA
• Java is a language which was developed
in 1991 by a company called Sun
Microsystems. The language was originally called Oak, but was later renamed Java.

• Java was created in response to a major problem which existed in the


computing world: different computers understood different languages, and as a
result, programmers had to write multiple versions of the same program if they
wanted it to run on different types of computers.

• The people at Sun decided to create a language which would be able to run on any
computer, including on the internet, and so Java was born. This ability to run on
all kinds of computers, and particularly on the internet, made Java extremely
popular with programmers, and Java became a hit.
1.4.1 HOW JAVA WORKS
• With most programming languages, when you write a program and
compile it, the program is translated into machine code, which the
computer can understand and execute.

• Java does things a little differently: when you compile a Java


program, it is translated into Java Byte Code, not machine code.

• When you run the compiled program, it is executed by Java Virtual


Machine (JVM), which is a virtual machine that is installed on your
computer.
HOW JAVA WORKS (2)

• It is the JVM’s job to translate the Java Byte code into a


language which the underlying operating system understands.
This is illustrated by the following diagram:
HOW JAVA WORKS (3)

• The computer in the above diagram could be running Windows,


Linux, MACOS or any operating system; it makes no
difference to your program.

• The JVM acts as the intermediary between your program


and the operating system of the computer, so you do not
need to change your program to run on different machines
1.4.2 INSTALLING JAVA
• In order to be able to create and run Java programs on your
computer, you will need to install the Java Development Kit (JDK).

• The JDK is a development environment for building applications,


applets, and components using the Java programming language.

• The JDK includes tools which are necessary for developing and
testing programs written in the Java programming language and
running on the Java platform.
INSTALLING JAVA (2)
• To install the JDK you will need to download the installation package and
install it on your computer.

• The JDK is available free from Oracle, and the latest version can be
downloaded from their website:
https://www.oracle.com/java/technologies/downloads/#jdk17-windows

• Here you may download the version of the JDK which is compatible to your
computer’s operating system. If you are a MAC user, you do not need to
install the JDK – MACS come with the JDK installed from the factory.
INSTALLING JAVA (3)
INSTALLING JAVA (4)
INSTALLING JAVA (5)
1.5 INTEGRATED DEVELOPMENT
ENVIRONMENTS
• Once you have the JDK installed, you can begin creating your own Java
programs. You do not need any special tools for typing in your java code
– you can use any word processor, or even a text editor like Notepad for
typing your code.

• This is fine if you’ve got a small program, but as your programs get bigger
and include more classes, it becomes difficult to maintain with just a text
editor.

• In such case it becomes necessary to use an Integrated Development


Environment (IDE)
INTEGRATED DEVELOPMENT
ENVIRONMENTS (2)
• An IDE is a program that is designed to help programmers and
developers build software.
• An IDE typically contains a code editor, compiler, debugger and other
utilities; all integrated into a single package, and presents all this via a
graphic interface.
• With an IDE you can do multiple tasks with the click of a single button.
• There are many different IDE’s for creating Java programs, but the
most popular are Eclipse, NetBeans and IntelliJ IDEA.
• For academic purposes, the preferred IDE is JGrasp due to its simplicity
of use. The figure below shows a screen from the JGrasp IDE:
1.5.1 INSTALLING JGRASP

• JGrasp is available for free, and can be downloaded at the


JGrasp Download page:

• http://spider.eng.auburn.edu/user-cgi/grasp/grasp.pl?;dl=down
load_jgrasp.html

• JGrasp also provides detailed, step-by-step documentation on


installation and usage of JGrasp. The documentation is
downloadable in PDF format, and can be found here:

• http://www.jgrasp.org/tutorials187/01_Installing.pdf
1.6 UNIT SUMMARY
• Computer programming is creating a sequence of very precise
instructions written in a language a computer understands, to perform
a specified task with a computer

• Complex problems can be analyzed and simplified using algorithms

• An algorithm is setof instructions for solving a problem; it consists


of actions to do, and the order in which to do them

• An algorithm must be complete, unambiguous, deterministic and finite


UNIT SUMMARY (2)
• Computers only understand binary machine code, which is very difficult for
humans to read and write code in

• Programming languages were thus developed to simplify the process of


writing programs. Programming languages are close to human languages, and
are hence easy to use

• High-level programming languages such as Java, C#, PHP, Python and Visual
Basic are some of the current popular languages in use

• An IDE is a program that is designed to help programmers and developers


build software
REVIEW QUESTIONS
REVIEW SOLUTIONS

You might also like