2. GOALS
To understand the activity of programming
To learn about the components of computers
To learn about level of programming languages
To become familiar with your computing
environment and your compiler
To compile and run your first Java program
To recognize 3 types of errors
3. What is Programming?
Computer programming
the art and science of designing and writing
computer programs.
Computer program
a sequences of instructions written in a
programming language to achieve a task/to solve
a problem.
4. What is a computer?
Hardware
the physical, tangible parts of a computer
keyboard, monitor, disks, wires, chips, etc.
Software
programs and data
a program is a series of instructions
A computer requires both hardware and software
Each is essentially useless without the other
7. Hardware Components
of a Computer – motherboard
processor chip
adapter cards
memory chips
memory slots
motherboard
Expansion
slots for
adapter cards
8. Hardware Components of a Computer
CPU
Brain of the
computer, most
expensive, the
faster computer.
CPU components–
control unit,
program counter,
register instruction,
arithmetic logic
unit, accumulator.
RAM
Temporary memory,
volatile, directly
connected to the
CPU, using
memory cells unit.
9. SECONDARY STORAGE
•Provides permanent storage for
information.
•Examples of secondary storage:
•Hard disks
•Floppy disks
•Zip disks
•CD-ROMs
•Tapes.
Hardware Components of a Computer
10. RAM vs Secondary Storage
Primary memory
volatile
Fast
Expensive
Low capacity
Works directly with the processor
Secondary Storage
Nonvolatile
Slow
Cheap
Large capacity
Not connected directly to the processor
13. CPU and Main Memory
Central
Processing
Unit
Main
Memory
Chip that executes
program commands
Eg.
Intel Pentium 4
Sun ultraSPARC III
Primary storage area
for programs and data
that are in active use
Synonymous with
RAM
14. Secondary Memory Devices
Central
Processing
Unit
Main
Memory
Floppy Disk
Hard Disk
Secondary memory
devices provide
long-term storage
Information is moved
between main memory
and secondary memory
as needed
Hard disks
Floppy disks
USB drives
Writable CDs
Writable DVDs
Tapes
15. Input / Output Devices
Central
Processing
Unit
Main
Memory
Floppy Disk
Hard Disk
Monitor
Keyboard
I/O devices facilitate
user interaction
Monitor screen
Keyboard
Mouse
Joystick
Bar code scanner
Touch screen
16. Software
In contrast to hardware, software is an abstract, intangible
entity.
Software can be categorized as system or application
software (refer next slide)
It consists of program and data to be used to perform
certain tasks
A program is a sequence of simple steps and operations,
stated in a precise language that the hardware can interpret
The process of programming involve algorithm design &
coding.
17. Software Categories
System Software
Systems programs keep all the hardware and software running
together smoothly
The most important system software is the operating system (OS)
controls all machine activities
provides the user interface to the computer
manages resources such as the CPU, memory & I/O
Windows XP, Unix, Linux, Mac OS
Application Software
generic term for any other kind of softwares
word processors, Spreadsheets, Web browsers, games
18. Algorithm
Algorithm refers to the strategy to solve a problem
It is a clear step by step sequence of instructions that
describes how to accomplish a certain task.
2 ways can be used to represent algorithm:
a) pseudocode
- using english-like-phrases to describe the
algorithm
b) flowchart
-using diagrams that employ the
symbol to describe the algorithm.
19. Coding
Algorithm need to be translated into computer
language so that it can be executed
Coding refers to the process of expressing
algorithm in a programming language
The product of coding is a program.
The act of carrying out the instructions contained
in a program is called program execution
A computer program is stored internally as a
series of binary numbers known as the machine
language of the computer
20. Digital Information
Computers store all information digitally:
numbers
text
graphics and images
video
audio
program instructions
In some way, all information is digitized - broken
down into pieces and represented as numbers
21. Representing Text Digitally
For example, every character is stored as a
number, including spaces, digits, and punctuation
Corresponding upper and lower case letters are
separate characters
H i , H e a t h e r .
72 105 44 32 72 101 97 116 104 101 114 46
22. p. 4.15 Fig. 4-16 Next
The ASCII data set 128 characters (0 until 127)
Character A in ASCII 01000001
American Standard Code for Computer
InterChange (ASCII)
23. Binary Numbers
Once information is digitized, it is represented
and stored in memory using the binary number
system
A single binary digit (0 or 1) is called a bit
A byte consists of 8 bits.
Each byte in main memory resides at a
numbered location called its address.
24. Memory
24
Main memory is divided
into many memory
locations (or cells)
9278
9279
9280
9281
9282
9283
9284
9285
9286
Each memory cell has a
numeric address, which
uniquely identifies it
27. Storage Capacity
Every memory device has a storage capacity,
indicating the number of bytes it can hold
Capacities are expressed in various units:
27
KB 2
10
= 1024
MB 2
20
(over 1 million)
GB 2
30
(over 1 billion)
TB 2
40
(over 1 trillion)
Unit Symbol Number of Bytes
kilobyte
megabyte
gigabyte
terabyte
28. Language Levels
Levels of programming language levels:
machine language
assembly language
high-level language
Each type of CPU has its own specific
machine language
The other levels were created to make it easier
for a human being to read and write programs
28
30. Programming Languages
Each type of CPU executes instructions only in a
particular machine language
A program must be translated into machine
language before it can be executed
A compiler is a software tool which translates from
high level language into a specific machine
language
An assembler translates from assembly language
into a specific machine language
30
31. Language Description Examples Translator
Machine Instruction in 0
and 1 bits
0111000011000
001
None
Assembly Instruction in
mnemonic code
LOAD 3
STOR 4
ADD
assembler
High-level Similar to human
language,
FORTRAN,
COBOL, Pascal,
C, C++, Java…
sum = 4 + 3; Compiler &
interpreter
Programming Languages
32. The Java Programming Language
Created by Sun Microsystems, Inc.
introduced in 1995 and it's popularity has
grown quickly since
Rich library
Platform-independent ("write once, run
anywhere") or architecture-neutral
33. Java Translation
The Java compiler translates Java source code
into a special representation called bytecode
Java bytecode is not the machine language for any
traditional CPU
Another Java software tool, called an interpreter
(or Java Virtual Machine (JVM)) , translates
bytecode into machine language and executes it
33
35. Portability
After compiling a Java program into byte-code, that
byte-code can be used on any computer with a byte-
code interpreter and without a need to recompile.
Byte-code can be sent over the Internet and used
anywhere in the world.
This makes Java suitable for Internet applications.
36. Becoming Familiar with your
Computer to use Java
Understand files and folders/directories
Locate the Java compiler/ Install J2SE
Set path & Java class path
Write a simple program (later)
Save your work
Compile & run
Use Dos Command Prompt or IDE
39. File Hello.java
1 public class Hello
2 {
3 public static void main(String[] args)
4 {
5 // display a greeting in the console window
6 System.out.println("Hello, World!");
7 }
8 }
40. Java Program Elements
A Java program is made up of class definitions.
A class definition must contains a header and a
body.
A class contains zero or more methods
A method is a named section of code that also has
a header & body
A method contains program statements
Single-line (starts with //) and multi-line (enclosed
by /* and */) comments are used to document the
code
41. Java Program Structure
41
public class Hello
{
}
// comments about the class
class header
class body
//Comments can be placed almost anywhere
42. Java Program Structure
42
public class MyProgram
{
}
// comments about the class
public static void main (String[] args)
{
}
// comments about the method
method header
method body
43. Compiling and Running
Type program into text editor
Save (file name must be similar to class name)
Open Dos Window
Change directory to saved file directory
Compile into byte codes
javac Hello.java
Execute byte codes
java Hello
45. Class Loader
A Java program typically consists of several pieces
called classes.
Each class may have a separate author and each is
compiled (translated into byte-code) separately.
A class loader (called a linker in other programming
languages) automatically connects the classes
together and loads the compiled code (bytecode) into
main memory.
46. JVM
Create/modify source code
Source code
Compile source code
Byte code
Run byte code
Output
Syntax errors
Runtime errors or
incorrect results
Creating a Java Program…
47. Errors
It is common for programmer to make mistake
in a program.
Three kinds of errors
Syntax errors
Runtime errors
Logic errors
48. Syntax Errors
Grammatical mistakes in a program
The grammatical rules for writing a program are very
strict
The compiler catches syntax errors and prints
an error message.
Example: using a period where a program
expects a semicolon
System.out.print("..."),
System.out.print("Hello);
49. Runtime Errors
Errors that are detected when your program is
running, but not during compilation
When the computer detects an error, it
terminates the program and prints an error
message.
Example: attempting to divide by 0
50. Logic Errors
Errors that are not detected during compilation or
while running, but which cause the program to
produce incorrect results
Example: an attempt to calculate a Fahrenheit
temperature from a Celsius temperature by
multiplying by 9/5 and adding 23 instead of 32
E.g
System.out.print("Hell");