Introductory Programming in C# Release 1.0
Total Page:16
File Type:pdf, Size:1020Kb
Introductory Programming in C# Release 1.0 Andrew N. Harrington and George K. Thiruvathukal May 29, 2012 CONTENTS 1 Context 1 1.1 Introduction to the Notes.........................................1 1.2 Comments on Miles Chapter 1......................................1 2 C# Data and Operations 3 2.1 Development Tools............................................3 2.2 Lab Exercise: Editing, Compiling, and Running............................5 2.3 Comments on Miles Simple Data....................................9 2.4 Division and Remainders.........................................9 2.5 Substitutions in Console.WriteLine................................... 10 2.6 Learning to Solve Problems....................................... 12 2.7 Lab: Division Sentences......................................... 15 2.8 Homework: Grade Calculation...................................... 18 3 Defining Functions of your Own 23 3.1 Syntax Template Typography...................................... 23 3.2 A First Function Definition....................................... 23 3.3 Multiple Function Definitions...................................... 25 3.4 Function Parameters........................................... 26 3.5 Multiple Function Parameters...................................... 29 3.6 Returned Function Values........................................ 30 3.7 Two Roles: Writer and Consumer of Functions............................. 32 3.8 Local Scope............................................... 33 3.9 Static Variables.............................................. 34 3.10 Not using Return Values......................................... 34 4 Basic String Operations 37 4.1 String Indexing.............................................. 37 4.2 Some Instance Methods and the Length Property............................ 37 4.3 A Creative Problem Solution....................................... 39 4.4 Lab: String Operations.......................................... 41 5 Decisions 43 5.1 Conditions I............................................... 43 5.2 Simple if Statements.......................................... 43 5.3 if-else Statements........................................... 44 5.4 More Conditional Expressions...................................... 46 5.5 Multiple Tests and if-else Statements................................ 48 5.6 If-statement Pitfalls........................................... 50 i 5.7 Compound Boolean Expressions..................................... 52 6 While Loops 55 6.1 While-Statements............................................ 55 6.2 While-Statements with Sequences.................................... 62 6.3 Interactive while Loops........................................ 67 6.4 Short-Circuiting && and ||........................................ 71 6.5 While Examples............................................. 72 6.6 More String Methods........................................... 73 6.7 Algorithms using While......................................... 75 6.8 Do-While Loops............................................. 78 6.9 Number Guessing Game Lab...................................... 79 6.10 Homework: Grade Calculation from Individual Scores......................... 83 6.11 Lab: Using MonoDevelop........................................ 86 6.12 Lab: Version Control........................................... 92 7 Foreach Loops 107 7.1 foreach Syntax............................................ 107 7.2 foreach Examples............................................ 108 8 For Loops 109 8.1 For-Statement Syntax.......................................... 109 8.2 Examples With for Statements..................................... 110 9 Files 119 9.1 File Syntax................................................ 119 9.2 Grade File Homework.......................................... 122 10 Arrays 127 10.1 One Dimensional Arrays......................................... 127 10.2 Musical Scales and Arrays........................................ 133 10.3 Linear Searching............................................. 136 10.4 Sorting Algorithms............................................ 137 10.5 Binary Searching............................................. 145 10.6 Lab: Arrays................................................ 147 10.7 Lab: Performance............................................ 151 10.8 Multi-dimensional Arrays........................................ 153 11 Lists 159 11.1 List Syntax................................................ 159 11.2 .Net Library (API)............................................ 161 12 Dictionaries 163 12.1 Dictionary Syntax............................................ 163 12.2 Dictionary Efficiency........................................... 164 12.3 Dictionary Examples........................................... 165 12.4 Lab: File Data and Collections...................................... 166 13 Classes 171 13.1 A First Class Example: Rational..................................... 171 13.2 Classes And Structs........................................... 177 13.3 Class Examples.............................................. 178 13.4 Book List Assignment.......................................... 181 13.5 Mercurial and Teamwork......................................... 185 ii 14 Interfaces 189 14.1 Fractions Revisited............................................ 189 14.2 CSProj Revisited............................................. 190 14.3 Group Game Project........................................... 194 15 Testing 201 15.1 Assertions................................................ 201 15.2 Attributes................................................. 201 15.3 Testing the Constructor.......................................... 202 15.4 Testing Rational Comparisons...................................... 203 15.5 Testing Rational Arithmetic....................................... 203 15.6 Testing Rational Conversions (to other types).............................. 204 15.7 Testing the Parsing Feature........................................ 204 15.8 Running the Tests............................................ 205 16 Acknowledgements 207 17 Change Log 209 18 TODO’s 211 Index 213 iii iv CHAPTER ONE CONTEXT 1.1 Introduction to the Notes These notes are designed for Comp 170. They are closely tied to the excellent C# introduction in Rob Miles’ free online C# Yellow Book. These notes will follow mostly the same order, adding some extra explanations, examples, and challenges, and intro- duce the Mono open source implementation of C#. The content here will be interspersed with comments about where to look at parts of Miles book, with clarifications of the book and comments about what is not important for a beginner in the book. Computer programs are designed to run on a computer and solve problems. Though the initial problems will be tiny and often silly, they will serve as learning tools to prepare for substantive problems. 1.2 Comments on Miles Chapter 1 Miles Chapter 1 on Computers and Programs makes a good introduction and gives context. Read for the grounding it gives. The chapter makes some reference to Microsoft, the original creator of C#, and its Visual Studio software development environment, which works only on Windows machines, and costs a lot if you are not a student. The optional text by Lewis discussed Visual Studio at some length. The next section of these notes will introduce an alternative to the Microsoft environment: Mono, which is free, open-source software that makes C# available for multiple platforms: Windows, Mac, or Linux machines. 1 Introductory Programming in C#, Release 1.0 2 Chapter 1. Context CHAPTER TWO C# DATA AND OPERATIONS 2.1 Development Tools 2.1.1 About Software Development Kits (SDKs) A software development kit (SDK) is a set of tools for developing in a particular programming language (in our class, C#). Developing in a language means everything from compiling to running and (when things go wrong) to debugging programs. The Microsoft SDK is the proprietary implementation of .Net. It runs only on Windows and is the primary development framework for all things Microsoft. The Mono Project SDK <http://mono-project.com> is the free/open source equivalent implementation of the Microsoft SDK. It runs on all major platforms (including Windows) and is needed in situations where you want to develop .Net applications on non-Windows platforms. As an interesting aside, the company whose developers lead the work on the Mono SDK are working on commercial tools that allow you to develop/run applications written in .Net on Apple iOS and Android mobile devices (phones and tablets). 2.1.2 About Integrated Development Environments (IDE) While just about everything you need to create programs can, indeed, be found in the SDK, it is not long before you wish there were an “app for that” so to speak. While most programmers who developed code (like your instructors) in the 1970s-1990s learned to program directly with the SDK using the command line, today’s programmers largely to prefer working in an IDE. There are two major IDEs for .Net development, which we explain briefly below: • Visual Studio is the Microsoft IDE that interfaces directly to the Microsoft SDK. • MonoDevelop is the free/open source IDE for developing applications using the Mono SDK on Windows and all other platforms (in particular, Linux and OS X). In addition, there is another Windows-specific IDE, SharpDevelop, that inspired the creation of MonoDevelop. It is still actively maintained and provides a somewhat “lighter weight” alternative to Visual Studio for Windows