Goals for this Lab

  • Familiarize you with the C# development cycle
  • Familiarize you with C# constructs

Look at the module for the book reading and questions on Gradescope.

Overview

If you have never programmed in C#, then this overview is for you. Programming in C# has several differences from Racket, Java, C, and other languages. My assumption is that you have a working knowledge of C. It is similar to C, except that it is object oriented (like Java) and has different ways of doing things like printing to the screen, reading in files, and operating on strings and booleans.

Like C, you can choose your favorite code editor for writing code, then you can separately compile and run your code.

In week 4, we will start with Unity. In weeks 1-3, we will program in C# separate from Unity to familiarize you with the C# language itself and object-oriented concepts before diving into Unity. This corresponds to, in the textbook, completing Section 1 (chapters 1-5) outside of Unity and then moving to Unity in Section 2 (chapter 6+).

When type your program, you type it into a file with a file extension .cs and edit it there. The file must be a plain text file with just the letters and carriage returns. Every time you make changes, make sure you save the changes before moving on compilation. All files need to end with the extension .cs. C# has a much more rigid way of dividing code into files and naming files.

The next step is to compile the code into an executable that the computer can run. The compiler we'll be using is mcs. If you compile the file Happy.cs, it will produce an executable Happy.exe.

If your program has multiple files (which it will), then the executable name corresponds to the file that has the Main method in it.

Finally, you can run the program. You do so by typing "./Happy.exe"

Getting Started: Example Code

During lab, in order to practice editing and compiling, we are providing you a complete program, split into three important files. Go through each file, with the associated explanation, to get ready for lab.

Begin by reading through TestLab1.cs, Lab1SampleFunctions.cs, and Puppy.cs.

This code has been chosen especially to introduce you to not only the syntax and semantics of C#, but also norms we will expect in this class. Take note of:

  • The types in the input parameters and return values
  • What to do when erroneous input is received
  • Explanatory comment for each function
  • Inline comments for the useful function
  • Proper indentation
  • Examples of printing out with different types and different configurations
  • Separation of main / testing code from the lab code
  • Using command-line parameters to allow flexible tests
  • Packaging code into classes and using classes



To compile the code, run: mcs TestLab1.cs Lab1SampleFunctions.cs Puppy.cs
This will produce an executable named TestLab1.exe. How to run the code is described below in Testing Methodology.
Note: if you want to work on a local machine, you may need to install a C# compiler first. Take a look at the C# compiler instructions in the Resources section to get instructions for installing a compiler, compiling your files, and running them.

Working on CSIL computers from home

You may find that you want to log into CSIL computers from home. There are two skillsets you need: vi and unix.

You first need to log in using some sort of a client. If you are on a Mac, you can open up a terminal and type, ssh ucid@linx.cs.uchicago.edu Then enter your password, and you're in!!! You can transfer a file from your computer to your campus computer using scp filename ucid@linux.cs.uchicago.edu:~/cs209/filename (you need to have navigated through the directories to your file on your local comptuer before doing this). You may need to be on a campus computer (dorm) or logged in with the VPN in order to use scp or ssh.

You'll want to create a directory
mkdir cs209
Make sure that directory is secure and won't allow anyone else to access it.
chmod 700 cs209
Then you'll want to go into that directory
cd cs209
Now you can do your work in here.
Here is a little unix tutorial I wrote for beginners.

To learn the basics of vi. You can use vimtutor, which is built into the unix machine. Just type vimtutor into the terminal when you're logged in to the campus computer. In just 10 minutes, you can become minimally proficient! You can chose whether you want to do your main development at home or logged in remotely, but at least this will allow you to choose. For more, here is a vi tutorial and a vi command cheatsheet.

Testing Methodology

We'll illustrate those three principles below. Imagine you have finished one method of a class (or several) that you now want to test. This is how we'll do it. There are a few general principles we will follow for testing.
1. Place your main in a separate file from your implementation so that you can swap out the main easily.
2. Create one test function for each function you're testing that takes in a single test case and tests it.
3. Create a set of test cases for each function that exercise the code well.


Principle 1: Place the main in a separate file.

Testing your code requires a Main method because all C# programs begin in main. We are going to place this Main function in a different file (and thus class) so that we can easily swap out what code is calling this function. The Main function has been placed in TestLab1.cs.


Principle 2. Create one test function for each function you're testing that takes in a single test case and tests it.

Inside TestLab1.cs, you will notice that the Main method is last, and above it is a single method for each method we are testing. It takes the command-line arguments as the inputs. Inside the method, it checks the number of arguments, converts them to the proper format, calls the method, and, if there is a return value, checks the return value.

If you look at main, you'll see that it looks at the first command-line argument and uses that to determine which method to test (and call the appropriate test method). In C#, unlike C, the executable name is not passed in to the program itself. The first number (at index 0) tells what test to run (PrintStuff vs ReturnAValue, etc.). That is followed by the inputs to give the method being tested. Finally, if the method has a return value, the last argument is the expected value so it can be compared to what the method returns.



Use the following command lines to test each method:
  • function: public void PrintStuff(int ival, float fval, char cval, string sval)
    How to run it: ./TestLab1.exe 0 5 6.3 c howdy
  • function: public int ReturnAValue(int ival)
    How to run it: ./TestLab1.exe 1 5 25 (5 is the input, 25 is the expected output)
  • function: public uint Factorial(uint n)
    How to run it: ./TestLab1.exe 2 3 6 (3 is the input, 6 is the expected output)


Principle 3. Create a set of test cases for each function that exercise the code well. Put that set of test cases into a file.

The last piece is to design and implement test cases. These functions are very simple, so no sophisticated test design is required. We test very small, degenerate cases (like adding or multiplying by 0), small cases that actually show the operation, and larger cases to verify that it works on two-digit or large numbers. In this testing methodology, the test cases can be placed in a separate file. I've placed mine in testlab1.txt Give that file executable priveleges: chmod +x testlab1.txt Now, on the command line, you can type: ./testlab1.txt and it will automatically go through all of the test cases you wrote. As you implement more methods, you can add more tests to your file. Each time you want to verify something is working, you can run that file, and it runs all previous tests plus your new ones!

Week 1 Warmup Assignment

Now that you are familiar with how to code, compile, and run C# programs with rigorous testing methods, you are ready to start diving in and programming!

For this warm-up, you'll implement a single method in Lab1SampleFunctions.cs and its testing code in TestLab1.cs:
public uint DogYears(Puppy p1)

This calculates a dog's age in "dog years." So if a dog is 5 years old, it's actually 35 years old in "dog years" because dogs age 7x faster than humans(apologies to all dog lovers - I know this is inaccurate. We're just having a little fun).

The skeleton is already in the file - you need only fill in the code. Then you will need to go into TestLab1.cs and implement a test function for it, following the convention created above. The code to test that function will be 4, and it will accept a single input parameter of the age of the puppy.