Introduction to Computer Science Basic Elements of Java A Look at Hardware and Software Unit 5.

129
Introduction to Computer Science Basic Elements of Java A Look at Hardware and Software Unit 5
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    0

Transcript of Introduction to Computer Science Basic Elements of Java A Look at Hardware and Software Unit 5.

Introduction to Computer Science

• Basic Elements of Java

• A Look at Hardware and Software

Unit 5Unit 5

5- 2

What this Course is Not

• This is not a course on Java, even though we spend time learning this language

• It’s a course introducing Computer Science, which also means introducing programming (principles are better understood when they are concrete)

• Java is a means to what we want to accomplish, not an end in itself

5- 3

Course Mission Statement

“To give the student the tools to develop correct, efficient, well-structured, and stylish programs, and to build a foundation for further studies in Computer Science.”-An Introduction to Computer Science with Java

Kamin, Mickunas, and Reingold

5- 4

Why We Like Java

• Java is Object Oriented to the core

• Java is, in certain key ways, simpler than other Object Oriented languages (like C++)

• Java is well-suited to the Internet

• Java is cross-platform

• Java’s popularity creates its own momentum

5- 5

A Simple Program

• A Java application:

class Hello {

public static void main (String[ ] args) { // Say hello. System.out.println(“Hello!”); }

}

5- 6

The Way You’ll Type It (sometimes I’ll do it this way, too)

• A Java application:import intro2cs.utils.*;

class Hello { public static void main (String[ ] args) { // Say hello. System.out.println(“Hello!”); WaitKey.waitKey(); }}

5- 7

Take it Apart

import intro2cs.utils.*;

class Hello { public static void main (String[ ] args) { // Say hello. System.out.println(“Hello!”); WaitKey.waitKey(); }}

import some classes

class headingmethod heading

print out “Hello!”wait for user to press keybefore finishing

comment

5- 8

Java Cares About File Names

• Type the program into a file named Hello.java (case matters)

• Java demands that the class name (here, “Hello”) must also be the base name of the file into which you put it

• The extension of the file must be .java

• Hence, Hello.java

• Each Class Requires a Separate File

5- 9

Steps to Nirvana

• Type the program into a file named Hello.java (case matters; really)

• Compile it

• Execute it

• The program displays the message Hello!on the screen

5- 10

Compilation

• Many high-level languages, like C, Pascal, or C++, must be compiled before they are run

• Compilation means translation into the native language of the computer

• Each computer (Wintel, Macintosh, Sun Sparc, etc.) has its own native language, so compiling the same program for each computer would create something different

• The statement “x = 3 + 2;” would be translated into a sequence of low-level operations

5- 11

Compilation

C, Pascal, or C++ program

Source code

1001110110101101 1101110001011010

Machine codefor somespecificmachine

Compilation

5- 12

Interpretation

• Other languages, like Lisp and SmallTalk, are interpreted

• They don’t get translated directly into machine language

• Instead, they are given to an interpreter, which is a program that performs each operation in the program on the computer

• The statement “x = 3 + 2;” would cause some sequence of operations to be carried out

5- 13

Interpretation

Lisp or SmallTalk program

Source code

Machine code for some specificmachine

Read by anInterpreter

Lisp or SmallTalkInterpreter for aSpecific Machine

5- 14

Some Differences

• Compilation and Interpretation differ in many ways

• When machine code is generated/activated– At compile time with compilation

– At run-time with interpretation

• The kind of machine code that is generated/activated

– Compilation can do optimizations

• The flexibility of development– Interpreters are more flexible because you don’t have to

recompile when you make changes to a program

5- 15

Where does Java fit?

• Compilation is efficient, but machine-specific

• Interpretation is flexible, but inefficient

• Java wanted to be efficient, but also flexible, and most important: PLATFORM INDEPENDENT (same program running on all computers)

• So Java is compiled and interpreted...

5- 16

Compilation and Interpretation

• A Java program can basically exist in one of two states:–Source code (that’s what you are

typing in)

–Byte code (a translation of the original code to something closer to the machine’s language --- but not any real machine’s language)

5- 17

Compilation and Execution

class Hello { public static void main...

Source code

op7 op342 op213 op431 Byte code

Compilation

Execution

Interpreter

5- 18

Java Byte Code

5- 19

The Java Virtual Machine

• The Java Virtual Machine is the interpreter that reads Java byte codes and carries out real machine operations

• To get Java running on any kind of computer, just implement the Java Virtual Machine

• Presto! Java is platform independent, and flexible (interpreted), but still reasonably efficient (because Java byte code is close to machine code)

5- 20

Another Program(this one takes input)

• We want a program that will do the following:

Please type the temperature (deg C): 2020 deg C is 68.0 deg F

5- 21

What the Robot World Lacked

• When we programmed the robot world, we had no real variables, and no real input or output

• In Java (as in virtually all programming languages), variables are essential

• Each variable has a type

5- 22

Input/Output in Java

import intro2cs.utils.*;

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

SimpleInput sinp = new SimpleInput(System.in);System.out.print(“Please type the temperature (deg C): ”);temperature = sinp.readInt();System.out.print(temperature);System.out.print(“ deg C is “);System.out.print(((9.0 * temperature)/5.0 ) + 32.0);System.out.println(“ deg F”);WaitKey.waitKey();

}}

5- 23

Perceiving Structure

import intro2cs.utils.*;

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

SimpleInput sinp = new SimpleInput(System.in);System.out.print(“Please type the temperature (deg C): ”);temperature = sinp.readInt();System.out.print(temperature);System.out.print(“ deg C is “);System.out.print(((9.0 * temperature)/5.0 ) + 32.0);System.out.println(“ deg F”);WaitKey.waitKey();

}}

5- 24

Helping Perceive Structure

import intro2cs.utils.*;

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

SimpleInput sinp = new SimpleInput(System.in);System.out.print(“Please type the temperature (deg C):”);temperature = sinp.readInt();System.out.print(temperature);System.out.print(“ deg C is ”);System.out.print(((9.0 * temperature)/5.0 ) + 32.0);System.out.println(“ deg F”);WaitKey.waitKey();

}}

5- 25

Variables

• temperature is a variable• The declaration of the variable

int temperature;saves a space for a value to be stored later, but doesn’t store anything there

• The assignment to the variabletemperature = sinp.readInt();actually puts something in the space that was created by the declaration

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

5- 26

Variable Declaration

temperature

You don’t know what’s in there;don’t use temperature yet

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

5- 27

Simple Variable Assignment

temperature = 36;

temperature

3636

Puts the integer 36 into temperature; now you can use temperature

5- 28

You Can Combine the Two Steps

class Temperature { public static void main (String[ ] args) {

int temperature = 36; // The Celsius temperature

temperature

36

5- 29

import intro2cs.utils.*;

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

SimpleInput sinp = new SimpleInput(System.in);System.out.print(“Please type the temperature (deg C):”);temperature = sinp.readInt();System.out.print(temperature);System.out.print(“ deg C is ”);System.out.print(((9.0 * temperature)/5.0 ) + 32.0);System.out.println(“ deg F”);WaitKey.waitKey();

}}

What the Program Looked Like

5- 30

Variable Assignment

SimpleInput sinp = new SimpleInput(System.in);temperature = sinp.readInt();

temperature

sinp

Create new object called sinp (from SimpleInput class); Send the object sinp a readInt() message, it returns an integer, which is then placed in temperature

readInt()

2020

5- 31

Special Variablesthat Remain Constant

• We can define a variable (a “symbolic constant”) whose value will never change, by writing

final int RETIREMENT_AGE = 70;

• This carries out assignment at the same time as declaration (as we can do with any variable)

• Now RETIREMENT_AGE cannot be legally changed in the program

5- 32

Data Types and Expressions

• Different kinds of information

• Each kind is a “type”

• Java has many data types–Each type has a name

–Each type has a set of literals

–Each type has a set of operations

• Data types in Java can besimple types or object types

5- 33

Different Simple Types in Java

• int–represents integers

– literals like 3 and 132

–operators like + , - , * , /

• double–represents real numbers

– literals like 3.0, 3.14159 and 2.997925e8

• The integer 3 and the double 3.0 are represented differently inside the computer

5- 34

More Simple Types in Java

• boolean–Represents true and false

• char–Represents individual characters

that are typed at the keyboard

5- 35

Expressions

• An Expression represents a value that can be used in Java statements– Made out of literals, variables, symbolic

constants, and operations– Every expression also has a type– 3 + 4 + temperature (has type

int)– (3.0 / 4.0)* RETIREMENT_AGE (has type double)

• Use parentheses to establish the order of operations

• Can also be messages sent to an object, like sinp.readInt( )

5- 36

Type of an Expression

No matter how simple or complicated an expression is, it always has a Java type, just like a variable.

5- 37

Where’s the Expression Go?

• An expression produces a value, which is then often used in an assignment statement

• So typically, the expression is on the right hand side of the assignment statement:– temperature = 36;– temperature = (x / 7) + y;– temperature = sinp.readInt( );

5- 38

The Order of Assignment

• In general, the computer evaluates the expression on the right side, and places that value in the variable on the left side, replacing the old value. So:

temperature = temperature + 10;

is completely legal; it adds 10 to the value of temperature

5- 39

Increment/Decrement

• Two special cases: incrementing and decrementing

cent++;is the same ascent = cent + 1;

cent--;is the same ascent = cent - 1;

5- 40

One of Your Own Kind,Stick to Your Own Kind

• Java is very strict about data types and assignments

• Java only puts into a variable a value of the appropriate type

• Expressions having an integer value can go into an integer variable, etc.

• You can’t do this:int i;double x = 3.0;i = 10.3 * x;

5- 41

Exceptions

• You can assign an integer expression to a double variable–The integer is converted to a double

without loss of information, so it is done automatically

• You can do this:int i = 3;double x;x = 10 * i;

5- 42

Converting Values from One Type to Another

• The cast operation lets you explicitly convert one type to another

• You can do this:int i;double x = 3.0;i = (int) (10.3 * x);

• The cast (int) takes the expression that follows it and converts it into an integer by removing any fractional part; i is assigned the integer value 30

5- 43

Object Types in Java

• The existence of a class in Java lets you have variables that are of new types, rather than just simple types

• (Java gives us simple types int, double, boolean, char, etc.)

• Every class C defines a set of values called objects of type C (that is, C is a class name)

• Just as you define a new variable to be of type int, you can define a new variable (object) to be of type C

5- 44

Defining Objects of Type C

C u, v, w;

declares u, v, and w to be objects of type C (or “variables of type C” or “instances of type C”)

• This is similar to writingint a, b, c;to declare a, b, and c to be variables of type int

Object type

Simple type

5- 45

One of these things,Is not like the other

• If C and D are two different classes, their objects have different types

• They can’t be assigned to one another• If we have

D t; // an object of type Dwe cannot do either of the following:u = t;t = u;

• We also can’t do something likeu = 10;

5- 46

Constructors

• Declaring a new object just tells the system the type of the object:C u;

• To create the object, we use a constructor:u = new C( arguments );

• A constructor is a special method of the class that initializes the new object; it has the same name as the class

• It is the only way to create objects

5- 47

Creating Objects and Filling them with Values

• This looks a lot like how we created objects in the robot world

• Yes, the two statements (declaration and creation) can be combined:C u = new C( arguments );

• That’s all we were doing in the robot world --- combining declaration (declaring the type of the object) with constructing a new object.

5- 48

They Look the SameBut They are Different

• The first C and the second C fulfill two different roles

• The first C tells the type of the object variable (like int tells the type of a simple variable)

• The second C is a call to the method C inside the class C that is the class’ constructor, and creates the object

C u = new C( arguments );

5- 49

Methods within an Object

• If u is an object of class C, and class C has a method called “display”, then we’ll apply a method to u by writing:u.display( arguments )

• You’ve already seen the same thing in the robot world

• We’re sending the object u a message “display” with arguments

5- 50

You can also send messages to Classes

• Unlike in the robot world, there are cases when a class is sent a message, rather than an object

• We saw this when we sent the waitKey( ) message to the WaitKey class in our sample programs

• Later we’ll talk about when we do this kind of thing

5- 51

The String Class

• This is an important predefined, special class that we are given as part of the Java system

• Can be used as follows:

String t1 = “To be ”,t2 = “or not to be.”;

System.out.print(t1 + t2);

prints out: To be or not to be.

5- 52

The String Classhas some useful Methods

• Strings have a method called "length":

String t1 = “To be ”,t2 = “or not to be.”;

• t1.length( ) then is an expression that returns the integer 6, the number of characters in the object t1

• We send the object t1 a message length( ), and get back the integer 6; we are asking the object about itself; that’s common

5- 53

Fun with Strings and +

System.out.print(“4” + “5”); //prints: 45System.out.print(“4” + 5); //prints: 45System.out.print(4 + 5); //prints: 9System.out.print(“4” + “005”); //prints: 4005System.out.print(“4” + 005); //prints: 45System.out.print(“4” * 5); //error!

• An integer concatenated to a string is automatically converted to a string

5- 54

Statements

• We’ve seen two Java programs that are sequences of statements, each statement either assigning a value to a variable, or an executable statement that prints output

System.out.print(“Please type the temperature (deg C): ”);temperature = sinp.readInt();

• There are of course other kinds of statements, conditionals (if, if/else), and iterative statements (like loop and while)

5- 55

Perceiving Structure (Again)

import intro2cs.utils.*;

class Temperature { public static void main (String[ ] args) {

int temperature; // The Celsius temperature

SimpleInput sinp = new SimpleInput(System.in);System.out.print(“Please type the temperature (deg C): ”);temperature = sinp.readInt();System.out.print(temperature);System.out.print(“ deg C is “);System.out.print(((9.0 * temperature)/5.0 ) + 32.0);System.out.println(“ deg F”);WaitKey.waitKey();

}}

5- 56

Debugging a Program

We're going to walk through a debugging of a sample program

5- 57

Write and Debug a Program

• Compute the total cost of a coffee order

• ( (Price per pound) x Weight ) + Shipping

• Shipping =((Shipping Rate per pound) x Weight ) + Fixed handling fee

• Price per pound and Weight vary

• Shipping Rate per pound and handling fee are fixed

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE,int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: );pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ totalPrice);

}}

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE,int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: );pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ totalPrice);

}}

ERROR

ERROR

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE;int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ totalPrice);

}}

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE;int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ totalPrice);

}}

ERROR

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE;int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ + totalPrice);

}}

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final int RATE_PER_POUND = 1.25,

FIXED_FEE;int priceperPound, weight,

shippingCost, coffeeCost;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shipingCost = RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = priceperPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shipingCost); System.out.println(“Total cost is “ + totalPrice);

}}

ERROR

ERROR

ERROR

ERROR

ERROR

ERROR ERRORERROR ERRORERROR

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final double RATE_PER_POUND = 1.25,

FIXED_FEE = 1.95;int pricePerPound, weight,

shippingCost, coffeeCost, totalPrice;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shippingCost=RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = pricePerPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shippingCost); System.out.println(“Total cost is “ + totalPrice);

}}

import intro2cs.utils.*;

class CoffeeOrder {public static void main (String[ ] args) {final double RATE_PER_POUND = 1.25,

FIXED_FEE = 1.95;int pricePerPound, weight,

shippingCost, coffeeCost, totalPrice;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shippingCost=RATE_PER_POUND+weight

+FIXED_FEE;totalPrice = pricePerPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shippingCost); System.out.println(“Total cost is “ + totalPrice);

}}

ERROR

ERROR

import intro2cs.utils.*;class CoffeeOrder {

public static void main (String[ ] args) {final double RATE_PER_POUND = 1.25,

FIXED_FEE = 1.95;double pricePerPound, weight,

shippingCost, coffeeCost, totalPrice;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readInt();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shippingCost=RATE_PER_POUND+weight

+FIXED_FEE;coffeeCost = pricePerPound * weight;totalPrice = pricePerPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shippingCost); System.out.println(“Total cost is “ + totalPrice);

}}

5- 67

We run the program

Enter price per pound: 8.95

java.lang.NumberFormatException: 8.95

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.<init>(Integer.java)

at sinp.readInt…

at CoffeeOrder.main(CoffeeOrder.java…We’ve got a problem. We tried to read a double by sending sinp the readInt() message.

import intro2cs.utils.*;class CoffeeOrder {

public static void main (String[ ] args) {final double RATE_PER_POUND = 1.25,

FIXED_FEE = 1.95;double pricePerPound, weight,

shippingCost, coffeeCost, totalPrice;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readDouble();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shippingCost=RATE_PER_POUND+weight

+FIXED_FEE;coffeeCost = pricePerPound * weight;totalPrice = pricePerPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shippingCost); System.out.println(“Total cost is “ + totalPrice);

}}

5- 69

We Run the Program Again

Enter price per pound: 8.95

Enter number of pounds: 3

Coffee total is 26.849999999999998

Shipping cost is 6.2

Total cost is 33.05

Great!Only one problem. The total cost is $0.50 too high.

import intro2cs.utils.*;class CoffeeOrder {

public static void main (String[ ] args) {final double RATE_PER_POUND = 1.25,

FIXED_FEE = 1.95;double pricePerPound, weight,

shippingCost, coffeeCost, totalPrice;SimpleInput sinp = new SimpleInput(System.in);System.out.println(“Enter price per pound: “);pricePerPound = sinp.readDouble();System.out.print(“Enter number of pounds: “);weight = sinp.readInt();shippingCost=RATE_PER_POUND*weight

+FIXED_FEE;coffeeCost = pricePerPound * weight;totalPrice = pricePerPound * weight +

shippingCost;System.out.println(“Coffee total is “ + coffeeCost);

System.out.println(“Shipping cost is “ + shippingCost); System.out.println(“Total cost is “ + totalPrice);

}}

5- 71

We Run the Program One Last Time

Enter price per pound: 8.95

Enter number of pounds: 3

Coffee total is 26.849999999999998

Shipping cost is 5.7

Total cost is 32.55

That’s good enough for now.

5- 72

Transparent vs. OpaqueFunctionality

• There used to be a visible connection between physical things and what they did–a butter churn

–a loom

–a watch

• The coming of electrical power changed all that

5- 73

The Computer

• The computer is the supreme example of opaque function

• Looking into the box does not help us to understand what is going on

• The best way to understand computers is to understand the logical layers out of which they are built

5- 74

The Layers of Computing

Underlying hardware

CPUMemory

Hard disk Keyboard

Machine Layer Software

AssemblyLanguages

MachineLanguages

Micro-Programs

System Layer Software

UNIX Windows AppleSystem

Application Layer Software

CompilerGames UtilitiesBrowser

Screen

5- 75

The "Classic" Computer Architecture

CPUMemory

Hard disk

Keyboard - Input

Screen - Output

5- 76

The "Classic" Typewriter Architecture

Keyboard - Input

Paper - Output

Direct Mechanical Action

5- 77

What Makes the Computer Different?

• Memory–Short-term

–Long-term

• Programmable–Multi-purpose

• Communication-enabled

5- 78

ASCC Mark I, "Automatic Sequence-Controlled Calculator"

• Built at Harvard by Howard Aiken, completed January 1943

• Approaching architecture of a modern computer

• Used mechanical electromagnetic relays for storing numbers and doing calculations

• The machine was 51 feet long, weighed 5 tons, and incorporated 750,000 parts. It included 72 accumulators

• 1947: Aiken predicts that the United States will need a total of six electronic digital computers

5- 79

ENIAC, the first electronic computer

• Eckert and Mauchly, University of Pennsylvania, November 1945

• ENIAC's architecture resembles that of the Harvard Mark I, but its components are entirely electronic, incorporating 17,468 vacuum tubes

• The machine weighs 30 tons, covers about 1000 square feet of floor, and consumes 130 kilowatts of electricity; it uses vacuum tubes

5- 80

ENIAC

• The machine incorporates 20 accumulators (the original plan was for 4). Each accumulator stores a 10-digit number, using 10 bits to represent each digit

• A separate unit can perform multiplication (in about 3 milliseconds), while another does division and square roots

5- 81

ENIAC

• A card reader is available to input data values, and there is a card punch for output

• The program is set up on a plugboard --- this is considered reasonable since the same or similar program would generally be used for weeks at a time

• The ENIAC's clock speed is 100 kHz

5- 82

ENIAC

5- 83

More ENIAC

5- 84

EDVAC, "Electronic Discrete Variable Automatic Computer"

Oppenheimer and von Neumann

EDVAC was the first stored program computer

5- 85

EDSAC, Electronic Delay Storage Automatic Computer

• Based on EDVAC report, first full-scale operational stored program computer, May 1949

• 16 tanks of mercury give a total of 256 35-bit words (or 512 17-bit words).

• The clock speed of the EDSAC is 500 kHz; most instructions take about 1500 ms to execute.

• Its I/O is by paper tape, and a set of constant registers is provided for booting

5- 86

Lots More History

• Brattain, Bardeen, and Shockley invent the transistor, which eventually replaces vacuum tubes, Dec. 1947

• Noyce invents the integrated circuit, which puts multiple connected transistors onto a chip, January 1959

• Hoff invents programmable computer on a chip, Intel 4004, 1970

5- 87

Inventor of Intel 4004 Computer-on-a-Chip, Marcian Hoff

5- 88

Intel 4004

• 2250 transistors on the chip

• Processed 4 bits of data at a time

• 600,000 operations a second

• Followed by (in Intel's line of chips)–the Intel 8008

–the 8080, the first chip that made a personal computer possible

–the 8088, and the 8086, and the 80286, 80386, 80486, Pentium, Pentium II, III, IV ...

5- 89

Underlying Hardware—CPU

• Central Processing Unit (CPU) — speed measured using many different benchmarks

– Integer performance, SPECint*_base2000

– Floating point performance, SPECfp*_base2000

– Entertainment benchmarks, internet benchmarks, …

• Pentium, Pentium II, III, IV, PowerPC, Sun Sparc

• See, for example, http://www.intel.com/performance/resources/desktop/index.htm for information about Pentium IV performance

5- 90

Underlying Hardware—RAM

• Random Access Memory (RAM) — size measured in megabytes, or millions of bytes

• Holds programs while they are being run

• A byte is 8 bits (binary digits), representing (roughly) one character

• 2002 desktop computer might have from 256 to 1024 megabytes of RAM; more powerful computers can have much more RAM

5- 91

Underlying Hardware—Hard Disk

• Hard disk holds programs “permanently” (even while power is off)

• 2002 desktop computer might have between 40 to 120 gigabytes of hard disk storage

5- 92

Underlying Hardware—Removable Media

• Floppy disks, writeable CDs (CD-R) and DVDs (DVD-R, DVD+R, DVD-RW, DVD+RW) and tape allow data to be backed up or transfered from one computer to another

• 2002 desktop computer works with removable diskettes that hold 1.44 megabytes of data

• Growing popularity of Zip drives, 1 gigabyte Jaz drives (Iomega), flash memory up to 128MB or 256MB are common (MemoryStick, CompactFlash, Secure Digital, MultiMediaCard), …

5- 93

Underlying Hardware—Input/Output Devices

• Input devices, like a keyboard and a mouse (and a microphone, and a joystick, and a trackball, and a pen, and a scanner, and…)

• Output devices, like a screen and a printer (and a speaker, and a plotter, and a laser printer, and…)

5- 94

A Brief Diversion

• Doug Engelbart, the inventor of the mouse (and windowed interfaces, and more):http://www2.bootstrap.org/history.htm

•The first mouse:

5- 95

Input Devices that didn't take off

• The chord key set:

5- 96

Names you probably don't know

• Short History of the Internethttp://www.umr.edu/~dunaw/net-history.html

• Paul Baran, of the RAND Corporation, commissioned by the U.S. Air Force to do a study on how it could maintain its command and control over its missiles and bombers, after a nuclear attack. Suggested a packet-switched network.http://www.umr.edu/~dunaw/net-history.html

• Leonard Kleinrock, UCLA researcher who connects computer to switch to another computer, 2 September 1969

• Ray Tomlinson, BBN engineer who invents email in 1972, "a quick hack", chooses the @ http://www.pretext.com/mar98/features/story2.htm

5- 97

Typical Desktop Hardware Configuration, 2002

• Pentium IV processor, running at 2.0 GHz

• 512 MB of RAM

• Flat Screen LCD 15” color monitor

• Built-in ethernet and modem

• CD-RW, DVD drives

• 60 gigabyte hard disk

• Approximate cost in U.S.: $1300

5- 98

Typical Desktop Hardware Configuration, 1984

• IBM AT 80286 processor, running at 6 MHz

• 256 KB of RAM, expandable to 3MB using five expansion cards

• Monochrome monitor (color graphics card)

• 20 MB hard disk

• Approximate cost in U.S.: $6600

5- 99

Hardware Comparison, 1984 –2002

• IBM AT 80286 processor, running at 6 MHz

• 256 KB of RAM, expandable to 3MB using five expansion cards

• Monochrome monitor (color graphics card)

• 20 MB hard disk

• Approximate cost in U.S.: $6600

• Pentium IV processor, running at 2.0 GHz

• 512 MB of RAM

• Flat Screen LCD 15” color monitor

• Built-in ethernet and modem

• CD-RW, DVD drives

• 60 gigabyte hard disk

• Approx. cost in U.S.: $1300

Price/performance ratio improvement, factor of over 4000

5- 100

The Meaning ofExponential Growth

• Moore’s Law: the computing power available at a fixed price will double every 18 months

• This has been true for over the last 35 years, and will continue to be true in coming years

• Most people cannot grasp the real meaning of exponential growth, confusing it with linear growth at a sharp angle over the short term

5- 101

Exponential Growth, DoublingEvery 18 months, 1974 = 1, 2001 = 262144

1974

1977

1980

1983

1986

1989

1992

1995

1998

0

10000

20000

30000

40000

50000

60000

7000019

74

1977

1980

1983

1986

1989

1992

1995

1998

Year

Exponential Growth

5- 102

If Cars Improved the Way Computers Do

• A Rolls Royce would cost $1 and travel 5000 kilometers on a liter of gasoline

• But it would be too small to get into

• Really:–$20,000 in 1967

–$5,000 in 1970

–$1,250 in 1973

–$0.0025 in 2002

5- 103

Why We Don’t Notice It

0

0.5

1

1.5

2

2.5

3

3.5

4

1995 1998

We are focused on short periods, where the difference between linear and exponential is obscured

5- 104

Exponential Growth, DoublingEvery 18 months, 1974 = 1, 2001 = 262144

1974

1977

1980

1983

1986

1989

1992

1995

1998

0

10000

20000

30000

40000

50000

60000

7000019

74

1977

1980

1983

1986

1989

1992

1995

1998

Year

Exponential Growth

5- 105

But the Future is Just as Amazing

1995 1998 2001 2004 2007 20100

200

400

600

800

1000

1200

1995 1998 2001 2004 2007 2010

1995 = 1 1998 = 4 2010 = 1024

5- 106

Two Curves

powerful

weakthen nowThe Hardware Curve

powerful

weakthen nowThe Software Curve

5- 107

Two Major Developments

• The two biggest developments in the computer industry in the last 5 years:–The transition of the computer from a

computing device to a communications and computing device

–The ever-shrinking, ever-cheaper computing power has led to “computers everywhere”, digital appliances, embedded and standalone

5- 108

Where Does it End?

• By 2012, the layer thickness of integrated circuits will measure just less than one nanometer, equivalent to five silicon atoms

• Two papers by Harvard and Cornell researchers in the June 13, 2002 issue of the journal Nature described a breakthrough in miniaturization: researchers have created transistors whose switching components are literally single atoms (but they lack “gain” and work just at very low temperatures).

• What happens when the physical limits are reached? Does the hardware curve straighten out? Do new technologies (optical computing, DNA computing, quantum computing) provide new solutions?

5- 109

The Layers of Computing

Underlying hardware

Machine Layer Software

AssemblyLanguages

MachineLanguages

Micro-Programs

System Layer Software

UNIX Windows AppleSystem

Application Layer Software

CompilerGames UtilitiesBrowser

CPUMemory

Hard disk KeyboardScreen

5- 110

Machine Layer Software

• Machine language instructions—built into the computer, a language of 1’s and 0’s

• Assembly language instructions— use brief English-like mnemonics that carry out slightly more complicated instructions. Assembly language is directly and easily translatable into machine language, using an assembler

5- 111

Machine Layer Software (II)

• Nowadays, there is less of a distinction between machine language and assembly language

• Computers are built that have “built-in” translators

• Microcode—a machine language program built into the CPU—is run when an assembly language command is given. So the assembly language is the machine language!

5- 112

The Layers of Computing

Underlying hardware

CPU MemoryHard diskKeyboard

Machine Layer Software

AssemblyLanguages

MachineLanguages

Micro-Programs

System Layer Software

UNIX Windows AppleSystem

Application Layer Software

CompilerGames UtilitiesBrowser

5- 113

System Layer Software

• The machine layer software is very low level—we need a second layer of software to take care of details

• The operating system is a constantly running program that:–keeps track of computer resources

–seems to be controlling the computer

5- 114

System Layer Software—the Operating System

• The operating system:–“Listens” to keyboard and mouse for input–“Talks” to screen and printer– Interprets commands as they are input–Makes programs available to the user and lets

him install new ones–Stores information in files, and manages them–Controls access to the computer–Splits CPU’s attention between several jobs–Communicating with other computers

5- 115

Lots of Operating Systems

• But there are three most popular ones today:

UNIX (and its Linux variant)

Windows (‘98, NT, XP)

Apple Macintosh OS X (built on top of Unix)

• They differ in big and small ways— timesharing, graphical interface, power…

• Also, embedded systems and handhelds

5- 116

What’s the Connection?

• Question: What’s the relationship between an operating system and hardware?

• Answer: Not much, even though each operating system has a “most common” implementation on particular hardware:–Windows on 80x86 chips, but Windows NT on

others

–Macintosh on 680x0 chips and PowerPC chips

–UNIX on SPARC chips, but also on 80x86 chips and 680x0 chips…and others

5- 117

What’s a GUI?

• A Graphical User Interface is commonplace in computing these days

• The Apple Macintosh has it, machines with Microsoft Windows have it, the Xerox Star had it, Unix machines have it (in many flavors)

• A graphical interface contrasts with a character-based interface, such as MS-DOS or plain Unix gives you

5- 118

Graphical User Interface

• The GUI runs on top of the Operating System, and makes the Operating System easier to use

• Usually includes: bitmapped displays, menus, windows, use of a pointing device (like a mouse), buttons, etc.

5- 119

Windows XP

5- 120

The Layers of Computing

Underlying hardware

CPU MemoryHard diskKeyboard

Machine Layer Software

AssemblyLanguages

MachineLanguages

Micro-Programs

System Layer Software

UNIX Windows AppleSystem

Application Layer Software

CompilerGames UtilitiesBrowser

5- 121

Application Layer Software

• These are the programs that do specific jobs—word processors, drawing programs, spreadsheet,tax programs, etc.

• Applications are written inany convenient language—Pascal, C, Lisp, Modula-2,Ada, Fortran…

• The underlying platform is usually a specific operating system

Applications

Programming Language

Operating System

5- 122

Programming Languages

• Low level (first or second generation languages) are closely tied to the computer’s instruction set. They are good when:–the program must control some

hardware that can only be controlled in this low-level language

–the program must run extremely quickly

5- 123

Higher-level Languages

• Higher level languages (third generation languages) like Pascal, C, C++, Java, etc. are more disconnected from the hardware on which they run

• They can be used to solve any kind of problem

• They can run on any kind of computer

5- 124

Java

• Designed by Sun team led by James Gosling

• Originally called Oak, it was intended for consumer devices like TV-top boxes

• Being cross platform, and more stable than C++, were essential goals

• When the TV-top market didn’t materialize, figured they’d try the internet

5- 125

Fourth Generation Languages

• Application languages (fourth generation) are more high level languages, but also more specialized

• Examples are PostScript, database languages, etc.

• They are very good at the tasks they do, and clumsy for general-purpose tasks

5- 126

Some PostScript Code

%!PS-Adobe-3.0 EPSF-2.0%%Creator: Windows PSCRIPT%%Title: PowerPoint - UNIT5.PPT%%BoundingBox: 13 10 577 832%%DocumentNeededResources: (atend)%%DocumentSuppliedResources: (atend)%%Pages: 0%%BeginResource: procset Win35Dict 3 1/Win35Dict 290 dict def Win35Dict begin/bd{bind def}bind def/in{72mul}bd/ed{exch def}bd/ld{load def}bd/tr/translate ld/gs/gsave ld/gr/grestore ld/M/moveto ld/L/lineto ld/rmt/rmoveto ld/rlt/rlineto ld/rct/rcurveto ld/st/stroke ld/n/newpath ld/sm/setmatrix ld/cm/currentmatrixld/cp/closepath ld/ARC/arcn ld/TR{65536 div}bd/lj/setlinejoin ld/lc/setlinecap ld/ml/setmiterlimit ld/sl/setlinewidth ld/scignore falsedef/sc{scignore{pop pop pop}{0 index 2 index eq 2 index 4 index eqand{pop pop 255 div setgray}{3{255 div 3 1 roll}repeat setrgbcolor}ifelse}ifelse}bd/FC{bR bG bB sc}bd/fC{/bB ed/bG ed/bR ed}bd/HC{hR hG hB sc}bd/hC{

5- 127

The Field of Computer Science

• We’ve been looking at computers (software and hardware), but that’s not an accurate description of the field of Computer Science:

Hardware Software Theory

5- 128

Areas in Computer Science

• Algorithms and Data Structures

• Programming Languages

• Computer Architecture

• Operating Systems

• Software Engineering

• Symbolic and numerical computation and modeling, graphics

• Databases

• Artificial Intelligence

• Robotics

• Computer Vision

5- 129

How they fit together

Hardware Software Theory

Architecture Languages

SoftwareEngineering

Algorithms

OperatingSystems

RoboticsDatabases

Artificial Intelligence

Symbolic/NumericalComputation