Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Coupling in Java Code

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Coupling in Java Code

    The the Coupling Corruption Propagation is an OO metric that computed as follows:

    Coupling Corruption Propagation = Number of child methods invoked with the parameters based on the parameters of the original invocation.

    In the code bellow, a parent method calculateStringBuildSpeed invokes a child method called buildString with the parameter length. The method buildString then calls three child methods of its own callStartTime, callEndTime, and reportResult. The methods callStartTim and callEndTime do not take any parameter. Therefore, they are effectively immune to any coupling effects that originate from calculateStringBuildSpeed. However,
    reportResult takes one variable stringLength that is calculated from the original length variable. If the variable is ever compromised (i.e., it becomes null, has a value that exceeds the int range, or contains a value that reportResult cannot use), then reportResult method could indirectly be corrupted from calculateStringBuildSpeed. Therefore, the coupling corruption propagation is 2 in the following code.

    So how can I modify the following code so that the Coupling Corruption Propagation will decrease for example into 1 or 0

    -------------------------------------------------

    Public void CalculateStringBuildSpeed(double
    numOfElements)
    {
    buildString (numOfElements);
    }
    .
    .
    .
    public String buildString(double length) {
    String result = "";
    int startTime;
    int endTime;
    int stringLength = (int) length;
    startTime = callStartTime();
    System.out.println("Starting Now");
    for (int i = 0; i < stringLength; i++) {
    result += (char)(i%26 + 'a');
    }
    endTime = callEndTime();
    reportResult(startTime,endTime,stringLength);
    return result;
    }

    ---------------------------------------------------------


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Coupling in Java Code

    Can you explain how the logic needs to be changed for that?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    --- Update ---

    Also posted at: question about coupling in java code
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Coupling in Java Code

    Public void CalculateStringBuildSpeed(double
    numOfElements)
    {
    buildString (numOfElements);
    }
    .
    .
    .
    public String buildString(double length) {
    String result = "";
    int startTime;
    int endTime;
    int stringLength = (int) length;
    startTime = callStartTime();
    System.out.println("Starting Now");
    for (int i = 0; i < stringLength; i++) {
    result += (char)(i%26 + 'a');
    }
    endTime = callEndTime();
    reportResult(startTime,endTime,stringLength);
    return result;
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Coupling in Java Code

    What happened to the formatting of the code? The indentations for the statements nested inside of {}s has been lost.
    Please edit the code and fix the indentations so the code is readable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Coupling in Java Code

    Public void CalculateStringBuildSpeed(double numOfElements)
    {
        buildString (numOfElements);
    }
    .
    .
    .
       public String buildString(double length) {
          String result = "";
          int startTime;
          int endTime;
          int stringLength = (int) length;
          startTime = callStartTime();
          System.out.println("Starting Now");
          for (int i = 0; i < stringLength; i++) {
               result += (char)(i%26 + 'a');
          }
          endTime = callEndTime();
          reportResult(startTime,endTime,stringLength);
          return result;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Coupling in Java Code

    That looks better.

    how can I modify the following code so that the Coupling Corruption Propagation will decrease for example into 1 or 0
    Can you explain how the logic needs to be changed for that? Once you have the logic, we can help you write the code to implement it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Coupling in Java Code

    Quote Originally Posted by Norm View Post
    That looks better.


    Can you explain how the logic needs to be changed for that? Once you have the logic, we can help you write the code to implement it.
    Coupling between methods is the concept that two or more methods are reliant on each other in some way. This could involve data sharing or decision making in the child methods using one of its call parameters. The effects of coupling can easily ripple into other methods several levels down the call chain. Coupling corruption propagation is meant to measure the total number of methods that could be affected by erroneous originating method. An arithmetic error can result when certain ranges of parameter values can be accepted in one method, but this range of values may exceed limits in another method that takes the same parameter. Such a situation can result in exception errors as well. Another potential security flaw results when a critical parameter is forced to remain at a certain value, and the result remains the same no matter what other parameters are changed.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Coupling in Java Code

    How does that relate to writing a program in java? What steps would a program take to work on that concept?
    It's a long way from what you have posted to a design for writing a program.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help in understanding cohesion vs coupling
    By smithjo in forum Java Theory & Questions
    Replies: 0
    Last Post: January 14th, 2013, 12:22 AM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM