Design Metrics

A. Cohesion

In the following designs:

a. Draw MCG(C) and compute LCOM1(C)

b. Draw MAG(C) and compute LCOM2(C)

A1.

class C {
   int f1, f2, f3, f4;
   int m1() { return f1; }
   int m2() { return f2; }
   int m3() { return f3; }
   int m4() { return f4; }
}

A2.

class C {
   int f1, f2, f3, f4;
   int m1() { return f1 + f2; }
   int m2() { return f1 + f3; }
   int m3() { return f2 + f4; }
   int m4() { return f3 + f4; }
}

A3.

class C {
   int f1, f2, f3, f4;
   int m1() { return f1 + f2 + f3; }
   int m2() { return f2 + f3 + f4; }
   int m3() { return f1 + f4; }
   int m4() { return 0; }
}

B. Responsibility and Stability

Each of the following diagrams depict the contents of some package, p. For each package compute the responsibility, stability, encumbrance, and deviance of each class.

B1.

B2.

B3.

C. Coupling Degree

In each of the following exercises assume B is the class:

class B {
   public void meth1() { ... }
   public void meth2() { ... }
   public void meth3() { ... }
   public void meth4() { ... }
}

Also assume that the instability of B is 0.

Compute couplingDegree(A, B) for the following declarations of A:

C1.

class A {
   private B b = new B();
   public void meth1() { b.meth1(); }
   public void meth2() { b.meth2(); }
   public void meth3() { b.meth3(); }
   public void meth4() { b.meth4(); }
}

C2.

class A {
   private B b = new B();
   public void meth1() { b.meth1(); }
   public void meth2() { b.meth1(); }
   public void meth3() { b.meth1(); }
   public void meth4() { b.meth1(); }
}

C3.

class A {
   private B b = new B();
   public void meth1() {
      b.meth1();
      b.meth2();
      b.meth3();
      b.meth4();
   }
   public void meth2() {  }
   public void meth3() {  }
   public void meth4() {  }
}

D. Coupling Degree 2

A cruder measure of the coupling degree is:

couplingDegree2(A, B) =
   the probability that a random change to B will require a change to A

In each of the following exercises assume B is the C++ class:

class B {
public:
   void meth1() { ... }
   void meth2() { ... }
protected:
   void meth3() { ... }
   void meth4() { ... }
private:
   void meth5() { ... }
   void meth6() { ... }
};

Assume a randomly chosen B method is to have its name changed. For example, meth3 will be renamed to method3.

For each of the following declarations of A, compute the maximum value of couplingDegree2(A, B):

D1.

class A {
   B b;
   // etc.
};

D2.

class A: public B {
   // etc.
};

D3.

class A {
   friend class B;
   // etc.
};

E. A package p contains declarations of five classes A, B, C, D, and E. Classes C, D, and E have instability = 0. Here are the declarations of A and B:

class B {
   int task1(D d) { ... }
   int task2(E e) { ... }
}

class A extends D {
   B b;
   int x, y;
   void task3() { x = b.task1(this); }
   void task4() { System.out.println(x); }
   void task4() { y = b.task1(this); }
}

E1. Draw a dependency graph for package p.

E2. Compute the instability(B) and responsibility(B).

E3. Compute couplingDegree(A, B).

E4. Compute LCOM1(A).

E5. Compute LCOM2(A).

F. The input degree of a class is the average number of parameters per declared method. Using Java reflection, complete the implementation of this metric:

class Metrics {
   static double inputDegree(Class c) { ??? }
}

G. One way to measure the coupling degree between to classes A and B is to count the number of declared methods in A that have at least one parameter of type B. Using Java reflection, finish the following implementation:

class Metrics {
   static int cplDeg(Class a, Class b) { ??? }
}