0Pricing
C# Academy · Lesson

Implementation & tests

Implement Rectangle, Circle, and Triangle; add basic input guards; write tiny ad-hoc tests without a framework.

Implementation & tests is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Implementation plan

Goal: Implement shapes and verify them.

  • Implement Rectangle, Circle, Triangle
  • Guard invalid inputs
  • Add a tiny Assert helper
  • Run quick checks in Main

Rectangle & Circle

Add simple guards in constructors so dimensions are positive. Keep formulas small and clear.

using System;

public interface IShape
{
  double Area();
  double Perimeter();
}

public sealed class Rectangle : IShape
{
  private readonly double _w, _h;

  public Rectangle(double w, double h)
  {
    if (w <= 0 || h <= 0) throw new ArgumentOutOfRangeException("w/h must be > 0");
    _w = w; _h = h;
  }

  public double Area() { return _w * _h; }
  public double Perimeter() { return 2 * (_w + _h); }
}

public sealed class Circle : IShape
{
  private readonly double _r;

  public Circle(double r)
  {
    if (r <= 0) throw new ArgumentOutOfRangeException("r must be > 0");
    _r = r;
  }

  public double Area() { return 3.14159 * _r * _r; }
  public double Perimeter() { return 2 * 3.14159 * _r; }
}

public class Program
{
  public static void Main(string[] args)
  {
    IShape a = new Rectangle(3, 4);
    IShape b = new Circle(2);
    Console.WriteLine("Rect A=" + a.Area() + " P=" + a.Perimeter());
    Console.WriteLine("Circ A=" + b.Area() + " P=" + b.Perimeter());
  }
}

Triangle with guards

Validate sides (triangle inequality). Use a well-known formula and keep numbers readable.

using System;

public interface IShape
{
  double Area();
  double Perimeter();
}

public sealed class Triangle : IShape
{
  private readonly double _a, _b, _c;

  public Triangle(double a, double b, double c)
  {
    if (a <= 0 || b <= 0 || c <= 0) throw new ArgumentOutOfRangeException("sides must be > 0");
    // triangle inequality: each side < sum of other two
    if (a >= b + c || b >= a + c || c >= a + b) throw new ArgumentException("invalid triangle");
    _a = a; _b = b; _c = c;
  }

  public double Perimeter() { return _a + _b + _c; }

  // Herons formula for area
  public double Area()
  {
    double s = (_a + _b + _c) / 2.0;
		double value = s * (s - _a) * (s - _b) * (s - _c);
		return Math.Sqrt(value);
		}
}

public class Program
{
  public static void Main(string[] args)
  {
    IShape t = new Triangle(3, 4, 5);
		Console.WriteLine("Tri A=" + t.Area() + " P=" + t.Perimeter());
		}
}

Ad-hoc Assert helper

Create a tiny Assert class. Compare expected vs actual with a tolerance for doubles.

using System;

		public static class Assert
			{
			// Approx compare for doubles (tolerance)
										  public static void AreClose(double expected, double actual, double eps, string msg)
  {
		if (Math.Abs(expected - actual) > eps)
			{
			throw new Exception("Assert failed: " + msg + " expected=" + expected + " actual=" + actual);
    }
    Console.WriteLine("OK: " + msg);
  }
}

public interface IShape
{
  double Area();
  double Perimeter();
}

public sealed class Rectangle : IShape
{
  private readonly double _w, _h;
  public Rectangle(double w, double h) { if (w <= 0 || h <= 0) throw new ArgumentOutOfRangeException(); _w = w; _h = h; }
  public double Area() { return _w * _h; }
  public double Perimeter() { return 2 * (_w + _h); }
}

public class Program
{
  public static void Main(string[] args)
  {
    Rectangle r = new Rectangle(3, 4);
    Assert.AreClose(12.0, r.Area(), 0.0001, "rectangle area");
    Assert.AreClose(14.0, r.Perimeter(), 0.0001, "rectangle perimeter");
  }
}

Run quick tests

Bundle a few tests in Main. Keep expectations simple and readable.

using System;

public static class Assert
{
  public static void AreClose(double expected, double actual, double eps, string msg)
  {
    if (Math.Abs(expected - actual) > eps)
      throw new Exception("Assert failed: " + msg + " expected=" + expected + " actual=" + actual);
    Console.WriteLine("OK: " + msg);
  }
}

public interface IShape
{
  double Area();
  double Perimeter();
}

public sealed class Circle : IShape
{
  private readonly double _r;
  public Circle(double r) { if (r <= 0) throw new ArgumentOutOfRangeException(); _r = r; }
  public double Area() { return 3.14159 * _r * _r; }
  public double Perimeter() { return 2 * 3.14159 * _r; }
}

public sealed class Triangle : IShape
{
  private readonly double _a, _b, _c;
  public Triangle(double a, double b, double c)
  {
    if (a <= 0 || b <= 0 || c <= 0) throw new ArgumentOutOfRangeException();
    if (a >= b + c || b >= a + c || c >= a + b) throw new ArgumentException("invalid triangle");
    _a = a; _b = b; _c = c;
  }
  public double Perimeter() { return _a + _b + _c; }
  public double Area()
  {
    double s = (_a + _b + _c) / 2.0;
    return Math.Sqrt(s * (s - _a) * (s - _b) * (s - _c));
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    double eps = 0.001;

    Circle c = new Circle(2);
    Assert.AreClose(12.56636, c.Area(), eps, "circle area r=2");
    Assert.AreClose(12.56636, c.Perimeter(), eps, "circle peri r=2");

    Triangle t = new Triangle(3, 4, 5);
    Assert.AreClose(12.0, t.Perimeter(), eps, "triangle perimeter 3-4-5");
    Assert.AreClose(6.0, t.Area(), eps, "triangle area 3-4-5");
  }
}

Implementation tips

Checklist:

  • Validate inputs in constructors.
  • Keep formulas short; use local constants for pi.
  • Create a tiny Assert for ad-hoc tests.
  • Test both Area and Perimeter for each shape.

Ad-hoc testing idea

Quick check: What is a simple way to test a small library without a test framework?

Recap

Recap: Implement shapes with clear guards, then verify with a minimal Assert helper in Main. Keep tests small and deterministic.

Frequently asked questions

Is the “Implementation & tests” lesson free?

Yes — the full text of “Implementation & tests” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Implementation & tests”?

Implement Rectangle, Circle, and Triangle; add basic input guards; write tiny ad-hoc tests without a framework. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Implementation & tests” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Design (interfaces + inheritance)
  2. Implementation & tests
  3. Polymorphic behaviors
← Back to C# Academy