Download presentation
Presentation is loading. Please wait.
Published byKenneth Flowers Modified over 9 years ago
1
Invitation to Computer Science 5 th Edition Chapter C# Programming in C#
2
Invitation to Computer Science, 5th Edition2 Objectives In this chapter, you will learn about: Creating and running a simple program in C# Virtual data storage Statement types An example of a complete program
3
Invitation to Computer Science, 5th Edition3 Objectives (continued) Managing complexity Object-oriented programming Graphical programming
4
Introduction to C# Figure 1 shows a simple but complete C# program Figure 2 shows the general form of a typical C# program Comments –Anything appearing on a line after the double slash symbol (//) is ignored by the compiler Prologue comment –Introductory comment that comes first Invitation to Computer Science, 5th Edition44
5
5 Figure 1 A Simple C# Program
6
Invitation to Computer Science, 5th Edition6 Figure 1 A Simple C# Program (continued)
7
Invitation to Computer Science, 5th Edition7 Figure 2 The Overall Form of a Typical C# Program
8
Invitation to Computer Science, 5th Edition8 Figure 3 The Program of Figure 1 (line numbers added for reference)
9
Introduction to C# (continued) Using directive –Refers to the System library Class header –Announces that a class is about to be defined + sign –The C# concatenation operator joins two strings together Free-format language –Does not matter where things are placed on a line Invitation to Computer Science, 5th Edition9
10
Creating and Running a C# Program Three-step process –First step: type the program into a text editor –Second step: the program must be compiled using a C# compiler for your computer –Third step: loads and executes the program file Integrated Development Environment –Lets the programmer perform a number of tasks within the shell of a single application program Invitation to Computer Science, 5th Edition10
11
Virtual Data Storage Assembly language –Does not require us to give the actual memory address of the storage location to be used for each item Identifiers –Names in a programming language –Cannot be one of the few reserved words C# is a case-sensitive language –Uppercase letters are distinguished from lowercase letters Invitation to Computer Science, 5th Edition11
12
Virtual Data Storage (continued) Constants –Quantities are fixed throughout the duration of the program –Values are known ahead of time Variables –Values that change as the program executes Variable declaration –Consists of a data type followed by a list of one or more identifiers of that type Invitation to Computer Science, 5th Edition12
13
Invitation to Computer Science, 5th Edition13 Figure 4 Some of the C# Primitive Data Types
14
Virtual Data Storage (continued) Array –Groups together a collection of memory locations, all storing data of the same type –Enables us to talk about an entire table of values or the individual elements making up that table Invitation to Computer Science, 5th Edition14
15
Invitation to Computer Science, 5th Edition15 Figure 5 A 12-Element Array hits
16
Invitation to Computer Science, 5th Edition16 Statement Types Input statement –Collects a value from the user for a variable within the program Output statement –Writes a message or the value of a program variable to the user’s screen Assignment statement –Assigns a value to a program variable
17
Invitation to Computer Science, 5th Edition17 Statement Types (continued) Control statements –Affect the order in which instructions are executed Flow of control in the program –The path through the program that is traced by following the currently executing statement
18
Invitation to Computer Science, 5th Edition18 Input/Output Statements String –Can be composed of items joined by the concatenation operator + Literal string –Enclosed in double quotes Format specifier –Used to control the appearance of numerical output Fixed-point format –Real number written in the form 11.34
19
Invitation to Computer Science, 5th Edition19 The Assignment Statement Pseudocode operation Set the value of “variable” to “arithmetic expression” –C# equivalent variable = expression; Basic arithmetic operations + Addition - Subtraction * Multiplication / Division
20
Invitation to Computer Science, 5th Edition20 Control Statements Control mechanisms –Sequential: instructions are executed in order –Conditional: which instruction executes next depends on some condition –Looping: a group of instructions may be executed many times Boolean condition –Can be either true or false –Often involves comparing the values of two expressions and determining whether they are equal
21
Invitation to Computer Science, 5th Edition21 Figure 6 Sequential Flow of Control
22
Invitation to Computer Science, 5th Edition22 Figure 7 C# Comparison Operators
23
Invitation to Computer Science, 5th Edition23 Figure 8 C# Boolean Operators
24
Invitation to Computer Science, 5th Edition24 Figure 9 Conditional Flow of Control (if-else)
25
Invitation to Computer Science, 5th Edition25 Figure 10 If-Else with Empty Else
26
Control Statements (continued) Compound statement –Can be used anywhere a single statement is allowed –Example { Console.WriteLine(“This is the first statement.”); Console.WriteLine(“This is the second statement.”); Console.WriteLine(“This is the third statement.”); } Invitation to Computer Science, 5th Edition26
27
Invitation to Computer Science, 5th Edition27 Figure 11 The TravelPlanner Program with a Conditional Statement
28
Invitation to Computer Science, 5th Edition28 Figure 12 while Loop
29
Control Statements (continued) Initialization of variables –Using assignment statements to set the values of certain variables before they are used by the program Sentinel value –One extra integer that is not part of the legitimate data but is instead a signal that there are no more data Infinite loop –The condition, once true, would remain true forever, and the loop body would be endlessly executed Invitation to Computer Science, 5th Edition29
30
Invitation to Computer Science, 5th Edition30 Figure 13 The TravelPlanner Program with Looping
31
Invitation to Computer Science, 5th Edition31 Figure 13 The TravelPlanner Program with Looping (continued)
32
Another Example Example –Write a program to assist SportsWorld, a company that installs circular swimming pools To estimate costs for swimming pool covers or for fencing –SportsWorld needs to know the area or circumference of a pool, given its radius Invitation to Computer Science, 5th Edition32
33
Invitation to Computer Science, 5th Edition33 Figure 14 A Pseudocode Version of the SportsWorld Program
34
Invitation to Computer Science, 5th Edition34 Figure 15 The SportsWorld Program
35
Invitation to Computer Science, 5th Edition35 Figure 15 The SportsWorld Program (continued)
36
Invitation to Computer Science, 5th Edition36 Figure 16 A Sample Session Using the Program of Figure 15
37
Managing Complexity Divide and conquer –A problem-solving approach and not just a computer programming technique Figure 17(a) –An example of a structure chart or structure diagram Invitation to Computer Science, 5th Edition37
38
Invitation to Computer Science, 5th Edition38 Figure 17 Structure Charts
39
Using Functions Functions –Each function in a program should do one and only one subtask Argument list –List of identifiers for variables pertinent to that function Invitation to Computer Science, 5th Edition39
40
Invitation to Computer Science, 5th Edition40 Figure 18 Structure Chart for the SportsWorld Task
41
Invitation to Computer Science, 5th Edition41 Figure 19 A High-Level Modular View of the SportsWorld Program
42
Invitation to Computer Science, 5th Edition42 Figure 20 The Main Function in a Modularized Version of the SportsWorld Program
43
Writing Functions Any function can invoke another function A function can invoke itself The function header consists of three parts –A return indicator –The function identifier –A parameter list Invitation to Computer Science, 5th Edition43
44
Invitation to Computer Science, 5th Edition44 Figure 21 The Outline for a C# Function
45
Writing Functions (continued) Return indicator –Classifies a function as a “void” or a “nonvoid” function Function identifier –Can be any legitimate C# identifier Argument is passed by value –If the value is one that the function must know to do its job but should not change Invitation to Computer Science, 5th Edition45
46
Writing Functions (continued) Argument is passed by reference –If value passed to the function is one that the function should change, and the main function should know the new value By default –Arguments in C# are passed by value, which protects them from change by the function getInput function –Both radius and taskToDo are values that getInput obtains from the user Invitation to Computer Science, 5th Edition46
47
Invitation to Computer Science, 5th Edition47 Figure 22 The getInput Function
48
Invitation to Computer Science, 5th Edition48 Figure 23 The doCircumference Function
49
Invitation to Computer Science, 5th Edition49 Figure 24 The Complete Modularized SportsWorld Program
50
Invitation to Computer Science, 5th Edition50 Figure 24 The Complete Modularized SportsWorld Program (continued)
51
Invitation to Computer Science, 5th Edition51 Figure 25 The SportsWorld Program Using Nonvoid Functions
52
Invitation to Computer Science, 5th Edition52 Figure 25 The SportsWorld Program Using Nonvoid Functions (continued)
53
Invitation to Computer Science, 5th Edition53 Figure 26 Some C# Terminology
54
Object-Oriented Programming A program is considered a simulation of some part of the world that is the domain of interest –“Objects” populate this domain When an object-oriented program is executed –The program generates requests for services that go to the various objects Terms associated with object-oriented programming –Encapsulation –Inheritance –Polymorphism Invitation to Computer Science, 5th Edition54
55
Invitation to Computer Science, 5th Edition55 Figure 27 Three Key Elements of OOP
56
C# and OOP Member variables –Properties of a class Member functions –Services that any object of the class can perform Objects –Instances of classes Invitation to Computer Science, 5th Edition56
57
Invitation to Computer Science, 5th Edition57 Figure 28 An Object-Oriented SportsWorld Program
58
Invitation to Computer Science, 5th Edition58 Figure 28 An Object- Oriented SportsWorld Program (continued)
59
Invitation to Computer Science, 5th Edition59 Figure 28 An Object- Oriented SportsWorld Program (continued)
60
One More Example In Figure 29 –The Circle object has a radius property –The Rectangle object has a width property and a height property –Any Circle object can set the value of its radius and can compute its area –A Square object has a side property –The Square2 object doesn’t have any properties or any way to compute its area Invitation to Computer Science, 5th Edition60
61
Invitation to Computer Science, 5th Edition61 Figure 29 A C# Program with Polymorphism and Inheritance
62
Invitation to Computer Science, 5th Edition62 Figure 29 A C# Program with Polymorphism and Inheritance (continued)
63
Invitation to Computer Science, 5th Edition63 Figure 29 A C# Program with Polymorphism and Inheritance (continued)
64
Invitation to Computer Science, 5th Edition64 Figure 30 Output from the Program of Figure 29 Figure 29
65
One More Example (continued) Square –A stand-alone class with a side property and a doArea function Square2 class –Recognizes the fact that squares are special kinds of rectangles –Subclass of the Rectangle class –Inherits the width and height properties from the “parent” Rectangle class Invitation to Computer Science, 5th Edition65
66
What Have We Gained? Reasons why OOP is a popular way to program –Software reuse –A more natural “worldview” Software reuse –Useful class that has been implemented and tested becomes a component available for use in future software development Invitation to Computer Science, 5th Edition66
67
Invitation to Computer Science, 5th Edition67 Figure 31 A Hierarchy of Geometric Classes
68
A More “Natural” Worldview Object-oriented programming –Recognizes that in the “real world,” tasks are done by entities (objects) –Allows the programmer to come closer to modeling or simulating the world as we see it Object-oriented program design –Begins by identifying objects that are important in the domain of the program Invitation to Computer Science, 5th Edition68
69
Graphical Programming Graphics –Make it easier to manage tasks of the operating system –Can help us visualize and make sense of massive amounts of output produced by programs that model complex physical, social, and mathematical systems Invitation to Computer Science, 5th Edition69
70
Graphics Hardware Bitmapped display –Screen is made up of thousands of individual picture elements, or pixels, laid out in a two-dimensional grid High-resolution terminals –Terminals with a high density of pixels Frame buffer –Memory that stores the actual screen image Invitation to Computer Science, 5th Edition70
71
Invitation to Computer Science, 5th Edition71 Figure 32 Pixel-Numbering System in a Bitmapped Display
72
Invitation to Computer Science, 5th Edition72 Figure 33 Display of Information on the Hardware Terminal
73
Graphics Software Graphics library –Collection of functions Graphics functions at our disposal –DrawLine(Pen, x1, y1, x2, y2) –DrawRectangle(Pen, x, y, width, height) –DrawEllipse(Pen, x, y, width, height) –DrawString(string, font, brush, x, y) Invitation to Computer Science, 5th Edition73
74
Summary In a high-level language, the programmer: –Need not manage the storage or movement of data values in memory –Can think about the problem at a higher level –Can use program instructions that are both more powerful and more natural language-like –Can write a program that is much more portable among various hardware platforms Invitation to Computer Science, 5th Edition74
75
Summary (continued) Modularization, through the use of functions and parameters –Allows the program to be more cleanly structured Object-oriented programming –Allows a more intuitive view of the problem solution –Provides the possibility for reuse of helpful classes Invitation to Computer Science, 5th Edition75
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.