CS 101 Chapter 1 Background Aaron Bloomfield Fall
CS 101 Chapter 1: Background Aaron Bloomfield Fall 2006
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
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 3
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) 4
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? 5
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 6
Stonehenge 7
Computer Size ENIAC then… ENIAC today… 8
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 10
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… 11
Example algorithm: map directions 12
Example algorithm: car radio removal 13
Example algorithm: Recipes 14
Bad algorithms n Not all algorithms are “good” n So then what makes an algorithm “bad”? n n n Can be wrong Can be inefficient Can have other errors as well 15
Bad algorithms: Map. Quest directions n Consider directions to get around grounds 16
17
Note that this is not an incorrect algorithm! Just a very inefficient one 18
Bad algorithms: Shampoo directions 19
20
Bad algorithms: Shampoo directions n Lather, rinse, repeat n This algorithm repeats forever! n n 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 21
Bad algorithms: Inexact recipes 22
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? 23
“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 24
Note that this is not an incorrect algorithm! Just a very inefficient one 25
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! 26
How do we tell all this to a computer? n “Computer: Tea, Earl Gray, hot” n n Unfortunately, that doesn’t work so well today… n n Jean-Luc Picard from Star Trek Computers don’t understand English Instead, we’ll use a computer language: Java 27
Onto the book chapter n A lot of this terminology will be confusing at first n We will be going over it in more detail throughout the semester 28
Computer Organization • Computer advertisement specification – Intel® Pentium 4 Processor at 3. 06 GHz with 512 K cache – 512 MB DDR SDRAM – 200 GB ATA-100 Hard Drive (7200 RPM, 9. 0 ms seek time) – 17” LCD Monitor – 64 MB NVIDIA Ge. Force 4 MX Graphics Card® – 16 x Max DVD-ROM Drive – 48 x/24 x/48 x CD-RW Drive – 56 K PCI Telephony Modem – Windows XP Home Edition® – 10/100 Fast Ethernet Network Card 512 million bytes of 3. 06 Reads memory 17” Computer Can Microprocessor Stores billion on send read DVDs the 200 that or and operations operating diagonal. 16 billion receive can write times for be faster displaying bytes data CDs. up Resolution system to per than at Can of 56 two second data. images athousand using hold basic rates up You 650 to awith DVD – transferred at double 10 drive. million want 64 graphical or the 1, 280 bits million 100 high normal Can per bytes by million hold interface RPM second bytes 1, 024 of rate up and data bytes ofto memory. 8 Reads billion low per seek pixels at bytes More second 48 time. memory times of data supports 0. 009 faster A byteseconds and more is 8 writes bits colors is and 24 times higher average faster resolution than A bit a basic is a 0 drive or a 29 1
Backbones 30
A bit of humor… 31
An optical illusion 32
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) 33
Java’s Direct Lineage n C (1972) by Dennis Ritchie n C++ (1985) by Bjarne Stroustrup n Java (1991) by James Gosling and others at Sun Microsystems 34
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 35
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) 36
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 37
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 to advertise the drive? 38
Programming Task n Display the forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943. 39
Display. Forecast. java // 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. "); } } Anmethod Three A application statements is a named program make piece is uprequired the of code action to that have of performs method a main()action some public static void or implements method named a behavior main(). Method main() is part of class Display. Forecast 40
Sample output 41
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 42
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. "); } } 43
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. "); } } 44
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 45
Carved watermelons 46
Software engineering n Goal n Production of software that is effective and reliable, understandable, cost effective, adaptable, and reusable n Worktocorrectly Because Cost Design Makes software sense develop of thedue and long and so not that to lifetime maintain the fail newgreat many features should people costs not andexceed involved will capabilities be expected involved to can havebe benefit flexible added components n Creation that can be used in other software n n Debugging Maintenance Enhancement Two-thirds of the cost is typically beyond creation 47
Principles of software engineering n Abstraction n Encapsulation n Modularity n Hierarchy Separate Construct Determine Rankingcomponents or a the system ordering relevant from of into properties external objects andpackages internal features components and while ignoring aspects nonessential details 48
Object-oriented design n Purpose n Promote thinking about software in a way that models the way we think and interact with the physical word n n Including specialization Object n n Properties or attributes Behaviors 49
Programming n Class n n Term for a type of software object Object n An instance of a class with specific properties and attributes 50
Programming n n Problem solving through the use of a computer system Maxim n You cannot make a computer do something if you do not know how to do it yourself 51
Problem Solving n Remember Solutions Accept n goal is not a clever solution a correct solution Oftenprocess The require isboth iterative concrete and but abstract thinking n Teamwork In solving the problem increased understanding might require restarting 52
Problem Solving Process n What is it? n n Analysis Design Implementation Testing Describe the components and associated processes for solving the problem Determine the inputs, and other components the Develop solutions for outputs, the components and use those of components Test the components individually and collectively problem to produce an overalland solution Straightforward flexible Description should beflexible sufficiently specific to allow you to solve Method – process Straightforward and the problem Object – component and associated methods 53
Problem Solving Process 54
Tips n Find out as much as you can n Reuse what has been done before n Expect future reuse n Break complex problems into subproblems 55
Tips n Find out as much as you can n Reuse what has been done before n Expect future reuse n Break complex problems into subproblems Consider Research can is require and generate questions Find out what knownsignificant about thetime problem Sketching apresenter solution andbecause then repeatedly its components The worthwhile the resultrefine is a better Talk effort to theis until the entire process is specified understanding Determine what attempts have succeeded and what attempts True of the problem makes it easier to solve have understanding failed 56
Tips n Find out as much as you can n Reuse what has been done before n Expect future reuse n Break complex problems into subproblems Your Be open timetoisindirect valuable use of existing materials Correctness is probably even more valuable Use existing infrastructure that is known to work 57
Tips n Find out as much as you can n Reuse what has been done before n Expect future reuse n Break complex problems into subproblems Make as few assumptions as necessary Maximizes the likelihood that your effort can be used in future situations 58
Tips n Find out as much as you can n Reuse what has been done before n Expect future reuse n Break complex problems into subproblems Divide-and-conquer Solve subproblems and combine into an overall solution 59
Today’s demotivators 60
- Slides: 59