Computer Programming I
Chapter 1
An Introduction to Computers and
Java
1
What is a computer?
2
What is a Computer?
A computer is a machine that performs
computations, logical operations, or more
generally, data manipulation according to some
prescribed sequence of instructions called a
computer program.
The physical components of a computer are termed
hardware and the programs software.
3
The Hardware
The major hardware components:
The central processing unit (CPU)
Primary or random access memory (RAM)
Secondary or long term memory
Input and output devices (I/O devices)
4
Computer Companonts
5
The Central Processing Unit
The CPU does:
the computing
the processing
the bulk of the work
Important components of the CPU:
arithmetic and logic unit (ALU)
control unit (CU)
clock
6
The Central Processing Unit
The ALU performs calculations, billions per
second
The CU controls or coordinates which calculations
the ALU performs
The CPU clock determines how frequently the
computer hardware executes instructions.
A system’s hardware components are synchronized
with the clock. Every time the clock ticks, another
hardware action occurs.
7
Primary or Random Access Memory
How Data is Stored
binary format
a sequence of 0’s and 1's called bits.
ASCII encoding:
‘a’ is represented by 01100001
‘b’ is encoded as 01100010
A sequence of eight bits is called a byte.
8
Where Data is Stored
When the CPU executes a program, the program
instructions, along with relevant data, are stored in
primary memory.
Primary memory is also known as random access
memory (RAM) because data may be retrieved or
accessed in random, rather than sequential, order.
9
Where Data Is Stored
You can conceptualize
RAM as a collection of
storage cells or boxes,
each capable of
holding just a single
byte of information.
A unique number, or
memory address,
identifies each such
storage cell.
10
Secondary Memory
Long term
Permanent storage
Secondary memory devices:
hard disks
tapes
CDs
flash memory sticks.
11
Secondary Memory
The programs that you use every day such as word
processors, spreadsheets, and games are
permanently stored on secondary storage devices.
Compared to RAM, secondary memory is, in
general, cheaper (per bit), slower, larger,
electromechanical rather than electronic, and
persistent: secondary memory devices do not lose
their values when you turn off the computer.
12
Input/Output Devices
Standard input devices: Output devices:
keyboards monitors
mouses printers
joysticks speakers
stylus pens
cameras
microphones
13
The Software
The programs that run on a computer are collectively
known as software. Word processors, internet
browsers, editors, database management systems,
computer games, and spreadsheets are all part of
your computer's software library.
When you turn on or boot your computer, a program
called the operating system(OS)automatically runs.
This special program provides an interface between
you and your computer.
14
Computer Software
Application Software
Refers to programs that are developed to solve some
specific problems.
There are two types of application software:
application program to solve special classes of problems
Application programs that you can write to solve your
own problems.
Examples of application software:
word processing
database programs
spreadsheets
graphic programs
15
Computer Software
System Software
Refers to programs that make the computer usable
and accessible to the developers and programmers of
applications software.
Examples of system software:
Operating systems
Language translator
Linker
Loader
Preprocessors
16
Programming Languages
Programming Language is an agreed upon
format of symbols that allow a programmer to
instruct a computer to perform certain
predefined tasks.
Provide features to support the data processing
activities, which include declaring variables,
evaluating numeric expressions, assigning
values to variables, reading and writing data to
devices, looping and making decisions.
17
Programming Languages
a. Machine Languages
Is the natural language of a computer.
Does not need to translate and is ready for immediate
execution.
Machine language instruction is a binary string of 0s and
1s.
010 1 1000 0001 0000 1100 0000 0001 0000
Are machine-dependent - each computer type has its own
machine language.
Programs written in machine languages are not portable
because programs written in for one type of computer
cannot be run on another type
18
Programming Languages
Machine Language
Each CPU executes instructions encoded in its
own unique native machine language.
A hypothetical instruction for adding one number
to another might have the form:
10010010 00000001 00000001 10101101
19
Programming Languages
Assembly Languages
Consists of English-like abbreviations.
Easier to understand.
L 1, GROSSPAY
S 1, TAX
ST 1, NETPAY
Program written in assembly languages cannot be directly
processed by a computer.
Must use language translators, called assemblers, to convert
them to machine code.
Disadvantages:
In general, each assembly language instruction corresponds to
one machine language instruction. Therefore, the programs
written in them are lengthy.
Because of variations in assembly languages, programs written
using them are not portable. 20
Programming Languages
High-Level languages
Instructions are quite English-like, and a single
instruction can be written to correspond to many
operations at the machine level.
For example, the assembly language program can be
written in high-level languages as follows:
Netpay = gross pay – tax
Are easier to learn than machine or assembly
languages.
Have to be converted to machine languages before
they can be executed using compilers, system software
that translates a source program into an almost
executable object program
21
Programming Languages
For example:
COBOL – developed in the 1960s for business
transactions.
FORTRAN – developed for mathematic calculations.
Pascal - is a structured high-level language.
C – is designed to support only procedure-oriented
programming. Popular language for developing system
applications such as operating system and compilers.
Java – is an object-oriented language.
C++ - is extension of C programming language that
support object oriented programming and procedure-
oriented approach.
22
Programming Languages
In the 1960s, the first high-level language,
FORTRAN, was invented and no longer were
programmers forced to devise programs with binary
instructions. FORTRAN instructions use an
English-like syntax.
Today, hundreds of high-level languages are
available, with dozens in mainstream use, including:
Fortran 2003, COBOL, Lisp, Visual BASIC, C, C++,
C#, Java, Perl, Python, PHP, and Javascript.
23
Programming Languages
A typical instruction coded in a high-level
language:
if income > 1000000 then
print "You are rich!"
24
The Compiler
The program must be translated into the machine
language of that computer.
Accepts a program written in a high-level language and
produces a translation into the target machine language.
The Compilation Process
source file
source
source file
object file
object
object file file
#include <stdio.h> 0100100101011001000
1000010100011101011
main() { 0110100111010101100
printf("hello\n");
} #include <stdio.h>
executable file
main() {
compiler executable file
printf("hello\n"); executable file
}
0100100101011001000
1000010100011101011
0110100111010101100
1001011010110001011
0100100101001011011
0101101011010100101
other object files linker
and libraries
other
other object
objectfiles
files
and libraries
and libraries
010010010110010110
101100010110010111
010110101101010010
Java
General-purpose language developed by Sun
Microsystems in the early 1990s. Three main goals
for their new language:
Platform Independence - Java programs should
be capable of running on any computer.
Security - Java programs should not be susceptible
to hackers' code and dangerous viruses.
Reliability - Java programs should not "crash.”
27
The Java Virtual Machine
In order to make Java a cross-platform
programming language, Java's creative team
designed an abstract computer implemented in
software called the Java Virtual Machine (JVM).
You install software on your computer that
simulates a JVM computer. The machine language
of the JVM is called bytecode. Java programs are
first compiled into bytecode, and then executed.
28
The Java Virtual Machine
The Java interpreter, which is part of the JVM,
executes each bytecode instruction, one by one.
Once a Java program is translated into
bytecode, the bytecode can run on any
computer that has installed the JVM. A Java
program needs to be compiled into bytecode
just once.
The Java Interpreter
source file class file JAR archive
import acm.program.*; CA FE BA BE 00 03 00 CA FE BA BE 00 03 00
00 16 07 00 1A 07 00 00 16 07 00 1A 07 00
public class Hello 00 04 00 07 0C 00 13 00 04 00 07 0C 00 13
public void run() { 01 00 16 2B 4C 6A 61 01 00 16 2B 4C 6A 61
println("hello"); 47 72 61 70 68 69 63
} 2D 00 1F 08 00 0F 07
} 14 0A 00 02 00 08 0A
compiler linker 00 18 0C 00 17 00 1C
other class files
47 72 61 70 68 69 63
JVM
2D 00 1F 08 00 0F 07
14 0A 00 02 00 08 0A
00 18 0C 00 17 00 1C
hello
hello
30
Programming and Algorithms
An algorithm is a finite, step-by-step procedure for
accomplishing some task or solving a problem.
The study of algorithms is a cornerstone of
computer science.
A programming language is your tool, a tool that
you can use to investigate and implement
algorithms.
31