Title: Overview of Computers and Programming Languages
1Overview of Computers and Programming Languages
Qi ZhangAug 25
COMP 110 Introduction to Programming
2Last Week
- Course Introduction
- Installation of IDE jGRASP
- Running the first java program
- Questions with jGRASP?
3Todays Lecture
- Before you start programming, you need to
understand how computers work - Hardware and software
- Programming languages and compilers
- Designing programs
4Hardware vs. Software
A computer is made up of hardware and software
Software
Hardware
- CPU
- e.g. Intel Core 2 Duo
- Input/Output devices
- keyboard
- monitor
- mouse
- main memory
- e.g. 2GB RAM
- secondary memory
- e.g. 120 GB hard drive
- operating systems
- Windows XP
- Mac OS X
- Unix / Linux
- applications
- games
- Microsoft Word
- Internet Explorer
5Hardware Organization
CPU
memory
motherboard
hard drive
6Central Processing UnitCPU
- "the brain" of a computer
- Arithmetic Logic Unit (ALU) .
- carries out all arithmetic and logical ops
- Microcontroller
- Fetch and decode instructions
- Control the flow of info between CPU and main
memory
CPU
Program
7Memory
- Provides permanent and temporary storage of
information - Two basic types
Auxiliary memory
Main memory
8Main Memory
- Directly connected to the CPU
- Aka Random Access Memory (RAM)
- Main memory holds
- The current program
- Data the program is using
- The results of intermediate calculations
- All programs must be brought into main memory
before execution - When power is turned off, everything in main
memory is lost
9Bit and Byte
- Ordered sequence of storage cells
- Each cell is a physical device which has two
stable states (0/1) - Bit a 0/1 digit
- Byte
- 8 bits of memory
- A quantity of memory
- 4GB main memory
- Memory Location
- Adjacent bytes
10Address
- A computers main memory is divided into
numbered bytes - Address of a Byte The number of the byte
- Address of a Memory Location The address of the
first byte
11CPU and Main Memory
All programs must be brought into main memory
before execution
CPU Executes program instructions
Main memory Primary storage area for programs
and data that are in active use
12Auxiliary Memory (Secondary Memory)
- Provides permanent storage for information
- Retains information even when power is off
- Examples of secondary storage
- Hard Disks
- Floppy Disks
- USB Flash Driver
- CD-ROMs
- Tapes
- Measured in bytes
- Java programs files are copied from auxiliary
memory to main memory in order to be run.
13Secondary Storage
Secondary memory devices provide long-term storage
Information is moved between main memory and
secondary memory as needed
14Input/Output Devices
I/O devices facilitate user interaction
Keyboard, mouse, etc
monitor, printer, etc
15Hardware vs. Software
A computer is made up of hardware and software
Software
Hardware
- CPU
- e.g. Intel Core 2 Duo
- Input/Output devices
- keyboard
- monitor
- mouse
- main memory
- e.g. 2GB RAM
- secondary memory
- e.g. 120 GB hard drive
- operating systems
- Windows XP
- Mac OS X
- Unix / Linux
- applications
- games
- Microsoft Word
- Internet Explorer
16Programs
- A program is a set of instructions for a computer
to follow - Following the instructions is called running or
executing the program.
17Running a Program
- Sometimes the computer and the program are
considered to be one unit. - Programmers typically find this view to be more
convenient.
18Questions
- Where is your Microsoft Word program stored when
your computer is powered off? - What is the first program which is running when
you first turn on the computer?
19Programming Languages
High-level Programming Languages (e.g. Java)
human readable
translated by
Compiler
Low-level language (Machine language /assembly
language)
computer readable
20Programming Languages
High-level Programming Languages (e.g. Java)
Source code
human readable
translated by
Compiler
Low-level language (Machine language /assembly
language)
Object code
computer readable
21Portability Issue
Source code
Compiler 1
Compiler n
Compiler N
Object code N
Object code n
Object code 1
Machine N
Machine 1
Machine n
- Most high-level languages need a different
compiler for each type of computer and for each
operating system. - Most compilers are very large programs that are
expensive to produce.
22Java Does It Differently
Java source code
Compiler
Java byte code
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
- Byte code provides portability
- Interpreter is much simpler compared with compiler
23Java Does It Differently
Java source code
Compiler
Java byte code
Java Virtual Machine (JVM)
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
- Byte code provides portability
- Interpreter is much simpler compared with compiler
24Java Byte Code, Java Virtual Machine
- Java byte code
- not a machine language
- generated when you compile your Java source code
- similar to the machine language of most common
computers, can be easily translated to machine
language - good 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. - Java Virtual Machine
- Translates byte code to machine language and
executes it - Java Virtual Machine is a simpler program
compared with compiler - Java Virtual Machine has to be installed to run
the byte code on any particular machine
25Questions
- In Lab 0, you worked on a program which prints
out Hello world!, recall the files generated
and the actions you took - Which is the Java source code?
- Which is the byte code, how was it generated?
- Did you install the Java Virtual Machine? When?
- Given you a Java source code, what are the steps
you need to take to run it?
26Run your Java Program
Java source code
Hello.java
Compiler
Step 1 Compile
Java byte code
Hello.class
Step 2 Run
Java Virtual Machine (JVM)
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
- Byte code provides portability
- Interpreter is much simpler compared with compiler
27Program Design
- Programming is a creative process
- Java is an Object-oriented programming language.
28Object-Oriented Programming
- Our world consists of objects (people, trees,
cars, cities, airline reservations, etc.). - Objects can perform actions which affect
themselves and other objects in the world. - People drive cars
- People make airline reservations
- Object-oriented programming (OOP) treats a
program as a collection of objects that interact
by means of actions.
29OOP Terminology
- Objects, appropriately, are called objects.
- Actions are called methods.
- Objects of the same kind have the same type and
belong to the same class. - Objects within a class have a common set of
methods and the same kinds of data - but each object can have its own data values.
30Algorithms
- An algorithm describes a strategy/plan of
performing an action. - Once an algorithm is defined, expressing it in
Java (or in another programming language) usually
is easy. - Algorithms usually are expressed in English or in
pseudocode
31Example Total Cost of All Items
- Write the number 0 on the whiteboard.
- For each item on the list
- add the cost of the item to the number on the
whiteboard - replace the number on the whiteboard with the
result of this addition. - Announce that the answer is the number written on
the whiteboard.
Class TotalCostCalculator Object
myTotalCostCalculator Method add
32OOP Design Principles
- Encapsulation.
- Polymorphism.
- Inheritance.
33Encapsulation
- Information hiding hides the detail of what is
inside the capsule - Describe a class/object by giving only enough
information to allow a programmer to use it.
34Polymorphism
- many forms
- Example.
- class car
- object myAutoCar, myManualCar
- method start, stop, turn, etc..
- More detail in Ch.7
35Inheritance
- The way of organizing classes
- Avoid repeat description
Vehicle
Automobile
Motorcycle
Bus
Automobile
Automobile
Automobile
Automobile
36Testing and Debugging
- Eliminate errors by avoiding them in the first
place. - Carefully design classes, algorithms and methods.
- Carefully code everything into Java.
- Test your program with appropriate test cases
(some where the answer is known), discover and
fix any errors, then retest.
37Errors
- An error in a program is called a bug.
- Eliminating errors is called debugging.
- Three kinds or errors
- syntax errors
- runtime errors
- logic errors
38Syntax 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
39Runtime 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
40Logic 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
41Sample Java Program p.23
- import java.util. public class
FirstProgram public static void
main(String args)
System.out.println("Hello out there.")
System.out.println("I will add two number for
you.") System.out.println("Enter two
whole number on a line") int
n1, n2 Scanner keyboard
new Scanner(System.in) n1
keyboard.nextInt() n2
keyboard.nextInt()
System.out.println("The sum of those two numbers
is") System.out.println(n1 n2)
41
42import Package
- import java.util.
- Package Library of classes which are already
defined - Syntax
- import packagename.
- The class(es) in the package is going to be used
later in the code
42
43Begin the Class
- public class FirstProgram public
static void main(String args) - Begin a definition of a class named FirstProgram
- A Java program consists of one or more classes
- Each class should be in a separate file.
- The name of the file should be the same as the
name of the class. FirstProgram.java
43
44Output to screen
- System.out.println("Hello out there.")
- System.out.println("I will add two numbers for
you.") - System.out.println("Enter two whole numbers on a
line") - Write what is in quotes to screen
- Object System.out
- Method println
44
45Invoke methods on objects
parameters)
Object.
Method (
Syntax
Invoke Method
45
46Variable
- int n1, n2
- Variable - store piece of data
- n1 - store integer
- n2 - store integer
46
47Create Scanner Object
- Scanner keyboard new Scanner(System.in)
- Create an object (keyboard) of Scanner class
- Syntax
- Classname objectname new Classname(parameters
)
47
48Call method on object
- n1 keyboard.nextInt()
- Calls the method nextInt of object keyboard
- nextInt is defined in class Scanner
- Read an integer from the keyboard and store it in
n1
Object
48
49Output to screen
- System.out.println("The sum of those two numbers
is") - System.out.println(n1 n2)
Add n1 and n2 Print the sum to the screen
49
50Next class