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

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...
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...
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...
I am studying for Investments 10th by Bodie Kane in my University. As doing assignments, I...
I am studying for Investments 10th by Bodie Kane in my University. As doing assignments, I have these questions concerning margin problems. Could any experts help me to solve these questions?:) Rachel opens a margin account and purchases 400 shares of Ross Inc. at $40 per share. The initial margin requirement is 50%. The share price falls to $25 per share by the end of the year. 1) What is the remaining margin in the account? 2) If the maintenance...
I almost finished my pre-lab... but I am stuck! What are the energies of (-)-menthone and...
I almost finished my pre-lab... but I am stuck! What are the energies of (-)-menthone and (+)-isomenthone? How can I find this (how to do it)? Thank you for the help!
Wondering if I am doing these calculations correctly I have a lab report due today. Questions...
Wondering if I am doing these calculations correctly I have a lab report due today. Questions Load P1 = 0 lb P2 = 350 lb P3 = 700 lb Gauge # Strain Stress Strain Stress Strain Stress 1 1102 724 383 2 1876 1546 1324 3 2177 2179 2181 4 876 1204 1558 5 662 989 1296 Sample Calculation: = s / e (10,600,000 psi)*1102 = 1.168 x 10^10 lb E = 10600 ksi = 10,600,000 psi
Hi, I am doing up my homework on mathematics and here are some questions. I want...
Hi, I am doing up my homework on mathematics and here are some questions. I want to cross-reference my answers thank you. Decide if the statements below are True or False. Please include some rough workings for me to understand: (1) Mr. Tan borrowed $500,000 from Bank XYZ at 5% annual interest to be repaid monthly over 20 years. The amount that he pays back to XYZ each month is between $3000 to $3500. (2) Continuing from (1): after 15...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT