1.03k likes | 1.52k Views
Introduction. What is a Computer? Programming a Computer Types of Computer Systems History of Computers The Software Crisis Programming Languages. What is a Computer?. Computer is a machine Has parts Runs on electricity Breaks down sometimes What makes it different from other machines?
E N D
Introduction What is a Computer? Programming a Computer Types of Computer Systems History of Computers The Software Crisis Programming Languages
What is a Computer? • Computer is a machine • Has parts • Runs on electricity • Breaks down sometimes • What makes it different from other machines? • Can follow a list of instructions • Instructions process symbols A computer isan information-processing machine Therefore Programming and Problem Solving with Java
What is a Computer? • Computer follows a list of instructions one instruction at a time • Start with first instruction • Do each next instruction in turn • Stop when last instruction reached • Similar to how you follow a list of instructions... Programming and Problem Solving with Java
What is a Computer? • Example of following instructions - assemble a bike • Right Pedal • 1. Find the pedal with an "R" marked on the end of the threads. • 2. Insert the threads into the opening on the crank on the right side of the bicycle. • 3. Twist the threads into the crank by hand in a clockwise direction. • 4. Tighten the threads securely with a 5/8-inch open-end wrench. • Left Pedal • Follow the same assemblyinstructions for the right pedal (above) but twist the threads in a counterclockwise direction. Programming and Problem Solving with Java
What is a Computer? • Kinds of instructions a computer can follow (in one step) • “Add the numbers 16 and 23” • Kinds of instructions a computer can’t follow (in one step) • “Balance my checkbook” • What’s the difference? • Computer instructions are simple (add 2 numbers, subtract one number from another) • Must combine thousands of computer instructions in a program to do more complex tasks Programming and Problem Solving with Java
What is a Computer? • Kinds of computer instructions • Arithmetic: add, subtract, multiply, divide • Comparison: compare two numbers to see which is greater, or whether the two numbers are equal or not • Branching: jump to some other instruction in the program (list of instructions) and continue processing from there • A computer can execute millions of these simple instructions each second Programming and Problem Solving with Java
What is a Computer? • Major parts of a computer Programming and Problem Solving with Java
What is a Computer? • A vending machineis like acomputer Programming and Problem Solving with Java
What is a Computer? • Common input and output devices Programming and Problem Solving with Java
What is a Computer? • Central Processing Unit (CPU) • Most important part of the computer • Follows instructions • Important characteristics • Clock speed • How fast the computer switches between 2 on states or 2 off states • Early 80’s: 5,000,000 times per second (5 MHz) • Today: 400,000,000 to 1,000,000,000 and up! (400 MHz to 1 GHz) • Instruction set • The set of machine language instructions the CPU understands • Examples: Intel Pentium, Motorola PowerPC, IBM AS/400 • Some CPUs understand machine language instructions of others -- AMD and Cyrix chips understand Intel Pentium instructions Programming and Problem Solving with Java
What is a Computer? • Subcomponents of a CPU • The arithmetic/logic unit, or ALU: does addition, subtraction, and other arithmetic operations, and also compares values • The control unit. determines which instruction to execute next • A small number of registers. each is a high-speed storage area for temporary results Programming and Problem Solving with Java
What is a Computer? • Types of Computer Memory • Primary • Secondary • Primary Memory • Connected directly to the CPU • Usually called RAM (random-access memory) • Usually loses contents when power switched off -- volatile Programming and Problem Solving with Java
What is a Computer? • Secondary Memory • Disks, tapes, floppy disks • Is sometimes removable • Much cheaper than primary memory • Slower than primary memory • Keeps contents when power switched off -- non-volatile Programming and Problem Solving with Java
What is a Computer? • Types of Computer Memory Programming and Problem Solving with Java
What is a Computer? • Primary Memory • Can imagine as sequence of cells • Each cell has a memory address • First cell is memory address 0 Programming and Problem Solving with Java
What is a Computer? • Primary Memory • Each memory cell stores a single value, such as a small number • If you put a new value into a memory cell, the old contents is lost Programming and Problem Solving with Java
What is a Computer? • Primary Memory • Can retrieve the contents of a cell without losing the contents Programming and Problem Solving with Java
What is a Computer? • Secondary Memory • Information clustered in files • File: collection of related information • Each file has a name • Example: NAMES.DAT, Balance.java Programming and Problem Solving with Java
Programming a Computer • How we solve most problems without a computer • Analyze the problem, come up with a solution Programming and Problem Solving with Java
Programming a Computer • How we can get the computer to solve a problem • Must come up with an algorithm (series of steps) • Computer follows the steps to solve the problem Programming and Problem Solving with Java
Programming a Computer • Algorithm • Steps that a computer can follow to solve a problem • Holiday Cookies • 1 cup sugar 1/4 tsp. ginger • 3/4 cup shortening 1 tsp. cinnamon • 1 cup dark corn syrup 1/2 tsp. nutmeg • 2 eggs 1/2 tsp. cloves • 1 lemon rind (grated) 2 tsp. anise seed • 3 cups flour 3/4 tsp. ground cardaman • 1 tsp. baking powder icing (powder sugar and water) • 1 tsp. baking soda colored sugar and candy decorations • 1. Sift dry ingredients together in a large bowl and set aside. • 2. In another large bowl, use mixer to cream sugar and shortening together. • 3. Mix syrup, eggs, and lemon rind into sugar and shortening mixture. • 4. Add dry ingredients to mixture gradually. • 5. Refrigerate at least one hour to make handling easier. • 6. With rolling pin, roll out to 1/8-inch thickness on a floured surface, and cut with cookie cutters. • 7. Bake on greased cookie sheet 10-12 minutes at 375°F. • 8. Remove from cookie sheet and let cool to room temperature. • 9. Frost with icing, and decorate with colored sugar and candy. Similar to a recipe Programming and Problem Solving with Java
Programming a Computer • Recipe lists steps, but doesn’t say who is doing the steps • Implied that you (the cook) should do the steps • Many algorithms written this way -- structured approach to programming • New approach -- object-oriented programming • Consists of a set of cooperating objects trying to accomplish a goal • Each step says who (which object) should do that step • Similar to the script for a play Programming and Problem Solving with Java
Programming a Computer • THE IMPORTANCE OF BEING EARNEST • by Oscar Wilde • The Persons of the Play • John Worthing, J.P. Lady Bracknell • Algernon Moncrieff Hon. Gwendolen Fairfax • Rev. Canon Chasuble, D.D. Cecily Cardew • Merriman, Butler Miss Prism, Governess • Lane, Manservant • The Scenes of the Play • Act I: Algernon Moncrieff's Flat in Half-Moon Street, W. • Act II: The Garden at the Manor House, Woolton. • Act III: Drawing-Room at the Manor House, Woolton Programming and Problem Solving with Java
Programming a Computer • THE IMPORTANCE OF BEING EARNEST (continued) • ACT ONE • SCENE. Morning-room in Algernon's flat in Half-Moon Street. The room is luxuriously and artistically furnished. The sound of a piano is heard in the adjoining room. (Lane is arranging afternoon tea on the table, and after the music has ceased, Algernon enters.) • ALGERNON. Did you hear what I was playing, Lane? • LANE. I didn't think it polite to listen, sir. • ALGERNON. I'm sorry for that, for your sake. I don't play accurately-anyone can play accurately-but I play with wonderful expression. As far as the piano is concerned, sentiment is my forte. I keep science for life. • LANE. Yes, sir. • ALGERNON. And, speaking of the science of life, have you got the cucumber sandwiches cut for Lady Bracknell? • LANE. Yes, sir. (Hands them on a salver.) • ALGERNON. (Inspects them, takes two, and sits down on the sofa.) Oh! ... by the way, Lane, I see from your book that on Thursday night, when Lord Shoreman and Mr. Worthing were dining with me, eight bottles of champagne are entered as having been consumed. • LANE. Yes, sir; eight bottles and a pint. • ALGERNON. Why is it that at a bachelor's establishment the servants invariably drink the champagne? I ask merely for information. Programming and Problem Solving with Java
Programming a Computer • THE IMPORTANCE OF BEING EARNEST (continued) • LANE. I attribute it to the superior quality of the wine, sir. I have often observed that in married households the champagne is rarely of a first-rate brand. • ALGERNON. Good Heavens! Is marriage so demoralizing as that? • LANE. I believe it is a very unpleasant state, sir. I have had very little experience of it myself up to the present. I have only been married once. That was in consequence of a misunderstanding between myself and a young person. • ALGERNON. (Languidly.) I don't know that I am much interested in your family life, Lane. • LANE. No, sir; it is not a very interesting subject. I never think of it myself. • ALGERNON. Very natural, I am sure. That will do, Lane, thank you. • LANE. Thank you, sir. (Lane goes out.) • ALGERNON. Lane's views on marriage seem somewhat lax. Really, if the lower orders don't set us a good example, what on earth is the use of them? They seem, as a class, to have absolutely no sense of moral responsibility. • (Enter Lane.) • LANE. Mr. Ernest Worthing. • (Enter Jack. Lane goes out.) • ... Programming and Problem Solving with Java
Programming a Computer • Why is an object-oriented algorithm like a script? • Object-oriented algorithm simulates activities of several explicit actors • Each actor has specific steps to perform • Each actor free to use any appropriate technique to accomplish its task • Combined effort of all these simulated actors results in the goal of the algorithm • Combined effort of real actors of a play results in enjoyable entertainment Programming and Problem Solving with Java
Programming a Computer • Programming languages • Very formal language with strict rules about spelling and grammar • Need write algorithm in a programming language before running the algorithm on the computer • Many programming languages • Will use Java in this course Programming and Problem Solving with Java
Programming a Computer • Computers understand machine language directly • Example: add 16 and 23 in Intel 8086 machine language • 1011 0000 0001 0000 • 0000 0100 0001 0111 • 1010 0010 0100 1000 0000 0000 Programming and Problem Solving with Java
Programming a Computer • Parts of machine language program step • First statement from example • Instruction says: Copy the number 16 into the AL register • Difficult to read and write machine language! 1011 0000 0001 0000 Copy information to a register Use the AL register The number 16 in binary (base 2) Programming and Problem Solving with Java
Programming a Computer • Assembly language • One step up from machine language • Each assembly language instruction corresponds to one machine language instruction MOV AL,16D 1011 0000 0001 0000 ADD AL,23D 0000 0100 0001 0111 MOV [SUM],AL 1010 0010 0100 1000 0000 0000 Programming and Problem Solving with Java
Programming a Computer • Assembly language • Computer can’t understand assembly language directly • Must translate from assembly to machine language • Assembler: program that does this translation Programming and Problem Solving with Java
Programming a Computer • High-level languages • Each high-level language instruction may correspond to several machine language instructions • Easier for people to read and write than assembly • Java example: • (Note: Java usually translated to Java bytecodes, not Intel machine language...) 1011 0000 0001 0000 sum = 16 + 23; 0000 0100 0001 0111 1010 0010 0100 1000 0000 0000 Programming and Problem Solving with Java
Programming a Computer • High-level languages • Must translate high-level language program to machine language before computer can execute it • Compiler: program that does this translation Programming and Problem Solving with Java
Programming a Computer: Java • The Java Programming Language • A high-level language • Java compiler translates to Java bytecodes for execution on a Java Virtual Machine (JVM) Programming and Problem Solving with Java
Programming a Computer: Java • Advantage of Java Virtual Machine approach • Can run compiled Java program on any machine with a Java Virtual Machine installed • Makes compiled Java programs very portable • Disadvantage • Takes more time -- Java programs run slower than programs compiled to machine code • BUT ... • Today’s machines are so fast that the difference is not noticeable for many programs Programming and Problem Solving with Java
Programming a Computer: Java • Kinds of Java programs • Applications • Applets • Applications • Run as stand-alone programs • Programs stored on the same machine they run on • Like a text editor, word processor, spreadsheet, ... • Can be graphical or console-based • Applets • Run inside a web browser • Programs stored on a web server, run on a client machine Programming and Problem Solving with Java
Programming a Computer: Java • Example Java program (application) • // This Java program finds the sum of 16 and 23, • // then displays the result. • public class Add16And23 • { • public static void main(String[] args) • { • int sum; • sum = 16 + 23; • System.out.println("The sum of 16 and 23 is " + sum); • } • } • Steps to run this program • Type the program text exactly • Save the program in file Add16And23.java • Compile the program with a Java compiler • Run program Add16And23.class with a JVM interpreter Programming and Problem Solving with Java
Programming a Computer: Java • Use an editor that saves text files, such as MS-DOS edit (in MS-DOS box) Programming and Problem Solving with Java
Programming a Computer: Java • Compile the program • javac Add16And23.java • Run the program • java Add16And23 • Result: program should display • The sum of 16 and 23 is 39 $ javac Add16And23.java $ java Add16And23 The sum of 16 and 23 is 39 $ Unix display Programming and Problem Solving with Java
Programming: Development • Example program: display average of series • First step: understand the problem • Where should program get the numbers? Keyboard • How does program know how many numbers? Will enter 0 at end of list • Where should program display result? On screen • Second step: come up with algorithm • Solve problem yourself -- write down steps you took • Result is an algorithm Programming and Problem Solving with Java
Programming: Development • Steps for finding average of series of numbers Programming and Problem Solving with Java
Programming: Development • Convert steps into an algorithm • Display the program's title • Set running total to 0. • Set running count to 0. • Repeat these steps: • Read a value. • Check if value is 0 (stop this loop if so). • Add value to running total. • Add 1 to running count. • Compute the average by dividing the running total by the count. • Display the average. Programming and Problem Solving with Java
Programming: Development • Convert algorithm into program • // This program displays the average of a series of • // numbers terminated by zero. The user enters the numbers using • // the keyboard, and the average is displayed on the screen. • import java.io.*; • import java.text.NumberFormat; • public class ComputeAverage • { • public static void main(String[] args) • throws java.io.IOException, java.text.ParseException • { • double inputValue; // Last value read from the user • double runningTotal; // Total of all input values read so far • int runningCount; // Count of all input values read so far • double average; // Average of input values • // Create an input stream and attach it to the standard • // input stream • BufferedReader inStream • = new BufferedReader(new InputStreamReader(System.in)); • // Create a number formatter object • NumberFormat formatter = NumberFormat.getInstance(); • // Display the program title • System.out.println("--- Compute Average ---"); • // Set running total to 0 • runningTotal = 0; Programming and Problem Solving with Java
Programming: Development • // Set running count to 0 • runningCount = 0; • // Read the first input value • System.out.print("Enter a value (0 to stop): "); • System.out.flush(); • inputValue = formatter.parse(inStream.readLine()).doubleValue(); • // Keep reading until 0 • while (inputValue != 0) • { • // Add value to running total • runningTotal = runningTotal + inputValue; • // Add 1 to running count • runningCount = runningCount + 1; • // Read the next input value • System.out.print("Enter a value (0 to stop): "); • System.out.flush(); • inputValue • = formatter.parse(inStream.readLine()).doubleValue(); • } • // Compute the average • average = runningTotal / runningCount; • // Display the average • System.out.println("The average is " + average); • } • } Programming and Problem Solving with Java
Programming: Development • Last step: Make sure the program works correctly • First test -- ok • --- Compute Average --- • Enter a value (0 to stop): 9 • Enter a value (0 to stop): 3.2 • Enter a value (0 to stop): 8 • Enter a value (0 to stop): 0 • The average is 6.73333 • Second test -- ok • --- Compute Average --- • Enter a value (0 to stop): 9.1 • Enter a value (0 to stop): 2.6 • Enter a value (0 to stop): 7.3 • Enter a value (0 to stop): 1.4 • Enter a value (0 to stop): 6.44 • Enter a value (0 to stop): 8 • Enter a value (0 to stop): 5.72 • Enter a value (0 to stop): 0 • The average is 5.79429 • Third test -- not ok (doesn’t work if nothing to average) • --- Compute Average --- • Enter a value (0 to stop): 0 • The average is NaN Programming and Problem Solving with Java
Programming: Development • Modified program • // This program displays the average of a series of • // numbers terminated by zero. The user enters the numbers using • // the keyboard, and the average is displayed on the screen. • import java.io.*; • import java.text.NumberFormat; • public class ComputeAverage • { • public static void main(String[] args) • throws java.io.IOException, java.text.ParseException • { • double inputValue; // Last value read from the user • double runningTotal; // Total of all input values read so far • int runningCount; // Count of all input values read so far • double average; // Average of input values • // Create an input stream and attach it to the standard • // input stream • BufferedReader inStream • = new BufferedReader(new InputStreamReader(System.in)); • // Create a number formatter object • NumberFormat formatter = NumberFormat.getInstance(); • // Display the program title • System.out.println("--- Compute Average ---"); • // Set running total to 0 • runningTotal = 0; Programming and Problem Solving with Java
Programming: Development • // Set running count to 0 • runningCount = 0; • // Read the first input value • System.out.print("Enter a value (0 to stop): "); • System.out.flush(); • inputValue = formatter.parse(inStream.readLine()).doubleValue(); • // Keep reading until 0 • while (inputValue != 0) • { • // Add value to running total • runningTotal = runningTotal + inputValue; • // Add 1 to running count • runningCount = runningCount + 1; • // Read the next input value • System.out.print("Enter a value (0 to stop): "); • System.out.flush(); • inputValue • = formatter.parse(inStream.readLine()).doubleValue(); • } • if (runningCount > 0) • { • // Compute the average • average = runningTotal / runningCount; • // Display the average • System.out.println("The average is " + average); • } • else • System.out.println("Can't compute average: No input values"); • } • } Changes Programming and Problem Solving with Java
Programming: Development • Last step: Test program again • First test -- ok • --- Compute Average --- • Enter a value (0 to stop): 9 • Enter a value (0 to stop): 3.2 • Enter a value (0 to stop): 8 • Enter a value (0 to stop): 0 • The average is 6.73333 • Second test -- ok • --- Compute Average --- • Enter a value (0 to stop): 9.1 • Enter a value (0 to stop): 2.6 • Enter a value (0 to stop): 7.3 • Enter a value (0 to stop): 1.4 • Enter a value (0 to stop): 6.44 • Enter a value (0 to stop): 8 • Enter a value (0 to stop): 5.72 • Enter a value (0 to stop): 0 • The average is 5.79429 • Third test -- ok • --- Compute Average --- • Enter a value (0 to stop): 0 • Can't compute average: No input values Programming and Problem Solving with Java
Programming: Development • Steps in program development • Understand the problem • Come up with an algorithm • Write the program • Test the program Programming and Problem Solving with Java
Programming: Development • Understand the problem • Need to understand problem thoroughly before beginning to write program • Tempting to start writing program before understanding problem -- probably single most common cause of errors in programs • How to understand the problem • Look at input and output requirements • Ask people involved with the problem domain • Try writing about the program -- write what it should do in your own words Programming and Problem Solving with Java