C# Programming


Location: Room 108, 德田館
Time: 1900 ~ 2200

``Just because people tell you it can't be done,
that doesn't necessarily mean that it can't be done.
It just means that they can't do it.''
-- Anders Hejlsberg

``It's not a bug - it's an undocumented feature.''
-- Anonymous

Instructor Information

Software Installation & WiFi Access

Make-up Class Method Students are advised to utilize the website NTU COOL for class compensation. Please note, the required login credentials are those associated with your individual training class account.

Recording Classroom Lectures Policy Recording of classroom lectures is prohibited unless advance written permission is obtained from the class instructor and any guest presenter(s).

Objectives

Syllabus

Algorithm-based programming

Object-oriented programming

Advanced topics

Schedule [ 328, 330, 335, 341, 351, 355, 361, 369, 386, 391, 397, 400, 405, 407 ]

Date Summary
2024.4.9
2024.4.12
  • Data types, variables, and operators
    • Variable declaration (with memory address)
    • Primitive types: int, double, char / string, bool
    • Operators: assignment operator (=), arithmetic operators (+, −, *, /, %), relational operators (>, >=, <, <=, ==, !=), logical operators (!, &&, ||, ∧)
    • Type conversion (casting)
2024.4.16 (2 hours)
  • Data types, variables, and operators (cont'd)
    • Operators: compound arithmetic operators (+=, ++)
    • Misc: console input, parsers, string format
  • Flow controls
    • Selections (branching): if-else, switch-case-default-break, ?: operator
    • Digression: pseudorandom number generator (PRNG)
    • Repetitions: while loop
2024.4.19
  • Flow controls (cont'd)
    • Repetitions: do-while loop, for loop
    • Jump statements
    • Nested loops
    • Case study: Monte Carlo
    • (FYR) Analysis of algorithms: big O
  • Homework 1
2024.4.23 (3.5 hours)
  • Data structures
    • Arrays: syntax & memory allocation
    • Examples of processing arrays
    • Foreach loops
    • Cloning arrays (shallow copy?)
    • Random permutation (shuffling)
    • Some algorithms: sorting, searching (especially binary search)
    • Multidimensional arrays and jagged arrays
    • A glimpse on linked list
    • Generics and more data structures: list, stack, dictionary
  • Homework 2
2024.4.26 (3.5 hours)
  • Methods
    • Method definition & implementation
    • Call stack & variable scope (again, with memory model)
    • Pass by reference: ref, in, out
    • Implicitly typed local variables: var
    • Method overloading
2024.4.30
  • Methods (cont'd)
    • Variable-length arguments: param
    • Optional arguments
    • The Main method with command-line arguments
  • Object-oriented programming
    • Class & object
    • Encapsulation: private fields + public methods = public properties; auto-implemented properties
    • Constructors
  • Homework 3
2024.5.3
  • Object-oriented programming (cont'd)
    • Garbage collection (GC)
    • Unified modeling language (UML): class diagram
    • Self-reference: the this operator
    • HAS-A relationship: association, aggregation, composition
    • Instance members vs. static members
    • Briefing design patterns: Singleton Pattern
    • First IS-A relationships: class inheritance
    • Constructor chaining with the base operator
    • Method overriding: virtual & override
  • Homework 4
2024.5.7
  • Object-oriented programming (cont'd)
    • Subtype polymorphism: Liskov substitution principle, up / down casting, and the ``as'' operator
    • Runtime type testing: the ``is'' operator
    • Abstract method & class
    • Sealed method & class
    • Second IS-A relationships: interface inheritance
2024.5.10
Promotion

Sample code

Gradebook

Programming Labs

Late Homework Policy All programming labs should be submitted before the last date of class in order to deliver your final grades to the office so that the certificates can be issued soon. (See a recent complaint letter here.)

Homework Submission Send the source code (the file with .cs extension) to my email shown in the beginning of this page. Remember to indicate this class number, your full Chinese name, and homework number in your email title. For example, [CSharp 407] Homework 1 盧政良. Do not attach any class file because any executable file will be ruled out by Gmail.

Lab 1 Number-Guessing Game

Write a program for number-guessing game (you may refer to Guess the Number if you have never played this game). The program first generates a secret number ranging between 0 and 99, inclusive. Then the program asks the player to guess a number. If the input number is equal to the secret number, then the player wins. If not, then update the range depending on the input accordingly. (For example, assume that the secret number is 42. If the player types 50 for the first time, then the program shows (0, 49) on the screen.) When there is only one integer left, the player loses the game. Also, make sure that the player types a number in the feasible range; otherwise, ask the player to redo the input.

Lab 1-1 (Optional)

Add some strategies (AI?) to play this game and calculate each winning rate by using these strategies for 1e5 times. For example, the naive strategy is to guess a number in random, and its result is around 66%. Surprisingly, the winning rate for binary search is about 63%, not as expected to beat the naive one. Why?

Lab 1-2 (Optional)

Find the optimal strategy to beat the former two. The result is shown below. As you could see, the winning rate of my optimal strategy reaches 99%!

Lab 1-3 (Optional)

Modify the game loop which allows the player to guess at most 7 times (why?). Also report their performance like below:

The performance of binary search remains ~63% while the other two degrade severely!

Lab 2 Contagion Control

The virus, COVID-19 (aka Wuhan coronavirus), is primarily spread from close contact with infected people. You task is to design an algorithm to identify the chain of virus transmission from one person to the next. For simplicity, let N be the number of citizens, each denoted by an integer ID from 0 to N − 1. Each citizen is asked to keep a record of the ID of the person they comes in contact with. It is assumed that no two citizens keep the same ID of the citizen in close contact. To avoid typing numbers by hand, I suggust using the shuffling algorithm to generate a random sequence of 0, 1, 2, ..., N − 1 for testing. For example, consider N = 16 and assume that the citizen with ID 0 is initially infected. Your program is to identify and list all IDs along this infection chain. A possible output is shown below.

Lab 3 Rearrange Matrix by Sorting First Column

Let M be a 2D matrix whose number of ``rows'' and ``cols'' are integers specified by user input from the keyboard. Write a program for the following tasks: (1) generate integers from 0 to rows * cols − 1 and fill up a 2D array (more explicitly, created by calling new int[rows, cols]) with these integers in a random order (you could use the shuffle algorithm here), (2) sort M by performing row exchanges based on the numbers of the first columns in ascending order, and (3) output the resulting matrix. Note that you need to encapsulate some code snippets into functions / methods as needed.

Lab 3-1 (Optional)

Redo Lab 3 but using List<List<int>> instead of native arrays for M.

Lab 4 Refactoring Number-Guessing Game by Using Design Patterns

Rewrite your program of Lab 1 in OOP style. First analyze the functionalities of Lab 1 and then define at least two classes for each functionality. For instance, you could create two objects: one object to manage the game procedure, the other for the player who generates an integer within feasible range during the game. To achieve this, you will need to define a protocol, similar to the relationship between the Console.WriteLine method and the ToString method of any C# object. Hence the game will have minimal dependency on the player (the game won't need to know which kind of player during the game procudure). A possible class design in UML is shown below.

Lab 5 Simple Calculator by Windows Presentation Foundation (WPF)

Use WPF to implement a simple calculator illustrated in the figure below.

References

C# Programming Language

Design Patterns

.Net Framework / Core

Windows Presentation Foundation (WPF)

ASP.Net