CS 350 - Lab 10 "Introduction to C#"

resources:
MSDN .Net
System.Console:Members
sample code
getting started
sample Makefile

Before starting this lab you should read and understand the code in sample1.cs through sample6.cs (you don't need sample7.cs on event handling for this lab). Copy the code into your working directory:

    $ cp /home/fac/melissa/public_html/cs350-f15/Code/C_sharp/* .

In particular, you need to understand the concepts of properties and interfaces in C#. Properties are like short-cut get and set methods in C++. Interfaces are like C++ abstract classes. Recall that an abstract class is one with at least one pure virtual function and, because of this, can never be instantiated. In C# - an abstract class is formalized and denoted by the keyword 'interface.' In an interface, all methods must be implemented in the derived class (i.e., all pure virtual methods). Start with the code below and modify it. The code will not compile until you make the additions as listed below.

 $ cp /home/fac/melissa/public_html/cs350-f15/examples/week10/lab10.cs .
 $ cp /home/fac/melissa/public_html/cs350-f15/examples/week10/Makefile .

Make these additions to lab10.cs. You do not need to change any of the existing code. After making the necessary changes, your code should compile. Put all your code in the Assignment namespace and in the same file. Follow this instructions:

STEP 1. Note the virtual method

      void Display();
 
in the IShape interface. Implement Display() to write all attributes particular to each IShape object. For example, for a rectangle object you should display this:

    I'm a rectangle! I have width 30 and height 40.

STEP 2. Note the three public derived classes: Rectangle, Triangle, and Circle that inherit from the IShape interface. Rectangle has a height and width, Triangle has a base and height, and Circle has a radius. C# has a keyword 'base' so you cannot use that as a variable name. In each class you must implement the IShape.Area property's implicit Get{} method. Get{} will calculate and return the respective area of the IShape type.

STEP 3. Add a Radius property to Circle to both set and get the radius value. Test your property with

      shape3.Radius = 3.5f;
      Console.WriteLine("I just set my Radius to '{0}'.", shape3.Radius);

STEP 4. The code in Main should now execute to test everything. You do not need to change Main. Your output should look like this:


My Radius is '3.5'	: 
area of type 'Assignment.Rectangle'	: 179.375
area of type 'Assignment.Triangle'	: 33.6875
area of type 'Assignment.Circle'	: 38.465
I'm a rectangle!I have width 8.75 and height 20.5
I'm a triangle!	I have base 5.5 and height 12.25
I'm a Circle!	I have radius 3.5

HOW YOUR LAB WILL BE GRADED

Your code and Makefile must reside here:

  /home/stu/{username}/cs350/lab10/lab10.cs
  /home/stu/{username}/cs350/lab10/Makefile

Your work will be copied, compiled and executed by my script after the deadline.