Question

Use the Law of Demeter to evaluate the coupling in the following code fragments. If a code fragment violates the Law of Demet

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey, it is a very good problem to be understood. Let's start:

Code fragments are given in the question:

We need to find which part violates the law of Demeter.

For, understanding this let's have a brief introduction of the law of Demeter.

For a Class C and associated methods M, M should interact with the C objects when:

1. C object is referred.

2. M's argument objects are used

3. Instance variable objects of C

4. Objects created by M or methods which M calls.

5. Objects in a global variable.

Finding the problematic code:

Analyzing code fragment 1:

Since userID and email are the data members of the class and this thread is created from there. So, it is not a violation of the law of Demeter.

Rule 1 Applies here and hence this is not a violation.

Analyzing code fragment 2:

These 3 line violates the law of Demeter.

Rule number 4 states that objects created by the methods can execute their methods.

But here response is an object of Httpresponse and it is using the object of HttpClient which is a clear violation of law of Demeter.

A similar case is valid for the entity and content object. the entity is an object of HttpEntity but interacting with the HttpResponse object.

content is an object of InputStream but interacting with the object of the HttpEntity class.

I hope it will help.

Thanks and all the best :)

Please don't forget to thumbs up if it helped you.

***************************************COMPLETE************************************

Add a comment
Know the answer?
Add Answer to:
Use the Law of Demeter to evaluate the coupling in the following code fragments. If a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

  • Use your Food class, utilities code, and sample data from Lab 1 to complete the following...

    Use your Food class, utilities code, and sample data from Lab 1 to complete the following Tasks. def average_calories(foods):     """     -------------------------------------------------------     Determines the average calories in a list of foods.     foods is unchanged.     Use: avg = average_calories(foods)     -------------------------------------------------------     Parameters:         foods - a list of Food objects (list of Food)     Returns:         avg - average calories in all Food objects of foods (int)     -------------------------------------------------------     """ your code here is the...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • I need help writing the code for the fileget into the array Use FileGet to read...

    I need help writing the code for the fileget into the array Use FileGet to read vehicle information from a structured file named boytoys.dat into an array of the structure defined below (Copy and paste this code into your project.) Public Structure Boatlnfo <VBFixed String(15 Public make As String <VBFixedstring(15) Public model As String Public year As Integer <VBFixed String(4 Public pul chased As String <VBFixed String(18) Public location As String Public price As Long <VBFixedString (5) Public MSRP As...

  • I need help creating the methods for using a linked binary tree to build,evaluate, and paranthesize...

    I need help creating the methods for using a linked binary tree to build,evaluate, and paranthesize the expression. We have to use linked stack to pop and push the operands into the expression. Theres two classes: Expression: Constructor, set/gets, methods. ExpressionTest: Test driver for defining exp, and calling methods I need to put the mathematical exp (from the test class) into a binary tree, evaluate the exp in the binary tree, paranthesize it and print it out. So i need...

  • I am trying to understand the following code I found in a website so that I...

    I am trying to understand the following code I found in a website so that I can use it for a project that evaluates boolean expressions. The code is below but I think it is in java because I do not understand it. Can you help me describe what it is doing in c++ so that I can understand it better? The link to the code is here: https://stackoverflow.com/questions/16762057/algorithm-to-evaluate-value-of-boolean-expression The code is below: public static boolean evaluateBool(String s) { Stack<Object>...

  • Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly...

    Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...

  • According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord...

    According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. public class CandidateGenerator { private final LongFifo outputFifo; public CandidateGenerator(LongFifo outputFifo) { this.outputFifo = outputFifo; Runnable r = new Runnable() { @Override public void run() { runWork();}}; Thread t = new Thread(r, "CandidateGenerator"); t.start() } private void runWork() { try { outputFifo.add(2); long number = 3; while ( true ) { outputFifo.add(number); number += 2;} }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT