Transcript ch01_old1

Chapter 1: Introduction to Computers
and Java Objects
 Background information
 hardware
 software
 computer languages
 compiling, interpreting and assembling
 Introduction to Java
 Object-Oriented Development
 Testing and Types of Errors
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 item to
another, and 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
A Sip of Java!
History
 Invented in 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.
 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.21
©Silberschatz, Korth and Sudarshan
Applets vs. Java Applications
 Applets:
 Java programs intended to be downloaded via the WWW and run
immediately
 “little applications”
 Typically embedded in a web-page and run in a web browser
 Applications:
 Java programs intended to be installed then run
 Often larger, more complex applications
 Applets and applications are programmed slightly differently.
1.22
©Silberschatz, Korth and Sudarshan
import java.util.Scanner;
public class FirstProgram
{
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.23
©Silberschatz, Korth and Sudarshan
Explanation of Code ...
 Code to begin the program (to be explained later):
import java.util.Scanner;
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.
 The name of the class is also the name of the file.
1.24
©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 is a class
 out an object
 println is a method that outputs something
 double-quoted text inside the parentheses is an argument to the method
 general syntax: Object_Name.Method_Name(Arguments)
1.25
©Silberschatz, Korth and Sudarshan
… Explanation of Code ...
 Code to create two variables named n1, n2 for storing two whole
numbers (integer):
int n1, n2;
 These are called “variable declarations.”
 In this program they are used to store the user’s response.
1.26
©Silberschatz, Korth and Sudarshan
… Explanation of Code ...
 Creating an object called keyboard of the Scanner class:
Scanner keyboard = new Scanner(System.in);
 System.in refers to the keyboard
 The Scanner class provides the program with access to keyboard input.
1.27
©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.28
©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.29
©Silberschatz, Korth and Sudarshan
Compiling and Running
a Java Program
 Type the program into a file:
 FirstProgram.java
 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.30
©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.31
©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.32
©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.33
©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.34
©Silberschatz, Korth and Sudarshan
Design Principles of OOP
 Three of the Main design principles of Object-Oriented
Programming (OOP):
 Encapsulation
 Polymorphism
 Inheritance
1.35
©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.36
©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.37
©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.38
©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.39
©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.40
©Silberschatz, Korth and Sudarshan
Example of an Algorithm
Algorithm to find the total cost of a list of items (modified from the book):
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) Report the final recorded sum as the answer.
1.41
©Silberschatz, Korth and Sudarshan
Program Design Process

Design THEN code THEN test (not code, then design)

Design process

1.
define the problem clearly, precisely; understand the inputs and outputs
2.
describe the algorithms for solving the problem, usually in pseudocode
3.
Design objects, methods and classes your program needs
4.
re-evaluate your solution, and iteratively improve it, eliminating errors and
inefficiencies whenever they are identified.
Writing/Coding
1.
2.
3.
4.
5.
write the code
compile the code
read the code (“structured walkthrough”)
test the code
fix any errors, and repeat
1.42
©Silberschatz, Korth and Sudarshan
Testing and Debugging
 Even with careful programming, your code will contain errors and
must be thoroughly tested.
 Bug - a mistake in a program
 Debugging - fixing mistakes in a program
1.43
©Silberschatz, Korth and Sudarshan
Types of Errors
Generally, there are three types of programming errors:
 syntax
 run-time
 logic
1.44
©Silberschatz, Korth and Sudarshan
Syntax
 The syntax of a programming language is the set of grammar rules for
that programming language.
 A syntax error is a grammatical mistake in a program.




misspelling a command, e.g., “rtrn” instead of “return”
missing variable declaration
missing punctuation
using things inappropriately, e.g., adding an integer and a character
 Syntax errors:




are caught by compiler, hence the phrase “compiler-time error”
relatively easy to fix
prevent the program from executing
frequently result in misleading error messages
1.45
©Silberschatz, Korth and Sudarshan
Run-Time Errors
 An error that is detected when you program run (or executed) is called a
run-time error*.




dividing a number by 0
accessing memory that was de-allocated
indexing out of bounds in an array
reading input from a file that is not open
 Run-time errors:
 terminate a programs normal execution
 occasionally detected by the compiler
 occur intermittently
 frequently result in misleading, incomplete or non-intelligible messages
 frequently difficult to fix
*Note: what is a run-time error in one language might be a logicerror in another
1.46
©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 that causes a program to produce incorrect results is a logic
error.
 circleArea = radius * radius;
 sum = a - b;
// pi * radius * radius
// should be sum = a + b;
 Logic-time errors:
 not detected by the compiler
 produce no run-time error messages
 Cause the program to take incorrect action taken, or produce incorrect
results/output during program execution
 occur intermittently
 frequently difficult to fix
1.47
©Silberschatz, Korth and Sudarshan
Run-time and Logic Errors
 Run-time errors and logic-errors can be INCREDIBLY damaging due to
their intermittent and after-the-fact nature.
 For that reason, it is imperative that you:
 test extensively, on a variety of different test-cases
 perform structured walk-throughs
1.48
©Silberschatz, Korth and Sudarshan
Run-Time and Logic Errors
1. Mariner Bugs Out (1962)
 Cost: $18.5 million
 Disaster: The Mariner 1 rocket with a space probe headed for
Venus diverted from its intended flight path shortly after
launch. Mission Control destroyed the rocket 293 seconds after
liftoff.
 Cause: A programmer incorrectly transcribed a handwritten
formula into computer code, missing a single superscript
bar. Without the smoothing function indicated by the bar, the
software treated normal variations of velocity as if they were
serious, causing faulty corrections that sent the rocket off
course.
1.49
©Silberschatz, Korth and Sudarshan
Run-Time and Logic Errors
2. CIA Gives the Soviets Gas (1982)
 Cost: Millions of dollars, significant damage to Soviet economy
 Disaster: Control software went haywire and produced intense
pressure in the Trans-Siberian gas pipeline, resulting in the
largest man-made, non-nuclear explosion in Earth's history.
 Cause: CIA operatives allegedly planted a bug in a Canadian
computer system purchased by the Soviets to control their gas
pipelines. The purchase was part of a strategic Soviet plan to
steal or covertly obtain sensitive U.S. technology. When the
CIA discovered the purchase, they sabotaged the software so
that it would pass Soviet inspection but fail in operation.
1.50
©Silberschatz, Korth and Sudarshan
Run-Time and Logic Errors
3. World War III… Almost (1983)
 Cost: Nearly all of humanity
 Disaster: The Soviet early warning system falsely indicated the
United States had launched five ballistic missiles. Fortunately
the Soviet duty officer had a "funny feeling in my gut" and
reasoned if the U.S. was really attacking they would launch
more than five missiles, so he reported the apparent attack as a
false alarm.
 Cause: A bug in the Soviet software failed to filter out false
missile detections caused by sunlight reflecting off cloud-tops.
1.51
©Silberschatz, Korth and Sudarshan
Run-time and Logic Errors
4. Medical Machine Kills (1985)
 Cost: Three people dead, three people critically injured
 Disaster: Canada's Therac-25 radiation therapy machine
malfunctioned and delivered lethal radiation doses to patients.
 Cause: Because of a subtle bug called a race condition, a
technician could accidentally configure Therac-25 so the
electron beam would fire in high-power mode without the proper
patient shielding.
1.52
©Silberschatz, Korth and Sudarshan
Summary
 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 (“byte-code” for java).
 Actual translation of Java byte-code to the hardware’s specific machine
code occurs at run time (it is interpreted).
1.53
©Silberschatz, Korth and Sudarshan
Summary, Cont.
 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.54
©Silberschatz, Korth and Sudarshan