Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Library (computing) wikipedia , lookup
Functional programming wikipedia , lookup
Programming language wikipedia , lookup
Program optimization wikipedia , lookup
Go (programming language) wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Software bug wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Reactive programming wikipedia , lookup
Object-oriented programming wikipedia , lookup
1 Introduction to Computing and Programming C# Programming: From Problem Analysis to Program Design 2nd Edition C# Programming: From Problem Analysis to Program Design 1 Chapter Objectives • Learn about the history of computers • Explore the physical components of a computer system • Examine how computers represent data • Learn to differentiate between system and application software C# Programming: From Problem Analysis to Program Design 2 Chapter Objectives (continued) • Learn the steps of software development • Explore different programming methodologies • Become aware of how C# and .NET evolved and fit together • Learn why C# is being used today for software development C# Programming: From Problem Analysis to Program Design 3 History of Computers • Computing dates back 5,000 years • Currently in fourth or fifth generation of modern computing • Pre-modern computing – Abacus – Pascaline (1642) – Analytical Engine (1830 – Charles Babbage & Lady Lovelace) C# Programming: From Problem Analysis to Program Design 4 History of Computers (continued) Figure 1-1 The abacus, the earliest computing device C# Programming: From Problem Analysis to Program Design 5 History of Computers (continued) • First generation distinguished by use of vacuum tubes (mid 1940s) • Second generation distinguished by use of transistors (mid 1950s) – Software industry born (COBOL, Fortran) • Third generation – transistors squeezed onto small silicon discs (1964-1971) – Computers became smaller – Operating systems first seen C# Programming: From Problem Analysis to Program Design 6 History of Computers (continued) Figure 1-2 Intel chip C# Programming: From Problem Analysis to Program Design 7 History of Computers (continued) • Fourth generation – computer manufacturers brought computing to general consumers – Introduction of IBM personal computer (PC) and clones (1981) • Fifth generation – more difficult to define – Computers accept spoken word instructions – Computers imitate human reasoning through AI – Computers communicate globally – Mobile and wireless applications are growing C# Programming: From Problem Analysis to Program Design 8 Physical Components of a Computer System • Hardware – Physical devices that you can touch • Central processing unit (CPU) – Brain of the computer • Housed inside system unit on silicon chip • Most expensive component • Performs arithmetic and logical comparisons on data and coordinates the operations of the system C# Programming: From Problem Analysis to Program Design 9 Physical Components of a Computer System (continued) Figure 1-3 Major hardware components C# Programming: From Problem Analysis to Program Design 10 Physical Components of a Computer System (continued) Figure 1-4 CPU’s instruction cycle C# Programming: From Problem Analysis to Program Design 11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17 Physical Components of a Computer System (continued) • Primary storage – main memory – Called random-access memory (RAM) – Cache • Type of random access memory that can be accessed more quickly than regular RAM • Acts like a buffer, or temporary storage location • Two forms of cache memory: L1 and L2 – Each cell has a unique address C# Programming: From Problem Analysis to Program Design 18 Physical Components of a Computer System (continued) Figure 1-5 Addressing in memory C# Programming: From Problem Analysis to Program Design 19 Physical Components of a Computer System (continued) • Auxiliary storage – secondary storage – Nonvolatile, permanent memory – Most common types are magnetic and optic disks (hard disk, CD, DVD, zip, and flash memory) • Input/Output Devices – Used to get data inside the machine – Drive is the device used to store/retrieve from several types of storage media C# Programming: From Problem Analysis to Program Design 20 Data Representation • Bits – Bit – "Binary digIT" – Binary digit can hold 0 or 1 – 1 and 0 correspond to on and off, respectively • Bytes – Combination of 8 bits – Represent one character, such as the letter A – To represent data, computers use the base-2 number system, or binary number system C# Programming: From Problem Analysis to Program Design 21 Binary Number System Figure 1-6 Base–10 positional notation of 1326 C# Programming: From Problem Analysis to Program Design 22 Binary Number System (continued) Figure 1-7 Decimal equivalent of 01101001 C# Programming: From Problem Analysis to Program Design 23 Data Representation (continued) C# Programming: From Problem Analysis to Program Design 24 Data Representation (continued) • Character sets – With only 8 bits, can represent 28, or 256, different decimal values ranging from 0 to 255; this is 256 different characters • Unicode – Character set used by C# (pronounced C Sharp) – Uses 16 bits to represent characters – 216, or 65,536 unique characters, can be represented • American Standard Code for Information Interchange (ASCII) – subset of Unicode – First 128 characters are the same C# Programming: From Problem Analysis to Program Design 25 Data Representation (continued) C# Programming: From Problem Analysis to Program Design 26 Software • Consists of programs – Sets of instructions telling the computer exactly what to do • Two types of software – System – Application • Power of what the computer does lies with what types of software are available C# Programming: From Problem Analysis to Program Design 27 System Software • System software is more than operating systems • Operating System – Loaded when you power on the computer – Examples include Windows XP, Windows NT, UNIX, and DOS – Includes file system utilities, communication software • Includes compilers, interpreters, and assemblers C# Programming: From Problem Analysis to Program Design 28 Application Software • Application software performs a specific task – Word processors, spreadsheets, payroll, inventory • Writes instructions using a high-level programming language – C#, Java, Visual Basic • Compiler – Translates instructions into machine-readable form – First checks for rule violations • Syntax rules – how to write statements C# Programming: From Problem Analysis to Program Design 29 Software (continued) Figure 1-8 A machine language instruction C# Programming: From Problem Analysis to Program Design 30 Software Development Process • Programming is a process of problem solving • How do you start? • Number of different approaches, or methodologies • Successful problem solvers follow a methodical approach C# Programming: From Problem Analysis to Program Design 31 Steps in the Program Development Process 1. Analyze the problem 2. Design a solution 3. Code the solution 4. Implement the code 5. Test and debug 6. Use an iterative approach C# Programming: From Problem Analysis to Program Design 32 Steps in the Program Development Process • Software development process is iterative • As errors are discovered, it is often necessary to cycle back to a previous phase or step Figure 1-13 Steps in the software development process C# Programming: From Problem Analysis to Program Design 33 Step 1: Analyze the Problem • Precisely what is software supposed to accomplish? • Understand the problem definition • Review the problem specifications C# Programming: From Problem Analysis to Program Design 34 Analyze the Problem (continued) Figure 1-9 Program specification sheet for a car rental agency problem C# Programming: From Problem Analysis to Program Design 35 Analyze the Problem (continued) • What kind of data will be available for input? • What types of values (i.e., whole numbers, alphabetic characters, and numbers with decimal points) will be in each of the identified data items? • What is the domain (range of the values) for each input item? • Will the user of the program be inputting values? • If the problem solution is to be used with multiple data sets, are there any data items that stay the same, or remain constant, with each set? C# Programming: From Problem Analysis to Program Design 36 Analyze the Problem (continued) May help to see sample input for each data item Figure 1-10 Data for car rental agency C# Programming: From Problem Analysis to Program Design 37 Step 2: Design a Solution • Several approaches – Procedural and object-oriented methodologies • Careful design always leads to better solutions • Divide and Conquer – Break the problem into smaller subtasks – Top-down design, stepwise refinement • Algorithms for the behaviors (object-oriented) or processes (procedural) should be developed C# Programming: From Problem Analysis to Program Design 38 Design a Solution (continued) • Algorithm – Clear, unambiguous, step-by-step process for solving a problem – Steps must be expressed so completely and so precisely that all details are included – Instructions should be simple to perform – Instructions should be carried out in a finite amount of time – Following the steps blindly should result in the same results C# Programming: From Problem Analysis to Program Design 39 Design • Object-oriented approach • Class diagram – Divided into three sections • Top portion identifies the name of the class • Middle portion lists the data characteristics • Bottom portion shows what actions are to be performed on the data C# Programming: From Problem Analysis to Program Design 40 Class Diagram Figure 1-11 Class diagram of car rental agency C# Programming: From Problem Analysis to Program Design 41 Class Diagram (continued) Figure 1-15 Student class diagram C# Programming: From Problem Analysis to Program Design 42 Design (continued) • Structured procedural approach – Process oriented – Focuses on the processes that data undergoes from input until meaningful output is produced • Tools used – Flowcharts – Pseudocode, structured English • Algorithm written in near English statements for pseudocode C# Programming: From Problem Analysis to Program Design 43 Flowchart • Oval – beginning and end • Rectangular – processes • Diamond – decision to be made • Parallelogram – inputs and output • Flow line Figure 1-14 Flowchart symbols and their interpretation C# Programming: From Problem Analysis to Program Design 44 Step 3: Code the Solution • After completing the design, verify the algorithm is correct • Translate the algorithm into source code – Follow the rules of the language • Integrated Development Environment (IDE) – Visual Studio • Tools for typing program statements, compiling, executing, and debugging applications C# Programming: From Problem Analysis to Program Design 45 Step 4: Implement the Code • Source code is compiled to check for rule violations • C# → Source code is converted into Microsoft Intermediate Language (IL) – IL is between high-level source code and native code – IL code not directly executable on any computer – IL code not tied to any specific CPU platform • Second step, managed by .NET’s Common Language Runtime (CLR), is required C# Programming: From Problem Analysis to Program Design 46 Implement the Code (continued) • CLR loads .NET classes • A second compilation, called a just-in-time (JIT) compilation is performed – IL code is converted to the platform’s native code Figure 1-12 Execution steps for .NET C# Programming: From Problem Analysis to Program Design 47 Step 5: Test and Debug • Test the program to ensure consistent results • Test Driven Development (TDD) – Development methodologies built around testing • Plan your testing – Test plan should include extreme values and possible problem cases • Logic errors – Might cause abnormal termination or incorrect results to be produced – Run-time error is one form of logic error C# Programming: From Problem Analysis to Program Design 48 Programming Methodologies • Structured Procedural Programming – Emerged in the 1970s – Associated with top-down design • Analogy of building a house • Write each of the subprograms as separate functions or methods invoked by a main controlling function or module – Drawbacks • During software maintenance, programs are more difficult to maintain • Less opportunity to reuse code C# Programming: From Problem Analysis to Program Design 49 Programming Methodologies (continued) • Object-oriented – Newer approach – Construct complex systems that model real-world entities – Facilitates designing components – Assumption is that the world contains a number of entities that can be identified and described C# Programming: From Problem Analysis to Program Design 50 Object-Oriented Methodologies • Abstraction – Through abstracting, determine attributes (data) and behaviors (processes on the data) of the entities • Encapsulation – Combine attributes and behaviors to form a class (Wrapping the data and functions into single entity) • Polymorphism – Methods of parent and subclasses can have the same name, but offer different functionality • Invoke methods of the same name on objects of different classes and have the correct method executed. (more than one forms, like a door) C# Programming: From Problem Analysis to Program Design 51 The Evolution of C# and .NET • 1940s: Programmers toggled switches on the front of computers • 1950s: Assembly languages replaced the binary notation • Late 1950s: High-level languages came into existence • Today: More than 2,000 high-level languages – Noteworthy high-level programming languages are C, C++, Visual Basic, Java, and C# C# Programming: From Problem Analysis to Program Design 52 C# • One of the newest programming languages • Conforms closely to C and C++ • Has the rapid graphical user interface (GUI) features of previous versions of Visual Basic • Has the added power of C++ • Has the object-oriented class libraries similar to Java C# Programming: From Problem Analysis to Program Design 53 C# (continued) • Can be used to develop a number of applications – Software components – Mobile applications – Dynamic Web pages – Database access components – Windows desktop applications – Web services – Console-based applications C# Programming: From Problem Analysis to Program Design 54 .NET • Not an operating system • An environment in which programs run • Resides at a layer between operating system and other applications • Offers multilanguage independence – One application can be written in more than one language • Includes over 2,500 reusable types (classes) • Enables creation of dynamic Web pages and Web services • Scalable component development C# Programming: From Problem Analysis to Program Design 55 C# Relationship to .NET • Many compilers targeting the .NET platform are available • C# was used most heavily for development of the .NET Framework class libraries • C#, in conjunction with the .NET Framework classes, offers an exciting vehicle to incorporate and use emerging Web standards C# Programming: From Problem Analysis to Program Design 56 C# Relationship to .NET (continued) • C# is object-oriented • In 2001, the European Computer Manufacturers Association (ECMA) General Assembly ratified C# and its Common Language Infrastructure (CLI) specifications into international standards C# Programming: From Problem Analysis to Program Design 57 Visual Studio 2005 • Launched November 2005 – Included new language features (C# 2.0) • i.e. partial classes, generics, – Added enhancements to the IDE • i.e. refactoring, code snippets • Less than 6 months after the release, specifications for C# 3.0 and the next version of Visual Studio (code named Orcas) were unveiled [May 2006] C# Programming: From Problem Analysis to Program Design 58 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-59 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-60 Features of Object-Oriented Programming Languages (Cond..) • Interface – Interaction between a method and an object • Inheritance – Provides the ability to extend a class to create a more specific class • Polymorphism – Describes the ability to create methods that act appropriately depending on the context Microsoft Visual C# 2010, Fourth Edition 61 The C# Programming Language • Developed as an object-oriented and component-oriented language • Part of Microsoft Visual Studio 2010 • Allows every piece of data to be treated as an object and to consistently employ the principles of object-oriented programming • Contains a GUI interface that makes it similar to Visual Basic Microsoft Visual C# 2010, Fourth Edition 62 The C# Programming Language (cont'd.) • Modeled after the C++ programming language – However, eliminates some of the most difficult features to understand in C++ • Very similar to Java – In C#, simple data types are objects Microsoft Visual C# 2010, Fourth Edition 63 The first three lines, using System; and other System library services are commonly needed for a simple console application in Visual Studio. The namespace Lincoln tells Visual Studio that you are creating your own application library called Lincoln. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-64 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-65 Program Structure //-----------------------------------------------------------------// Prints a presidential quote //------------------------------------------------------------------ Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-66 Writing a C# Program that Produces Output class namespace Microsoft Visual C# 2010, Fourth Edition literal string method argument 67 Writing a C# Program that Produces Output (cont'd.) • Namespace – Provides a way to group similar classes • C# method parts – Method header • Includes the method name and information about what will pass into and be returned from a method – Method body • Contained within a pair of curly braces and includes all the instructions executed by the method 68 Writing a C# Program that Produces Output (cont'd.) • Whitespace – Any combination of spaces, tabs, and carriage returns (blank lines) – Organizes your code and makes it easier to read • Access modifier – Defines the circumstances under which the method can be accessed • Keywords – Predefined and reserved identifiers that have special meaning to the compiler 69 Writing a C# Program that Produces Output (cont'd.) • The name of the method is Main() – Every application must have a Main() method – Classes with a Main() method are called application classes; others are non-application classes • The method returns nothing as indicated by the keyword void Microsoft Visual C# 2010, Fourth Edition Selecting Identifiers • Requirements – Must begin with an underscore, at sign (@), or letter • Letters include foreign-alphabet letters – Can contain only letters, digits, underscores, and the at sign • Not special characters such as #, $, or & – Cannot be a C# reserved keyword Microsoft Visual C# 2010, Fourth Edition 71 Selecting Identifiers (cont'd.) Microsoft Visual C# 2010, Fourth Edition 72 Selecting Identifiers (cont'd.) Microsoft Visual C# 2010, Fourth Edition 73 Selecting Identifiers (cont'd.) Microsoft Visual C# 2010, Fourth Edition 74 Improving Programs by Adding Program Comments • Program comments – Nonexecuting statements that document a program • Comment out – Turn a statement into a comment • Types of comments in C# – Line comments – Block comments – XML-documentation format comments 75 Adding Program Comments (cont'd.) Microsoft Visual C# 2010, Fourth Edition 76 Using the System Namespace Microsoft Visual C# 2010, Fourth Edition 77 Using the System Namespace (cont'd.) Microsoft Visual C# 2010, Fourth Edition 78 Writing and Compiling a C# Program • Steps for viewing a program output – Compile source code into intermediate language (IL) – C# just in time (JIT) compiler translates the intermediate code into executable statements • You can use either of two ways to compile – The command line – The Integrated Development Environment (IDE) Microsoft Visual C# 2010, Fourth Edition 79 Compiling Code from the Command Prompt • What to do if you receive an operating system error message – Command csc • Stands for “C Sharp compiler” Microsoft Visual C# 2010, Fourth Edition 80 Compiling Code from the Command Prompt (cont'd.) • What to do if you receive a programming language error message – Program error messages start with the program name – Followed by the line number and position within the line of the error Compiling Code from within the Visual Studio IDE • Advantages of using the Visual Studio IDE – Some of the code you need is already created for you – The code is displayed in color – You can double-click an error message and the cursor will move to the line of code that contains the error – Other debugging tools are available Microsoft Visual C# 2010, Fourth Edition 82 Compiling Code from within the Visual Studio IDE (cont'd.) Microsoft Visual C# 2010, Fourth Edition 83 First Program • Enter your first C# program into a text editor so you can execute it • Use any text editor to write the following code and save it as Hello.cs Microsoft Visual C# 2010, Fourth Edition 84 Compiling and Executing a Program from the Command Line • Type the command that compiles your program: csc Hello.cs Microsoft Visual C# 2010, Fourth Edition 85 Compiling and Executing a Program from the Command Line (cont'd.) • Execute the program Hello.exe Microsoft Visual C# 2010, Fourth Edition 86 Compiling and Executing a Program Using the Visual Studio IDE • Steps – Create a new project (console application) – Enter the project name – Write your program using the editor – To compile the program, click Build on the menu bar, and then click Build Solution • As an alternative, or you can press F6 – To Debug on the menu bar and then click Start Without Debugging Microsoft Visual C# 2010, Fourth Edition 87 Compiling and Executing a Program Using the Visual Studio IDE (cont'd.) Microsoft Visual C# 2010, Fourth Edition 88 Compiling and Executing a Program Using the Visual Studio IDE (cont'd.) Microsoft Visual C# 2010, Fourth Edition 89 Compiling and Executing a Program Using the Visual Studio IDE (cont'd.) Microsoft Visual C# 2010, Fourth Edition 90 Deciding Which Method to Use • Advantage of using the command line – Saves disk space • Advantages of using the Visual Studio IDE – Automatic sentence completion – Words are displayed using different colors based on their category – Code automatically generated by the IDE is very helpful when writing a GUI Microsoft Visual C# 2010, Fourth Edition 91 Adding Comments to a Program • Line comment example // Filename Hello.cs // Written by <your name> // Written on <today’s date> • Block comment example /* This program demonstrates the use of the WriteLine() method to print the message Hello, world! */ 92 Chapter Summary • Computing dates back some 5,000 years – Currently in 4th or 5th generation of computing • Physical components of the computer • System software versus application software • Steps in program development process – 1. Analyze the problem – 2. Design a solution – 3. Code the solution – 4. Implement the code – 5. Test and debug C# Programming: From Problem Analysis to Program Design 93 Chapter Summary (continued) • Programming methodologies – Structured procedural – Object-oriented • C# – – – – – – One of the .NET managed programming languages Object-oriented 2001 EMCA standardized Provides rapid GUI development of Visual Basic Provides number crunching power of C++ Provides large library of classes similar to Java C# Programming: From Problem Analysis to Program Design 94