public static void Main(String[] args) { Rat r1 = new Rat(3, 4); Rat r2 = new Rat(5, 6); Console.WriteLine(r1 + " + " + r2 + " = " + (r1 + r2)); Console.WriteLine(r1 + " == " + r2 + " = " + (r1 == r2)); Console.WriteLine(r1 + " != " + r2 + " = " + (r1 != r2)); Console.WriteLine("Hash of " + r1 + " = " + r1.GetHashCode()); Console.WriteLine(r1 + " + " + 7 + " = " + (r1 + 7)); }
public void TestMethod2() { try { Rat r = new Rat(1, 0); Assert.Fail(); } catch (ArgumentException) { } }
public void TestMethod3() { Rat r = new Rat(1, 0); }
public void TestMethod1() { Rat r = new Rat(3, 6); Assert.AreEqual("1/2", r.ToString()); }
// Note how this method defines a new operator. Also note // the use of the checked block. Without it, the // arithmetic overflows within would be ignored. /// <summary> /// Returns the sum of r1 and r2. /// </summary> /// <exception cref="System.OverflowException">When arithmetic overflow</exception> public static Rat operator +(Rat r1, Rat r2) { Debug.Assert(r1.RI()); Debug.Assert(r2.RI()); checked { Rat result = new Rat(r1.num * r2.den + r1.den * r2.num, r1.den * r2.den); Debug.Assert(result.RI()); Debug.Assert(r1.RI()); Debug.Assert(r2.RI()); return result; } }