Question

In: Computer Science

I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint:...

I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint: matches() should have returned true when item matches the item passed into IdLookup constructor. Here is my code. What changes should be made?

/**
* This class is a lookup that matches items whose id matches exactly
* a specified id, to be passed into the constructor when creating
* the instance of the lookup.
*
* @author Franklin University
* @version 2.0
*/
public class IdLookup implements Lookup
{
private String lookupId;

/**
* Constructor for objects of class IdLookup.
* @param id the id to lookup.
*/
public IdLookup(String id)

{
  
   lookupId = id;
  
}

/**
* Indicates whether the item's id exactly matches the id
* passed into the contructor.
* @param item the item being checked for a match.
* @return true if the id of the item matches, otherwise
* false.
*/
@Override
public boolean matches(Item item)
{
   if (item == null) {
       return false;
      
   }
  
  
  
   return lookupId == item.getId();
      
     
  
}
}

Solutions

Expert Solution

Here is the corrected code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: You did not provide Lookup interface, or Item class, so I can only assume that everything else is working correctly and getId method of Item class returns a String. Your mistake was to use == operator to compare Strings which you should never do in order to compare their contents. For that, you should use equals() method. The reason is explained in comments.

public class IdLookup implements Lookup {
        private String lookupId;

        /**
         * Constructor for objects of class IdLookup.
         * 
         * @param id
         *            the id to lookup.
         */
        public IdLookup(String id)

        {

                lookupId = id;

        }

        /**
         * Indicates whether the item's id exactly matches the id passed into the
         * contructor.
         * 
         * @param item
         *            the item being checked for a match.
         * @return true if the id of the item matches, otherwise false.
         */
        @Override
        public boolean matches(Item item) {
                if (item == null) {
                        return false;

                }
                // assuming getId returns a String
                // you should use equals() method to compare two Strings, never use ==.
                // In case of Strings, == returns true only if the two objects have same
                // memory (i.e the two String objects being compared refer to the same
                // address in memory). equals() method returns true if two Strings have
                // same contents regardless of their memory address.
                return lookupId.equals(item.getId());

        }
}

Related Solutions

Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
I am doing a post lab for a acid and base buffers lab. I was asked...
I am doing a post lab for a acid and base buffers lab. I was asked to find the theoretical pH (what the pH should actually be) for each of my pH's in my data please help it is due tonight and that is the only question I cannot figure out. I just need one of the theoretical pHsd' one and I can do the rest. Background: 30 mL of sodium dihydrogen phosphate and 20 mL k2HPO4 was added to...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
I am doing a paper on stressors and my stressor is my job. I have had...
I am doing a paper on stressors and my stressor is my job. I have had an arguement with a superior who is know for being a bully, but i stood up to him and could end up unemployed. Having four kids, a wife, and bills to pay, this could be devastating. Using this stressor, what physiological changes occur in the brain due to the stress response? And what emotional and cognitive effects might occur due to this stressful situation?
I am stuck in Austin with a flat tire, and I need to get to my...
I am stuck in Austin with a flat tire, and I need to get to my class in Houston within 2 hours. The drive is 180 miles long, but I want to be careful of the Austin cops. I do not usually get caught speeding unless I am seen accelerating too fast, so I do not want to accelerate at a rate of more than 120 mi?/?h2. Just to be careful, I?m going to take exactly 2 hours to make...
i am doing an internship in the physics department i was lucky enough to actually get...
i am doing an internship in the physics department i was lucky enough to actually get this internship as ive just finished my first year. the problem is i have a very professional professor he is so so smart so whenever he explains something to me i just don't understand it, i feel like we have so much mis understanding between me and him. The way he asks me to do things seems so vague to me but so obvious...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
This is my lab and I am very confused regarding the calculations! It is supposes to...
This is my lab and I am very confused regarding the calculations! It is supposes to demonstrate Le Chatelier’s principle. After doing the following I need to calculate the molar absorbance for Co(H20)6 2+. The path length is 1cm and absorbance is .551. I got 5.51 L/mol *cm. I also need to calculate the CoCl4 2- molar absorbance which I do not understand. The absorbance is .570 and the path length is 1cm. I do not know how to find...
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
Ok this is a exerpt from my lab. I prepared all my solutions correctly, and am...
Ok this is a exerpt from my lab. I prepared all my solutions correctly, and am getting ready to use the spectrophotometre. But is is asking for precise concentrations, and i am confused as two how to use the dilution formula M1V1=M2V2 to find them. Any advice would be appreciated. You should now have six “dilute” solutions containing known concentrations of tetraaquasalicylatoiron(III) ion in addition to the stock solution. The concentrations of these dilute solutions are approximately (assumes stock solution...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT