Question

In: Computer Science

Which cohesion does this method belong to? Public void sampleMethod(int flag) { Switch (flag) { Case...

Which cohesion does this method belong to?

Public void sampleMethod(int flag)
{
Switch (flag) {
Case ON:
// code
Case OFF:
// code
Case CLOSE:
//code
}
}

Solutions

Expert Solution

Cohesion

Cohesion is a design technique and it is a part of system design. It is the measure of the functional strength of a module.

High cohesion is preferred because it reduces the error propagation, increases the reusability of a module, and decreases the complexity of the design.

If we have changed the parent class then we need to make a lot of changes in the subclass as well. It means that cohesion is low but cohesion must be high.

Type of cohesion:

  1. Coincidental cohesion
  2. Logical cohesion
  3. Temporal cohesion
  4. Procedural cohesion
  5. Communication cohesion
  6. Sequential cohesion
  7. Functional cohesion

Here, we have given a function that performs a well-defined task. An integer type parameter is passed and on the basis of the passed parameter, a specific action is performed.

So, the given source code is an example of a functional cohesion.

Functional cohesion is best because it reduces the error propagation chances.


Related Solutions

rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
For each call to the following method, indicate what console output is produced: public void mystery2(int...
For each call to the following method, indicate what console output is produced: public void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } } mystery2(1); mystery2(4); mystery2(16); mystery2(30); mystery2(100);
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT