Transcript ch1_old
Chapter 1: Introduction to Computers
and Java Objects
Background information
hardware
software
computer languages
compiling, interpreting and assembling
object-oriented design and development
types of errors
Introduction to Java
1.1
©Silberschatz, Korth and Sudarshan
Computer Basics
Computer system:
hardware + software
Hardware - the physical components
Software - the instructions that tell the hardware what to do
1.2
©Silberschatz, Korth and Sudarshan
Common Hardware Components
Memory
(main & auxiliary)
Input
Devices
Processor
(CPU)
(such as mouse and
keyboard)
Output
Devices
(such as video
display or printer)
Processor (CPU)
Input device(s)
Central Processing Unit
mouse, keyboard, monitor,
etc.
Interprets and executes the
instructions
Output device(s)
Memory
video display, printer, etc.
main & auxiliary
holds data and instructions
1.3
©Silberschatz, Korth and Sudarshan
Classification of Memory
At a high-level there are two types of memory:
Volatile – contents are lost when power is turned off
Main memory
Cache memory
Fastest and most expensive form of memory, per byte
Non-Volatile – contents are maintained when power is turned off
Hard drive (internal or external)
CD, DVD
Floppy disk
Tape (still used extensively)
Slowest and cheapest form of memory, per byte
1.4
©Silberschatz, Korth and Sudarshan
Classification of Memory, cont.
The books breakdown:
Main
working area
temporarily stores programs and data during program execution
Also known as Random Access Memory (RAM) and also known as “primary
memory”
Auxiliary
permanent (more or less)
saves program and results
includes floppy & hard disk drives, CDs, tape, etc.
also known as “secondary memory”
1.5
©Silberschatz, Korth and Sudarshan
Memory Organization
Bit = one binary digit, either 0 or 1
Nibble = 4 bits
Byte = 8 bits
Word = machine dependant, typically 4 bytes
Larger groupings: (number of bytes)
name
approximation
exact
Kilobyte (KB)
2^10
10^3
Megabyte (MB)
2^20
10^6
Gigabyte (GB)
2^30
10^9
Terabytes (TB)
2^40
10^12
Petabyte (PB)
2^50
10^15
Exabyte (EB)
2^60
10^18
Zetabyte (ZB)
2^70
10^21
Yottabyte (YB)
2^80
10^24
1.6
©Silberschatz, Korth and Sudarshan
Main Memory Organization
Main memory (RAM) is byte
addressable:
Consists of a list of locations,
each containing one byte of
data.
Each location has an
associated “number,” which is
commonly referred to as the
“address” of the location.
The number of bytes per data
item may vary from one
computer system to another.
Address Data Byte
3021
1111 0000
3022
1100 1100
3023
1010 1010
3024
1100 1110
3025
0011 0001
3026
1110 0001
3027
0110 0011
3028
1010 0010
3029
…
1.7
Item 1: 2 bytes
stored
Item 2: 1 byte
stored
Item 3: 3 bytes
stored
Item 4: 2 bytes
stored
Next Item, etc.
©Silberschatz, Korth and Sudarshan
Auxiliary Memory Organization
(file systems for users)
Main (Root) Directory / Folder
Files
Files
Subdirectory
Subdirectory
Subdirectory
Files
Files
Subdirectory
Subdirectory
Files
Subdirectory
Note: “directory” = “folder”
Files
1.8
©Silberschatz, Korth and Sudarshan
Running (Executing) a Program
A (computer) program is a set of instructions for a
computer to follow, or rather, execute.
The term application is sometimes used to informally
refer to a computer program (we will use the term more
formally later).
Program
Data
(input for the
program)
Computer
1.9
Output
©Silberschatz, Korth and Sudarshan
Many Types of Programs
System Software - Part of a computers “infrastructure,” and necessary
for the system to operate
Operating Systems - DOS, Microsoft Windows, MacOS, Linux, UNIX, etc.
Database Systems – Oracle, IBM DB2, SQL Server, Access
Networking Software
Web Servers
Application Servers
User Applications - Not required for the system to operate
Games
Office Applications – Word, Powerpoint, Excel
Web Browsers
Text Editors – textedit, vi, emacs
1.10
©Silberschatz, Korth and Sudarshan
Various Types of User Interfaces
Command-Line:
User types in commands one line at a time
DOS (Start -> run -> cmd)
Unix xterm
GUI (Graphical User Interface)
Windows, menus, buttons, sliders, etc.
MacOS, Windows
Sometimes also called “event-driven” interfaces
Application Program Interface (API)
Allows one program to communication, interact or “interface” with another
ODBC, JDBC, Swing, AWT
1.11
©Silberschatz, Korth and Sudarshan
Programming Language Hierarchy
High-Level Language (HLL)
Assembly Language
Machine Language
Hardware
1.12
©Silberschatz, Korth and Sudarshan
The highs and lows
of programming languages ...
High-Level Language (HLL)
Machine Language (lowest level)
closest to natural language
least natural language for humans
words, numbers, and math symbols
most natural language for hardware
multi-line statements/commands
just 0s and 1s
not directly understood by hardware
directly understood by hardware
“portable” (hardware independent)
not portable (hardware dependent)
Java, C, C++, COBOL, FORTRAN,
BASIC, Lisp, Ada, etc.
A program in machine language is
frequently referred to as:
A program in a HLL is frequently
an object program
referred to as:
object code
a source program
executable program
source code
executable code
source file
executable
source
1.13
©Silberschatz, Korth and Sudarshan
Assembly Language (middle level)
Assembly Language:
A more or less human readable version of machine language
Words, abbreviations, letters and numbers replace 0s and 1s
Single-line statements/commands
Easily translated from human readable to machine executable code
Like machine code, not portable (hardware dependent)
1.14
©Silberschatz, Korth and Sudarshan
Getting from Source to Machine Code
“Compiling a program” - Translating a program in a high-level language to a
machine code program.
“Compiler” - A program that compiles programs, i.e., translates high-level
language programs to machine code.
“Assembling” - Translating a program in assemble language to a machine code
program.
“Assembler” - A program that assembles, i.e., translates assembly code
programs to machine code.
Compilers and assemblers need to know the specific target hardware
1.15
©Silberschatz, Korth and Sudarshan
Compilers vs. Assemblers vs. Interpreters
Compilers and Assemblers:
translation is a separate user step from execution
translation is “off-line,” i.e. not at run time
Entire program is translated before execution
Interpreters: (another way to translate source to object code)
translation is not a separate user step from execution
translation is “on-line,” i.e. at run time
Translation and execution occur “line at a time”
Compiler,
Source
Code
Assembler, or
Object
Code
Interpreter
1.16
©Silberschatz, Korth and Sudarshan
Java Program Translation
Executing a java program involves both compilation and interpretation.
Java Program Translation & Execution:
Step #1: A java source program is compiled; this produces a program in “Byte
Code.”
Similar to assembly code, but hardware independent.
Step #2: An interpreter, called the Java Virtual Machine (JVM) translates the
byte code program to hardware-specific machine code, and executes it (in an
interpretive manner).
1.17
©Silberschatz, Korth and Sudarshan
Java Program Translation
Data for Java Program
Java Program
Java Compiler
Byte-Code
Program
Byte-Code Interpreter
Machine-Language
Instructions
Computer Execution
of Machine-Language Instructions
Output of Java Program
1.18
©Silberschatz, Korth and Sudarshan
Why Use Byte Code?
Question: Why not compile directly to machine code, rather than byte
code?
Disadvantages of Byte Code:
requires both compiler and interpreter
slower program execution
Advantages of Byte Code:
portability
very important
same program can run on computers of different types (useful with the Internet)
A JVM (interpreter) for new types of computers can be made quickly and
inexpensively, whereas a compiler cannot; only one compiler is needed.
1.19
©Silberschatz, Korth and Sudarshan
Java Program Translation Including Linker
Java Program
Previously Compiled Helper Programs
Data for Java Program
Java Compiler
Byte-Code
Program
Byte-Code Interpreter
Machine-Language
Instructions
Class Loader (i.e., Linker)
Computer Execution
of Machine-Language Instructions
Output of Java Program
1.20
©Silberschatz, Korth and Sudarshan
The Object-Oriented (OO) Paradigm
Some terminology:
Object-oriented programming
Object-oriented (programming) language
Object-oriented design
Object-oriented database
etc.
What does the term “object-oriented” mean?
The OO paradigm is a philosophy that has had, and continues to have, an impact on
all aspects of software design and implementation.
Software can be designed an implemented in a variety of ways, and the OO paradigm
is one; you will learn others over the next few years.
Currently, the OO approach is the most widely used.
1.21
©Silberschatz, Korth and Sudarshan
Object-Oriented Programming: OOP
What is the basic idea behind the OO paradigm?
The OO paradigm is based on the idea that all aspects of software – its design,
implementation, internal structure, as well as the supporting tools and language –
should be based on the real-world objects the software is associated with.
Example - An OO software system for air traffic control would contain internal
data items that correspond directly to:
aircraft
airports
passengers
runways
etc.
1.22
©Silberschatz, Korth and Sudarshan
Object-Oriented Programming: OOP
More terminology:
object - usually a person, place or thing (a noun), not necessarily physical
attribute - a property, characteristic or data associated with an object
method - an action associated with an object (a verb), sometimes called behavior
class - a category of similar objects
Objects have both attributes and methods
Objects of the same class have the same data elements and methods
Objects are sometimes said to send and receive messages to invoke actions
A java program consists of a collection of classes, objects and methods.
1.23
©Silberschatz, Korth and Sudarshan
Example of an Object Class
Class “Automobile:”
Data Items:
Methods:
manufacturer’s name
start engine
model name
turn engine off
year made
accelerate
color
decelerate
number of doors
engage cruise control
size of engine
display error code
etc.
adjust fuel mixture
etc.
1.24
©Silberschatz, Korth and Sudarshan
Design Principles of OOP
Three of the Main design principles of Object-Oriented
Programming (OOP):
Encapsulation
Polymorphism
Inheritance
1.25
©Silberschatz, Korth and Sudarshan
Encapsulation
A piece of software can frequently be used without knowing the details
of how it works.
Relatively small, well-defined and closely related “chunks” of software
can be packaged together (i.e., encapsulated) for use by other larger
“chunks” of software.
Analogy: In order to drive a car (generally):
You don’t need to know:
how many cylinders the engine has
whether the breaks are disk breaks or drum breaks
You do need to know:
Where the controls are and how to use them
What type of fuel
1.26
©Silberschatz, Korth and Sudarshan
Encapsulation
A better analogy: The transmission manufacturer:
doesn’t need to know:
the size of the cylinders in the engine
the size of the oil pan for the engine
does need to know:
specifications of the connections to the engine
range of torque and acceleration of the engine
One more analogy: the waiter vs. the cook
The book also calls this information hiding
1.27
©Silberschatz, Korth and Sudarshan
Polymorphism
Polymorphism—the same word or phrase can be mean different things
in different contexts
Analogy: in English, bank can mean:
side of a river or
a place to put money
Determining the correct meaning requires context, i.e., you have to see
it in a sentence.
In Java, two or more methods could be called “output.”
Which specific method is being invoked, and what it does, depends on
the context of the method call.
1.28
©Silberschatz, Korth and Sudarshan
An Inheritance Hierarchy
Inheritance—a way of organizing classes.
Classes with attributes (and methods) in common can be grouped so
that their common attributes are only defined once.
Vehicle
Automobile
Sedan
Sports Car
Motorcycle
School Bus
Bus
Luxury Bus
What properties does each vehicle inherit from the types of vehicles
above it in the diagram?
1.29
©Silberschatz, Korth and Sudarshan
Algorithms
An algorithm is a set of instructions (steps) for solving a problem:
each step must be clear and precise
each step must require finite resources
Inputs and outputs must be specified precisely
the algorithm must be complete
Analogous to a recipe.
May be in a number of different formats:
natural language (such as English)
a diagram, such as a flow chart
a specific programming language
pseudocode – a mix of natural and programming languages
1.30
©Silberschatz, Korth and Sudarshan
Example of an Algorithm
Algorithm that determines the total cost of a list of items:
Input: A list of item prices.
Output: The total cost of all the items.
1) Record (on a blackboard or piece of paper) an initial sum of 0.
2) Do the following for each item on the list:
a) Add the cost of the item to the sum.
b) Replace the previously recorded value by this new sum.
3) Output the final sum.
1.31
©Silberschatz, Korth and Sudarshan
Program Design Process
Design, then code (not code, then design)
Design process
1.
2.
3.
4.
define the problem clearly
design objects your program needs
develop algorithms for the methods of objects
describe the algorithms, usually in pseudocode
Writing/Coding
1.
2.
3.
write the code
test the code
fix any errors and retest
1.32
©Silberschatz, Korth and Sudarshan
Testing and Debugging
Even with careful programming, your code could still contain errors
and must be thoroughly tested.
Bug—a mistake in a program
Debugging—fixing mistakes in a program
1.33
©Silberschatz, Korth and Sudarshan
Types of Errors
Syntax
Run-Time
Logic
1.34
©Silberschatz, Korth and Sudarshan
Syntax
Syntax: the set of grammar rules for a programming language.
The compiler checks your program to make sure it follows the
grammar/syntax
Violating the syntax => error
1.35
©Silberschatz, Korth and Sudarshan
Syntax Errors
caught by compiler (“compiler-time error”)
automatically found, usually the easiest to fix
cannot run program until all syntax errors are
fixed
error message may be misleading
Example:
Misspelling a command, for example “rtrn”
instead of “return”
1.36
©Silberschatz, Korth and Sudarshan
Run-Time Errors
An execution error (during run-time)
The program cannot continue to run
Not always so easy to fix
Error message may or may not be helpful
Not detected by the compiler.
Example:
Division by zero - if your program attempts to divide
by zero it automatically terminates and prints an error
message.
1.37
©Silberschatz, Korth and Sudarshan
Logic Errors
Just because it compiles and runs without getting an
error message does not mean the program is correct!
An error in the design (the algorithm) or its implementation
Program compiles without errors
no run-time error messages
but incorrect action or data occurs during execution
Generally the most difficult to find and fix
Need to be alert and test thoroughly
think about test cases and predict results before executing the code
1.38
©Silberschatz, Korth and Sudarshan
Logic Error Examples
Algorithm Error:
circleArea = radius * radius;
(pi * radius * radius)
Implementation Error:
typed in wrong symbol in source code sum = a - b;
(should be sum = a + b;)
1.39
©Silberschatz, Korth and Sudarshan
Finally! Now, a taste of Java!
History
1991 - James Gosling, Sun Microsystems, Inc.
originally a language for programming home
appliances
later (1994) used for World Wide Web applications
byte code can be downloaded and run without compiling it
eventually used as a general-purpose
programming language (it is object-oriented)
Why the name “Java”? Not sure - it may just be a
name that came during a coffee break and it had
not been copyrighted, yet.
1.40
©Silberschatz, Korth and Sudarshan
Applets vs. Java Applications
Applets
Java programs intended to be downloaded via the WWW and run
immediately
“little applications”
run in a web browser
Applications
Java programs intended to be installed then run
often larger applications
Slightly different programming for each
1.41
©Silberschatz, Korth and Sudarshan
import java.util.*;
public class FirstProgram
A Sample Java Program
{
public static void main(String[] args)
{
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:");
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);
}
1.42
©Silberschatz, Korth and Sudarshan
Explanation of Code ...
Code to begin the program (to be explained
later):
public class FirstProgram
{
public static void main(String[ ] args)
{
Java applications all have similar code at the
beginning
The name of the class differs from one program to another.
1.43
©Silberschatz, Korth and Sudarshan
Explanation of Code ...
display text strings to the 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.");
Note the “dot” operator
System.out is an object
println is a method that it carries out
double-quoted text inside the parentheses is an argument to the method
general syntax: Object_Name.Method_Name(Arguments)
1.44
©Silberschatz, Korth and Sudarshan
… Explanation of Code ...
Code to create two variables named n1, n2 to
contain two whole numbers (integer):
int n1, n2;
They store the user’s response.
1.45
©Silberschatz, Korth and Sudarshan
… Explanation of Code ...
Creating an object called keyboard of the
Scanner class:
Scanner keyboard = new Scanner(System.in);
System.in is the keyboard, but the Scanner
class has easier methods to use.
1.46
©Silberschatz, Korth and Sudarshan
… Explanation of Code ...
Read two integers typed in from the keyboard and
store them in the variables n1 and n2:
n1 = keyboard.nextInt();
n2 = keyboard.nextInt();
1.47
©Silberschatz, Korth and Sudarshan
… Explanation of Code
Printing the sum to the console:
System.out.println(“The sum of those two numbers is:");
System.out.println(n1 + n2);
1.48
©Silberschatz, Korth and Sudarshan
Compiling and Running
a Java Program
Compile
javac <file>.java
Run (and link)
java <file>
<file> must have a main method
BlueJ has two similar steps by mouse clicking (discussed in the
labs).
1.49
©Silberschatz, Korth and Sudarshan
Summary
Part 1
A computer’s main memory holds both the program
that is currently running and its data.
Main memory is a series of numbered locations, each
one containing a single byte.
Auxiliary memory is for more or less permanent
storage.
A compiler is a program that translates a high-level
language, like java, into a lower level format (“bytecode” for java).
Actual translation of Java byte-code to the
hardware’s specific machine code occurs at run time
(it is interpreted).
1.50
©Silberschatz, Korth and Sudarshan
Summary
Part 2
An algorithm is a set of instructions for solving a
problem (it must be complete and precise).
An object is something that has both data and actions
(methods) associated with it.
A class defines a type of object; all objects of the same
class have the same methods.
Three OOP design principles are encapsulation,
polymorphism, and inheritance.
In a java program, a method invocation has the general
form Object_Name.Method_Name(Arguments)
1.51
©Silberschatz, Korth and Sudarshan