Lecture 1: OVERVIEW OF COMPUTERS AND
PROGRAMMING LANGUAGES
TOPICS
Computer Organization Overview:
hardware and software
Computer programming and programming
life-cycle phases
Algorithms
Programming languages
Procedural and
Object-Oriented Programming
OUTLINE
1. Computer Organization Overview
CPU
= central processing unit - executes the instructions in programs. Components:
Control Unit (CU) and Arithmetic Logic Unit (ALU). Location: motherboard
RAM = random-access memory = main memory - stores data that CPU
accesses, including instructions. Fast, but temporary memory; wiped out when
computer is shut down. Location: motherboard.
ROM = read-only memory. Programmed with a permanent collection of
pre-set bytes. Permanent memory. Location: motherboard.
Secondary memory: hard disks, CDs, DVDs, flash memory, etc. Stores
data that can be loaded into main memory. Slower, but permanent.
I/O devices = how you communicate with your machine (keyboard, monitor,
mouse, speakers, etc.)
Networking equipment = how other machines communicate with your machine:
networking cards, cables, etc.
Main memory = a table of bytes indexed by addresses. Computer data
consists of ON and OFF pieces (written as 1’s and 0’s). Standard terminology:
a) Programming = decomposition of a task into a sequence of primitive
operations (at the basic level, computer primitive operations are very simple).
Computer programming = Planning a sequence of steps
(called instructions) for a computer to follow.
b) Programming life-cycle phases:
1. Problem-solving
Sample problem: a programmer needs to design an algorithm to determine
an employee's weekly wages. First draft of the algorithm:
1. Look up the employee's pay rate
2. Determine the number of hours worked during the week
3. If the number of hours worked is less than or equal to 40, multiply the
number of hours by the pay rate to calculate regular wages.
4. If the number of hours worked is greater than 40, multiply 40 by the pay
rate to calculate regular wages, and then multiply the difference between the
number of hours worked and 40 by 1.5 times the pay rate to calculate overtime
wages
5. Add the regular wages to the overtime wages (if any) to determine total
wages for the week.
Sample data:
In one week an employee works 50 hours at the hourly pay rate of $25.00. Assume a 40-hour normal workweek and an overtime pay rate factor of 1.5. What are the employee’s wages?
40 x $
25.00 =
$ 1000.00
10 x 1.5 x $ 25.00 = $ 375.00
$ 1375.00
Weekly wages, in general:
if (hours
>= 40)
wages = (40 * payRate)
+ (hours - 40) * 1.5 * payRate
else
wages = hours * payRate
Recall our real life example:
( 40 x $ 25.00 ) + ( 10 x 1.5 x $ 25.00 ) = $1375.00
The refined algorithm to determine an employee’s weekly wages:
1. Get the employee’s hourly payRate
2. Get the hours worked this week
3. Calculate this week’s regular wages
4. Calculate this week’s overtime wages (if any)
5. Add the regular wages to overtime wages (if any) to determine total wages
for the week.
2. Implementation = translating an algorithm into a programming language (programming
language = a language with strict grammar rules, symbols, and special words
used to construct a computer program), testing, and debugging the program.
3. Maintenance = use and modify the program according to changing
requirements or errors that were discovered while using it.
(1) There are a finite number of steps.
(2) It takes a finite amount of time to execute the
steps.
(3) Each step is precise in meaning, and “doable.”
(4) It is deterministic.
(5) It solves a general class of problems.
Notes:
STEP 1: Take a coin out
of your pocket and give it to your friend
STEP 2: Repeat STEP 1
STEP 1: Make a list of
all positive integers
STEP 2: Arrange this
list in descending order (from largest to smallest)
STEP 3: Extract the
first integer from the resulting list
Answer: These instructions don't describe an algorithm because STEP 1 and STEP
2 are impossible to perform: nobody can make a list of all positive integers
(infinite list) and nobody can arrange the list in descending order starting with
the largest (what is the largest positive integer?)
In conclusion - the execution of each step in an algorithm does not
require creative skills, it requires rather only the ability to follow
directions. In specifying behavior, an algorithm must be precise, unambiguous,
complete, effective, and correct.
Very important: Once the solution algorithm is complete, it must be desk
checked. Beginning programmers are tempted to skip this testing phase;
however, this often leads to frustrations during the coding stage as the
programmer tries to juggle writing code with analyzing the program as a
whole. Remember: it is easier to find logical errors in pseudocode than
in program code.
Each computer system understands a low-level language that
is specific to that computer (machine language). Programmers usually write
programs in a high-level programming language that is easier to understand.
– Machine language: the language that can be directly used and
understood by the computer (operations are very low-level, specific to the
architecture).
– Computers can only execute machine code.
– Understood by a specific machine, so it is not portable (runs only on a
specific type of computer)
– Is made up of binary-coded instructions (strings of 0's and 1's)
– High-level languages (C++, Java, Ada,
Modula-2, Pascal, Cobol, Fortran…)
– Similar to natural language (easier to use and debug)
– Are portable (machine-independent)
– Not understood directly by a computer, so they must be converted somehow to
machine language: compilers (the whole program is translated into
machine language and then executed) and interpreters (the program is
translated and executed one line at a time; much slower than compilers). Java
uses a hybrid strategy: instead of compiling into machine language, Java
programs are compiled into an intermediate language (Java bytecodes)
that serves as the machine language for a theoretical/virtual computer (JVM
= Java Virtual Machine). Java then interprets those
programs by simulating the JVM.
The Compilation Process (C++):
The Java Interpreter:
– Generations of programming languages:
Programming languages require that we use certain structures to translate
algorithms into programs. There are 4 basic control structures common to
all programming languages (they stipulate the order in which instructions are
executed):
1.
Sequence: a
series of statements that are executed one after another
2. Selection (or branch): conditional structure; executes different statements according to certain conditions
3. Loop: repeats statements while certain conditions are met
4. Subprograms (or procedures, subroutines, functions; methods in Java): allow us to break a program into smaller units (modules). Subprogram = a meaningful collection of sequence, selection, loop, and subprogram.
5. Procedural and Object-Oriented Programming
Additional
Resources:
1. History of Computers (ENIAC): http://en.wikipedia.org/wiki/ENIAC
2.
History of Computers (hardware): http://en.wikipedia.org/wiki/History_of_computing_hardware
3. ENIAC (U.S. Army photos): 1, 2, 3, 4, 5, 6, 7, 8, 9
4. CPU components: http://computer.howstuffworks.com/microprocessor3.htm