6.2 Solutions

    1.  
      void plusEquals (Fraction other)
      {
         num = num * other.den + other.num * den;
         den = den * other.den;
      } 
    2.  
      Fraction plus (Fraction f)
      {
         Fraction sum = new Fraction();
         sum.num = num * f.den + f.num * den;
         sum.den = den * f.den;
         return sum;
      } 
    3.  
      void reduce ()
      {
         int min = min(abs(num), abs(den));
         for (int i = min; i > 1; i--)
            if (num % i == 0 && den % i == 0)
            {
               num /= i;
               den /= i;
            }
      } 
    1.  
      double modulus ()
      {
         return abs(sqrt(re*re + im*im));
      } 
    2.  
      void scale (double x)
      {
         re *= x;
         im *= x;
      } 
    1.  
      Complex plus (Complex c)
      {
         Complex sum = new Complex();
         sum.re = re + c.re;
         sum.im = im + c.im;
         return sum;
      } 
    2.  
      Complex times (Complex c)
      {
         Complex product = new Complex();
         product.re = re*c.re - im*c.im;
         product.im = re*c.im + im*c.re;
         return product;
      } 
    1.  
      double area ()
      {
         return PI*r*r;
      } 
    2.  
      Circle smaller (Circle c)
      {
         if (this.area() <= c.area())
            return this;
         else
            return c;
      } 
    3.  
      boolean isInside (Circle c)
      {
         double centreSeparation = sqrt((x-c.x)*(x-c.x) + (y-c.y)*(y-c.y));
         if (centreSeparation + r < c.r)
            return true;
         else
            return false;
      } 
  1.  
     void setup ()
    {
       // Part (a)
       Circle c1 = new Circle();
       c1.x = 4;
       c1.y = -1;
       c1.r = 3;
       Circle c2 = new Circle();
       c2.x = 3;
       c2.y = -2;
       c2.r = 5;
       // Part (b)
       println(c1.area());
       // Part (c)
       Circle small = c1.smaller(c2);
       println("Smaller circle has centre: "
                           + "(" + small.x + "," + small.y
                           + ") and radius " + small.r);
       // Part (d)
       if (c2.isInside(c1))
          println("c2 lies entirely within c1.");
       else
          println("c2 does not lie entirely within c1.");
    }