+ All Categories
Home > Documents > ©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects...

©Silberschatz, Korth and Sudarshan1.1 Chapter 1: Introduction to Computers and Java Objects...

Date post: 27-Dec-2015
Category:
Upload: evan-eaton
View: 218 times
Download: 0 times
Share this document with a friend
51
©Silberschatz, Korth and Sudarsha 1.1 Chapter 1: Introduction to Computers Chapter 1: Introduction to Computers and Java Objects and Java Objects Background information hardware software computer languages compiling, interpreting and assembling object-oriented design and development types of errors Introduction to Java
Transcript

©Silberschatz, Korth and Sudarshan1.1

Chapter 1: Introduction to ComputersChapter 1: Introduction to Computersand Java Objectsand Java Objects

Background information hardware

software

computer languages

compiling, interpreting and assembling

object-oriented design and development

types of errors

Introduction to Java

©Silberschatz, Korth and Sudarshan1.2

Computer BasicsComputer Basics

Computer system: hardware + software

Hardware - the physical components

Software - the instructions that tell the hardware what to do

©Silberschatz, Korth and Sudarshan1.3

Common Hardware ComponentsCommon Hardware Components

Input device(s) mouse, keyboard,

monitor, etc.

Output device(s) video display,

printer, etc.

Memory(main & auxiliary)

Processor(CPU)

Input Devices

(such as mouse and keyboard)

Output Devices(such as video

display or printer)

Processor (CPU) Central Processing

Unit

Interprets and executes the instructions

Memory main & auxiliary

holds data and instructions

©Silberschatz, Korth and Sudarshan1.4

Classification of MemoryClassification 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

©Silberschatz, Korth and Sudarshan1.5

Classification of Memory, cont.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”

©Silberschatz, Korth and Sudarshan1.6

Memory OrganizationMemory 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

©Silberschatz, Korth and Sudarshan1.7

Main Memory OrganizationMain 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 Item 1: 2 bytes stored

3022 1100 1100

3023 1010 1010 Item 2: 1 byte stored

3024 1100 1110 Item 3: 3 bytes stored

3025 0011 0001

3026 1110 0001

3027 0110 0011 Item 4: 2 bytes stored

3028 1010 0010

3029 … Next Item, etc.

©Silberschatz, Korth and Sudarshan1.8

F ile s

F ile s

F ile s

F ile s

S u b d ire c to ry

S u b d ire c to ry F ile s

S u b d ire c to ry

F ile s

S u b d ire c to ry

S u b d ire c to ry S u b d ire c to ry

M a in (R o o t) D ire c to ry / F o ld er

Auxiliary Memory Organization(file systems for users)

Note: “directory” = “folder”

©Silberschatz, Korth and Sudarshan1.9

Running (Executing) a ProgramRunning (Executing) a Program

Program

Computer

Data(input for

the program)

Output

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).

©Silberschatz, Korth and Sudarshan1.10

Many Types of ProgramsMany 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

©Silberschatz, Korth and Sudarshan1.11

Various Types of User InterfacesVarious 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

©Silberschatz, Korth and Sudarshan1.12

Programming Language HierarchyProgramming Language Hierarchy

Hardware

Machine Language

Assembly Language

High-Level Language (HLL)

©Silberschatz, Korth and Sudarshan1.13

The highs and lowsThe highs and lowsof programming languages ...of programming languages ...

High-Level Language (HLL) closest to natural language

words, numbers, and math symbols

multi-line statements/commands

not directly understood by hardware

“portable” (hardware independent)

Java, C, C++, COBOL, FORTRAN, BASIC, Lisp, Ada, etc.

A program in a HLL is frequently referred to as: a source program

source code

source file

source

Machine Language (lowest level) least natural language for humans

most natural language for hardware

just 0s and 1s

directly understood by hardware

not portable (hardware dependent)

A program in machine language is frequently referred to as: an object program

object code

executable program

executable code

executable

©Silberschatz, Korth and Sudarshan1.14

Assembly Language (middle level)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)

©Silberschatz, Korth and Sudarshan1.15

Getting from Source to Machine CodeGetting 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

©Silberschatz, Korth and Sudarshan1.16

Compilers vs. Assemblers vs. InterpretersCompilers 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,

Assembler, or

Interpreter

SourceCode

ObjectCode

©Silberschatz, Korth and Sudarshan1.17

Java Program TranslationJava 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).

©Silberschatz, Korth and Sudarshan1.18

Java Program TranslationJava Program Translation

Java Program Data for Java Program

Java Compiler

Byte-CodeProgram

Byte-Code Interpreter

Machine-LanguageInstructions

Computer Executionof Machine-Language Instructions

Output of Java Program

©Silberschatz, Korth and Sudarshan1.19

Why Use Byte Code?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.

©Silberschatz, Korth and Sudarshan1.20

Java Program Data for Java Program

Java Compiler

Byte-CodeProgram

Byte-Code Interpreter

Machine-LanguageInstructions

Computer Executionof Machine-Language

Instructions

Output of Java Program

Class Loader (i.e., Linker)

Previously Compiled Helper Programs

Java Program Translation Including Linker

©Silberschatz, Korth and Sudarshan1.21

The Object-Oriented (OO) ParadigmThe 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.

©Silberschatz, Korth and Sudarshan1.22

Object-Oriented Programming: OOPObject-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.

©Silberschatz, Korth and Sudarshan1.23

Object-Oriented Programming: OOPObject-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.

©Silberschatz, Korth and Sudarshan1.24

Example of an Object ClassExample of an Object Class

Class “Automobile:”

Data Items: manufacturer’s name

model name

year made

color

number of doors

size of engine

etc.

Methods: start engine

turn engine off

accelerate

decelerate

engage cruise control

display error code

adjust fuel mixture

etc.

©Silberschatz, Korth and Sudarshan1.25

Design Principles of OOPDesign Principles of OOP

Three of the Main design principles of Object-Oriented Programming (OOP): Encapsulation

Polymorphism

Inheritance

©Silberschatz, Korth and Sudarshan1.26

EncapsulationEncapsulation

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

©Silberschatz, Korth and Sudarshan1.27

EncapsulationEncapsulation

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

©Silberschatz, Korth and Sudarshan1.28

PolymorphismPolymorphism

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.

©Silberschatz, Korth and Sudarshan1.29

An Inheritance HierarchyAn Inheritance Hierarchy

Vehicle

Automobile Motorcycle Bus

Sedan Sports Car School Bus Luxury Bus

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.

What properties does each vehicle inherit from the types of vehicles above it in the diagram?

©Silberschatz, Korth and Sudarshan1.30

AlgorithmsAlgorithms

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

©Silberschatz, Korth and Sudarshan1.31

Example of an AlgorithmExample 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.

©Silberschatz, Korth and Sudarshan1.32

Program Design ProcessProgram Design Process

Design, then code (not code, then design)

Design process1. define the problem clearly2. design objects your program needs3. develop algorithms for the methods of objects4. describe the algorithms, usually in pseudocode

Writing/Coding1. write the code2. test the code3. fix any errors and retest

©Silberschatz, Korth and Sudarshan1.33

Testing and DebuggingTesting 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

©Silberschatz, Korth and Sudarshan1.34

Types of ErrorsTypes of Errors

Syntax

Run-Time

Logic

©Silberschatz, Korth and Sudarshan1.35

SyntaxSyntax

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

©Silberschatz, Korth and Sudarshan1.36

Syntax ErrorsSyntax 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”

©Silberschatz, Korth and Sudarshan1.37

Run-Time ErrorsRun-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.

©Silberschatz, Korth and Sudarshan1.38

Logic ErrorsLogic 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

©Silberschatz, Korth and Sudarshan1.39

Logic Error ExamplesLogic 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;)

©Silberschatz, Korth and Sudarshan1.40

Finally! Now, a taste of Java!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.

©Silberschatz, Korth and Sudarshan1.41

Applets vs. Java ApplicationsApplets 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

©Silberschatz, Korth and Sudarshan1.42

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 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);

}

}

A Sample Java ProgramA Sample Java Program

©Silberschatz, Korth and Sudarshan1.43

Explanation of Code ...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.

©Silberschatz, Korth and Sudarshan1.44

Explanation of Code ...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)

©Silberschatz, Korth and Sudarshan1.45

… … Explanation of Code ...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.

©Silberschatz, Korth and Sudarshan1.46

… … Explanation of Code ...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.

©Silberschatz, Korth and Sudarshan1.47

… … Explanation of Code ...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();

©Silberschatz, Korth and Sudarshan1.48

… … Explanation of CodeExplanation of Code

Printing the sum to the console:

System.out.println(“The sum of those two numbers is:");

System.out.println(n1 + n2);

©Silberschatz, Korth and Sudarshan1.49

Compiling and Running Compiling and Running a Java Programa 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).

©Silberschatz, Korth and Sudarshan1.50

SummarySummaryPart 1Part 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 (“byte-code” for java).

Actual translation of Java byte-code to the hardware’s specific machine code occurs at run time (it is interpreted).

©Silberschatz, Korth and Sudarshan1.51

SummarySummaryPart 2Part 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)


Recommended