実装とテスト
Rectangle、Circle、Triangleを実装し、基本的な入力ガードを追加します。フレームワークを使わず、小さなアドホックテストを書きます。
「実装とテスト」はCoddyKit上の無料C# Academyレッスンです。 これはレッスン2/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはC# Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 C# Academyコースには全3レッスンが含まれています。
実装計画
目標: 図形を実装して検証します。
- Rectangle、Circle、Triangle を実装します
- 無効な入力を防ぎます
- 小さな Assert ヘルパーを追加します
- Main で簡単なチェックを実行します
Rectangle と Circle
コンストラクターに簡単なガードを追加し、寸法が正の値になるようにします。数式は小さく明確に保ってください。
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
辺を検証します(三角不等式)。よく知られた公式を使用し、数値を読みやすく保ってください。
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());
}
}
アドホックな Assert ヘルパー
小さな Assert クラスを作成します。double の比較では許容誤差を設け、期待値と実際の値を比較します。
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");
}
}
簡単なテストを実行する
いくつかのテストを Main にまとめます。期待値は単純で読みやすいものにしてください。
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");
}
}
実装のヒント
チェックリスト:
- コンストラクターで入力を検証してください。
- 数式を短く保ち、円周率にはローカル定数を使用してください。
- アドホックなテスト用に小さな Assert を作成してください。
- 各図形の Area と Perimeter の両方をテストしてください。
アドホックテストの方法
まとめ
まとめ: 明確なガードを備えた図形を実装し、Main 内の最小限の Assert ヘルパーで検証します。テストは小さく決定的なものに保ちます。
よくある質問
「実装とテスト」レッスンは無料ですか?
はい。「実装とテスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、C# Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 C# Academyコースには全3レッスンが含まれています。
「実装とテスト」で何を学びますか?
Rectangle、Circle、Triangleを実装し、基本的な入力ガードを追加します。フレームワークを使わず、小さなアドホックテストを書きます。 ブラウザで直接実行するハンズオンコードでC# Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
C# Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのC# Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/3です。
「実装とテスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このC# Academyレッスンでコードを書いて実行できますか?
はい。すべてのC# Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。