Look at the module for the book reading and questions on Gradescope.
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"
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:
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.
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.
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.
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!