0

Cohesion in java

Consider the following java code which contains two classes Multiply and Display. Perform program analysis and identify what type of cohesion exists between the two classes. Also, comments what will be the impact of this cohesion during code maintenance. class Multiply { int a = 5; int b = 5; public int mul(int a, int b) { this.a = a; this.b = b; return a * b; } } class Display { public static void main(String[] args) { Multiply m = new Multiply(); System.out.println(m.mul(5, 5)); } }

22nd Nov 2020, 7:02 PM
faheem amjad
faheem amjad - avatar
1 Antwort
0
This is more about cohesion between tasks that are solved in one class. Since there is only one in each class, it is not enough to judge the type of cohesion. It suggests perfect atomic cohesion, but it is still possible to separate initialization a, b from computation (or not to do it at all), class testing from the display, and the display class solves elements in the computation class that do not do well. Better would be public class Multiply { public static int mul(int a, int b) { return a * b; } } public class Display { public static void integer(int r) { System.out.println(r); } } public class Multiply_test { public static void main(String[] args) { Display.integer( Multiply.mul(5, 5) ); } }
23rd Nov 2020, 10:48 PM
zemiak