CS 101 Chapter 1 Introduction Aaron Bloomfield Spring

  • Slides: 75
Download presentation
CS 101 Chapter 1: Introduction Aaron Bloomfield Spring 2007

CS 101 Chapter 1: Introduction Aaron Bloomfield Spring 2007

Let’s begin n Goal n n Teach you how to program effectively Skills and

Let’s begin n Goal n n Teach you how to program effectively Skills and information to be acquired n n What the heck all this means Problem solving Object-oriented design Java 2

So, what is a computer, anyway? 3

So, what is a computer, anyway? 3

What is a computer? n n Not a rhetorical question! “A device that computes…

What is a computer? n n Not a rhetorical question! “A device that computes… especially a programmable electronic machine that performs high-speed mathematical or logical operations or that assembles, stores, correlates, or otherwise processes information” n From American Heritage® Dictionary of the English Language, 4 th Edition 4

So what is a computation? n The act or process of computing n n

So what is a computation? n The act or process of computing n n Definition of computing: n n n Duh! To determine by the use of a computer To determine by mathematics, especially by numerical methods: computed the tax due My revised definition for computing: n The act of taking a problem with specific inputs and determining a specific answer (output) 5

Axiom n n By definition, a (properly functioning) computer will always produce the same

Axiom n n By definition, a (properly functioning) computer will always produce the same output given the same input So how do we compute random numbers? 6

The first computers n Scales – computed relative weight of two items n n

The first computers n Scales – computed relative weight of two items n n Computed if the first item’s weight was less than, equal to, or greater than the second item’s weight Abacus – performed mathematical computations n n Primarily thought of as Chinese, but also Japanese, Mayan, Russian, and Roman versions Can do square roots and cube roots 7

Stonehenge 8

Stonehenge 8

Computer Size ENIAC then… ENIAC today… 9

Computer Size ENIAC then… ENIAC today… 9

Algorithms 10

Algorithms 10

So what do we do with a computer… n … now that we have

So what do we do with a computer… n … now that we have one? n We have to tell a computer what to do! n n Computers have no intelligence of their own We tell a computer what to do by writing a computer program, or “algorithm” n In this course, we’ll use Java 11

Algorithms n n What is an algorithm? “A step-by-step problem-solving procedure, especially an established,

Algorithms n n What is an algorithm? “A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps” n n From American Heritage® Dictionary of the English Language, 4 th Edition We’ve seen lots of algorithms before… 12

Example algorithm: map directions 13

Example algorithm: map directions 13

Example algorithm: car radio removal 14

Example algorithm: car radio removal 14

Example algorithm: Recipes 15

Example algorithm: Recipes 15

An optical illusion 16

An optical illusion 16

Algorithm correctness 17

Algorithm correctness 17

Incorrect algorithms n Not all algorithms are “good” n So then what makes an

Incorrect algorithms n Not all algorithms are “good” n So then what makes an algorithm “bad”? n n Can Can be wrong be inefficient never stop have other problems as well… 18

Incorrect algorithms: just plain wrong From http: //en. for-ua. com/blog/2005/12/09/102028. html 19

Incorrect algorithms: just plain wrong From http: //en. for-ua. com/blog/2005/12/09/102028. html 19

Inefficient algorithms: Map. Quest directions n Consider directions to get around grounds 20

Inefficient algorithms: Map. Quest directions n Consider directions to get around grounds 20

Note that this is not an incorrect algorithm! Just a very inefficient one 21

Note that this is not an incorrect algorithm! Just a very inefficient one 21

Incorrect algorithms: Shampoo directions 22

Incorrect algorithms: Shampoo directions 22

23

23

Incorrect algorithms: Shampoo directions n Lather, rinse, repeat n This algorithm repeats forever! n

Incorrect algorithms: Shampoo directions n Lather, rinse, repeat n This algorithm repeats forever! n n n It never halts Note that humans know to not to spend forever performing the algorithm But computers do not! n n Remember, they have zero intelligence Hence the overplayed (and not very funny) computer joke: n n How did the computer scientist die in the shower? S/he read the directions: lather, rinse, repeat 24

Incorrect algorithms: Inexact recipes 25

Incorrect algorithms: Inexact recipes 25

Incorrect algorithms: summation n Consider this algorithm: n n n Given an integer n

Incorrect algorithms: summation n Consider this algorithm: n n n Given an integer n Keep track of an ongoing sum (starts at 0) Repeat n n Add n to the ongoing sum Subtract 1 from n Until n is zero What’s wrong with this algorithm? n n Will it ever stop? Will it always stop? n Are you sure? 26

Our goal n Is to write correct and efficient algorithms for a computer to

Our goal n Is to write correct and efficient algorithms for a computer to follow n n n Remember that computers are dumb! We aren’t going to worry about the efficient part in this course But what does “correct” mean? 27

“Correct” algorithms n Consider an algorithm to display the color blue n Is this

“Correct” algorithms n Consider an algorithm to display the color blue n Is this blue? Definitely n Is this blue? Also, yes: two correct results! n What about this? Maybe (could be green) n And this one? Definitely not 28

Note that this is not an incorrect algorithm! Just a very inefficient one 29

Note that this is not an incorrect algorithm! Just a very inefficient one 29

So what does all this mean? n Humans specify algorithms without a lot of

So what does all this mean? n Humans specify algorithms without a lot of precision n n Display the color “blue” Get me from “here” to “there” When there isn’t much precision, there are often multiple answers Computers need more precision n Display the color 0 x 0000 ff (royal blue): n n n There is only one possible outcome Find the shortest route from “here” to “there” We need to be very specific when we specify things to a computer n Computers are dumb! 30

Carved watermelons 31

Carved watermelons 31

Programming a computer 32

Programming a computer 32

How do we tell all this to a computer? n “Computer: Tea, Earl Gray,

How do we tell all this to a computer? n “Computer: Tea, Earl Gray, hot” n n Jean-Luc Picard from Star Trek Unfortunately, that doesn’t work so well today… n Computers don’t understand English 33

First Programming Languages n Ada Lovelace (1833) n n ENIAC (1945) n n n

First Programming Languages n Ada Lovelace (1833) n n ENIAC (1945) n n n Programmed by plugging wires Binary n n Programmed of Babbage’s analytical engine 1001010100101010 Assembly FORTRAN (1954) 34

Programming Languages n Common programming languages: n n n BASIC COBOL Pascal C (1972)

Programming Languages n Common programming languages: n n n BASIC COBOL Pascal C (1972) by Dennis Ritchie C++ (1985) by Bjarne Stroustrup Java (1991) by James Gosling and others at Sun Microsystems 35

Background 36

Background 36

Computing units of measure n A bit is either a 1 or a 0

Computing units of measure n A bit is either a 1 or a 0 n n n On or off, true or false, etc. A byte is 8 bits: n 01001010 n As there are 8 bits per byte, each byte can hold 28=256 values n 01001010 = 74 All computing measurements are in terms of bytes 37

Computing units of measure n Kilo (K) = 1, 000 (thousand) n Mega (M)

Computing units of measure n Kilo (K) = 1, 000 (thousand) n Mega (M) = 1, 000 (million) n Giga (G) = 1, 000, 000 (billion) n Tera (T) = 1, 000, 000 (trillion) n Kilo = 210 = 1, 024 = Kibi (Ki) n Mega = (1024)2 = 1, 048, 576 = Mebi (Mi) n Giga = (1024)3 = 1, 073, 741, 824 = Gibi (Gi) n Tera = (1024)4 = 1, 099, 511, 627, 776 = Tebi (Ti) 38

Computing units of measure n An unformatted text document (such as a Java program)

Computing units of measure n An unformatted text document (such as a Java program) n n A formatted document (such as a Word file) n n n About 1 Mb each (1, 000 bytes) An MP 3 music file n n About 5 k per page with formatting A digital camera picture n n 3 pages per kilobyte (1, 000 bytes) 5 Mb for a 5 minute song A music file on a CD n 50 Mb for a 5 minute song n 10 times the size of an MP 3! A movie clip n About 10 Mb per minute of (TV-sized) video 39

A marketing trick n This hard drive has 250, 059, 350, 016 bytes n

A marketing trick n This hard drive has 250, 059, 350, 016 bytes n n n = 250. 06 Gigabytes = 232. 89 Gibibytes Guess which one they use when they are advertising the drive? 40

Today’s demotivators 41

Today’s demotivators 41

Our first Java program 43

Our first Java program 43

Programming Task n Display “Hello World!” 44

Programming Task n Display “Hello World!” 44

Hello. World. java // Purpose: say hello! public class Hello. World { public static

Hello. World. java // Purpose: say hello! public class Hello. World { public static void main(String[] args) { System. out. println (“Hello, world!"); } } One application statement program makes up is of required the action to of have method a A An method is a named piece code that performs public main() staticorvoid method named main(). some action implements a behavior Method main() is part of class Hello. World 45

Sample output 46

Sample output 46

Program parts 47

Program parts 47

Common Language Elements n n There are some concepts that are common to virtually

Common Language Elements n n There are some concepts that are common to virtually all programming languages. Common concepts: n n n Key words Operators Punctuation Programmer-defined identifiers Strict syntactic rules 48

Java Documentation n Familiarize yourself with the Java documentation n n It will save

Java Documentation n Familiarize yourself with the Java documentation n n It will save you lots of time! A link to it is on the website n We will go over it in a future lab as well 49

Key Words n Key words in the sample program are: n n n public

Key Words n Key words in the sample program are: n n n public class static void String n n n String is not really a key word but is the name of a predefined class in Java We’ll go over the difference between these later Key words n n lower case (Java is a case sensitive language). cannot be used as a programmer-defined identifier. 50

Programming Languages n n n Some Java key words have no meaning but are

Programming Languages n n n Some Java key words have no meaning but are reserved to prevent their use. (ex. goto, const, include) Semi-colons are used to end Java statements; however, not all lines of a Java program end a statement. Part of learning Java is to learn where to properly use the punctuation. 51

Lines vs Statements n There is a difference between lines and statements when discussing

Lines vs Statements n There is a difference between lines and statements when discussing source code. System. out. println( message); n n This is one Java statement written using two lines. Do you see the difference? A statement is a complete Java instruction that causes the computer to perform an action. 52

Good commenting n Necessary so others can re-use your code n n And so

Good commenting n Necessary so others can re-use your code n n And so the graders can understand it! A well commented program: // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } 53

Bad commenting // // Thomas J. Watson (February 17, 1874 - June 19, 1956)

Bad commenting // // Thomas J. Watson (February 17, 1874 - June 19, 1956) is considered to be the founder of IBM. He was one of the richest men of his time and called the world's greatest salesman when he died. // // // Watson was born in Campbell, New York. His formal education consisted of only a course in the Elmira School of Commerce. His first job was at age 18 as a bookkeeper in Clarence Risley's Market in Painted Post, New York. Later he sold sewing machines and musical instruments before joining the National Cash Register Company as a salesman in Buffalo. He eventually worked his way up to general sales manager. Bent on inspiring the dispirited NCR sales force, Watson introduced the motto, "THINK, " which later became a widely known symbol of IBM. // // // Although he is well known for his alleged 1943 statement: "I think there is a world market for maybe five computers" there is no evidence he ever made it. The author Kevin Maney tried to find the origin of the quote. He has been unable to locate any speeches or documents of Watson's that contain this, nor is it present in any contemporary articles about IBM. The earliest known citation is from 1986 on Usenet in the signature of a poster from Convex Computer Corporation as "I think there is a world market for about five computers" --Remark attributed to Thomas J. Watson (Chairman of the Board of International Business Machines), 1943 // // // // While at NCR, he was convicted for illegal anticompetitive sales practices (e. g. he used to have people sell deliberately faulty cash registers, either second-hand NCR or from competitors; soon after the second-hand NCR or competitors cash register failed, an NCR salesperson would arrive to sell them a brand new NCR cash register). He was sentenced, along with John H. Patterson (the owner of NCR), to one year of imprisonment. Their conviction was unpopular with the public, due to the efforts of Patterson and Watson to help those affected by the 1913 Dayton, Ohio floods, but efforts to have them pardoned by President Woodrow Wilson were unsuccessful. However, the Court of Appeals overturned the conviction on appeal in 1915, on the grounds that important defense evidence should have been admitted. public class Display. Forecast { // method main(): application entry point public static void main(String[] args) { System. out. print("I think there is a world market for"); System. out. println(" maybe five computers. "); System. out. println(" Thomas Watson, IBM, 1943. "); } } 54

More bad commenting n From the context-switching code of Unix V 6 (file: slp.

More bad commenting n From the context-switching code of Unix V 6 (file: slp. c) /* * If the new process paused because it was * swapped out, set the stack level to the last call * to savu(u_ssav). This means that the return * which is executed immediately after the call to aretu * actually returns from the last routine which did * the savu. * * You are not expected to understand this. */ if(rp->p_flag&SSWAP) { rp->p_flag =& ~SSWAP; aretu(u. u_ssav); } n Source: http: //www. tuhs. org/Archive/PDP-11/Trees/V 6/usr/sys/ken/slp. c 55

Human stupidity 56

Human stupidity 56

Programming overview 58

Programming overview 58

The Programming Process 1. Clearly define what the program is to do. 2. Visualize

The Programming Process 1. Clearly define what the program is to do. 2. Visualize the program running on the computer. 3. Use design tools to create a model of the program. 4. Check the model for logical errors. 59

The Programming Process 5. Enter the code and compile it. 6. Correct any errors

The Programming Process 5. Enter the code and compile it. 6. Correct any errors found during compilation. Repeat Steps 5 and 6 as many times as necessary. 7. Run the program with test data for input. 8. Correct any runtime errors found while running the program. Repeat Steps 5 through 8 as many times as necessary. 9. Validate the results of the program. 60

Software Engineering n n Encompasses the whole process of crafting computer software. Software engineers

Software Engineering n n Encompasses the whole process of crafting computer software. Software engineers perform several tasks development of complex software projects. n n n n in the designing, writing, testing, debugging, documenting, modifying, and maintaining. 61

Software Engineering n Software engineers develop: n n n program specifications, diagrams of screen

Software Engineering n Software engineers develop: n n n program specifications, diagrams of screen output, diagrams representing the program components and the flow of data, pseudocode, examples of expected input and desired output. 62

Other buzzwords from the chapter n I’m not expecting you to fully understand these

Other buzzwords from the chapter n I’m not expecting you to fully understand these after reading chapter 1 n n Procedural programming Object oriented programming n n n Data hiding Code reusability Classes vs. objects Inheritance We will see all of these before the semester ends 63

Computer Systems 64

Computer Systems 64

Computer Systems n Hardware n n n The central processing unit (CPU) Main memory

Computer Systems n Hardware n n n The central processing unit (CPU) Main memory Secondary storage devices Input and Output devices Software n n Operating systems Application software 65

Computer Systems: Hardware Input Devices ALU Control Unit RAM Output Devices Input / Output

Computer Systems: Hardware Input Devices ALU Control Unit RAM Output Devices Input / Output Devices 66

Computer Systems: Hardware n n Computer hardware components are the physical pieces of the

Computer Systems: Hardware n n Computer hardware components are the physical pieces of the computer. The major hardware components of a computer are: n n The central processing unit (CPU) Main memory Secondary storage devices Input and Output devices 67

Central Processing Unit CPU Instruction (input) Arithmetic Logic Unit Result (output) Control Unit 68

Central Processing Unit CPU Instruction (input) Arithmetic Logic Unit Result (output) Control Unit 68

Central Processing Unit n The CPU performs the fetch, decode, execute cycle in order

Central Processing Unit n The CPU performs the fetch, decode, execute cycle in order to process program information. Fetch Execute The CPU’s control unit fetches, from main memory, the next instruction in the sequence of program instructions. The instruction is encoded in the form of a number. The control unit decodes the instruction and generates an electronic signal. Decode The signal is routed to the appropriate component of the computer (such as the ALU, a disk drive, or some other device). The signal causes the component to perform an operation. 69

Main Memory (aka RAM) n n Commonly known as random-access memory (RAM) RAM n

Main Memory (aka RAM) n n Commonly known as random-access memory (RAM) RAM n contains: n n n currently running programs data used by those programs is volatile n n when the computer is turned off, the contents of RAM are erased. short-term memory 70

Secondary Storage n n Secondary storage devices are capable of storing information for longer

Secondary Storage n n Secondary storage devices are capable of storing information for longer periods of time n non-volatile n long-term memory Examples • Hard drive • CD RW drive • DVD RAM drive • Compact Flash card 71

Input Devices n n n Input is any data the computer collects from the

Input Devices n n n Input is any data the computer collects from the outside world. That data comes from devices known as input devices. Common input devices: n n Keyboard Mouse Scanner Digital camera 72

Output Devices n n Output is any data the computer sends to the outside

Output Devices n n Output is any data the computer sends to the outside world. That data is displayed on devices known as output devices n Common output devices: n n n Monitors Printers Some devices such as disk drives perform input and output and are called I/O devices (input/output). 73

Computer Systems: Software n n Software refers to the programs that run on a

Computer Systems: Software n n Software refers to the programs that run on a computer. Two classifications of software: n n Operating Systems Application Software 74

Operating Systems n An operating system has two functions: n n n Control the

Operating Systems n An operating system has two functions: n n n Control the system resources. Provide the user with a means of interaction with the computer. Operating systems can be either n single tasking (run one program at a time) n n DOS multi-tasking (run many programs at once) n n n Windows Unix Apple 75

Operating Systems n Operating systems can also be categorized as n single user n

Operating Systems n Operating systems can also be categorized as n single user n n only one user to operate the computer at a time Examples: n n n DOS Windows 95/98/ME multi-user n n allow several users to run programs and operate the computer at once. Examples n n Unix BSD Windows NT/2000/XP OS/X 76

Application Software n Programs that make the computer useful to the user n n

Application Software n Programs that make the computer useful to the user n n n Spreadsheets Word processors Accounting software Tax software Games 77