310 likes | 463 Views
CMPE003 – Personal Computer Concepts. Hardware and Software Winter 2004 Instructor: Scott Cooper scooper@soe.ucsc.edu. Assignment 4. Budget spreadsheet. Chapter 15: Programming Languages. Telling the Computer What to Do. Programming.
E N D
CMPE003 – Personal Computer Concepts Hardware and Software Winter 2004 Instructor: Scott Cooper scooper@soe.ucsc.edu
Assignment 4 • Budget spreadsheet
Chapter 15: Programming Languages Telling the Computer What to Do
Programming • Creating the sequence of instructions that tells the computer what to do • Input data • Output data • Transform data
Speaking the language • Programming languages • Different ways of describing the sequence of instructions • Formalized constructs for telling the computer what to do • Must be translated into machine language • CPU instructions
Cooking up programs • Program code is like a recipe • Terms like “dice” and “sauté” stand for procedures described elsewhere • Order matters: you can’t cook the eggs first when making a custard
Successive refinement • Computers are very exact and literal • Humans are very imprecise and vague • Translating human intention into precise code is done in stages called successive refinement
Programming is hard “Adjusting to the requirement for perfection [is] the most difficult part of learning to program” Frederick P. Brooks, Jr., The Mythical Man-Month
Define the problem • Step 1: Carefully describe what it is you want to accomplish • Input data • Output data • Processing • User Interface (UI) This description should be understandable to an informed, non-technical audience
Describe the solution • Step 2: develop an algorithm – a detailed, step-by-step solution • May use mathematical techniques and reasoning • Desk checking: carrying out the steps by hand to verify the algorithm’s correctness
Tools for algorithm design • Flow charts • Pseudocode • Various formal and informal diagrams
Coding the solution • Step 3: Translate the algorithm to programming language source code • Must adhere to the syntax or grammar of the programming language • Errors must be corrected before the program can run • Can use a text editor or code editor to write source code
Compile • Step 4: Compile or translate your program code into the CPU’s binary instructions • Compiler is a program itself • Input: source code • Output: object code (or error messages) • One more step: linking • Now we are ready for testing
Testing • Step 5: Test the program • Run it on different inputs and check the output for correctness • Finding and correcting logical errors is called debugging
Documenting programs • Very important for later debugging and maintenance • Design documents • Testing procedures • Program comments • Narrative description within the code • In HTML: <!– This is a comment -->
Categorizing languages • High vs. low level • Low: closer to machine’s native 1s and 0s • High: more like human language • Compiled vs. interpreted • Compiled: translated in one step ahead of time • Interpreted: translated on-the-fly each time
Five language generations • Machine language • Assembly language • High-level languages • Very high-level languages • Natural languages
Machine language • Programs written directly in 1s and 0s • Usually with switches • Tedious and error-prone
Assembly language • Short mnemonics substituted for 0s and 1s • A for add, C for compare • Names for registers: R1, R2, … • Can use names instead of memory addresses • Requires a translator called assembler • Can use names for branch targets
High-level languages • Independent of CPU instructions • Languages focus on solving problems rather than controlling hardware for the first time • Programs can be written and debugged much more quickly • Requires a translator called a compiler • Each computer needs its own compiler for each language
Very high-level languages • Also called 4GLs • Specify the results and the solution is created automatically • Programmers can be much more productive • Specialized for certain types of tasks
Natural language • Make requests in natural syntax • “Just ask for what you want!” • Like 4GL, specialized for applications such as database queries
Major languages • Fortran • COBOL
Types of programming • Procedural programming • Subroutines break up program logic • Modular programming • Package related routines and data • Object-oriented programming • Data and operations bound together
O-O terms • Object – entity containing data and associated actions • Method – action that can be performed on an object • Attribute – piece of information describing an object (also member)
Object design • Objects defined in classes • Objects are instanced when they are actually used • Objects grouped in hierarchies by inheritance • Subclasses inherit the data and behavior of their parent • Can define new behavior or redefine existing methods
Using objects • Create an instance • Send a message • Handled by methods • Polymorphism • Messages are handled differently by different types of objects
O-O Languages • C++ • Java • Visual Basic .NET • C#
Finding average • Find the average of n numbers • Input: n numbers in consecutive memory locations • Output: numerical average of all n numbers • Sum the numbers • Divide by n
Average pseudocode total <- 0 for count <- 1 to n total <- total + numbers[count] average <- total / n
Average in C float average(int numbers[], int n) { int count; float total; for (count=0; count<n; count++) total = total + numbers[count]; return total / n; }