Daniel Perera wrote:
Jeff Verdegan wrote:
You're still doing the exact same thing--shifting elements down.
+ means it's already there, -means it's not. By dealing with +/- AFTER the above logic, you're going to have a serious problem if it's already there. Think about what your * -1 logic will do.
What do you WANT to do if it's already there? Whatever that is, you have to make that decision BEFORE you execute the insert logic.
Jeff Verdegan wrote:What does size represent? The number of elements currently in the array?
Look closely at this bit:
It looks like you're calling binarySearch() for the new size, before you add the new element.
Do you see why that would be a problem?
Daniel Perera wrote:
I think so - I'm increasing the number of elements in the array before physically adding a new element to the array itself. Is moving size++; to the following bit the right course of action?
Jeff Verdegan wrote:
At a quick scan it looks right, but since you're so close now, the best way to find out is to test it, and if needed, and println() statements to see what's going on step by step.
~ Mansukh
Mansukhdeep Thind wrote:I can see 2 mistakes in your code:
First : What does size represent? The number of elements in the array. Right? So, after inserting the first element, the size should be 1, not 0, then 2 and so on. Initialize size to 0, not -1, in the constructor.
Second : Think carefully that exactly where would you be placing the new entry in the directoryArray. Simply multiplying the placeEntry by -1 is not the insertPostion. Remember that the way we are using binarySearch() here, it will always return a -ve value as the entry is not present in the array. And what does that returned value represent? It is not the insertion point of the entry but equal to (-(I.P) -1). Therefore, insertionPoint needs to be calculated as:
Hence, insertionIndex = placeEntry-1
So, if your binarySearch() returns -2, it means the actual insertPosition would be -(-2) -1 = 1. Are you with me?
Whenever placeValue is negative, it means that the actual insertion point needs to be calculated by you using the -(I.P) - 1 formula. See the documentation of the method. So, I.P would become -1*(placeEntry) -1). So, what your method will partly look like is :
~ Mansukh
Mansukhdeep Thind wrote:Yes, that is what I wrote 6 hours ago. Nice work David. Now that you have completed the addEntry(String, String, String) method, functionally at least, lets move on. We will be coming back to refine the methods and add documentation and comments though. Which one do you want to go for next?
~ Mansukh
Mansukhdeep Thind wrote:Oh yes, Daniel not David. Anyways, let me first confirm the use case. You want to remove an entry from the array if that surname or extension exists in the directory. Am I correct?
~ Mansukh
Mansukhdeep Thind wrote:Also, what prompted you to use a try-catch construct?
Mansukhdeep Thind wrote:Why not pass Entry reference as a parameter to the remove method instead of a surname? Why not have a remove(Entry entry) method? Does you assignment specifically say so? Also, what prompted you to use a try-catch construct?
Daniel Perera wrote: 2) Delete entries from the directory either by name or number
~ Mansukh
Mansukhdeep Thind wrote:
Daniel Perera wrote: 2) Delete entries from the directory either by name or number
Do you mean delete the first entry or all the entries that match the surname? There could be more than one person having the same surname. Then what?
~ Mansukh
Mansukhdeep Thind wrote:For the time being, let us assume that there are no duplicate surnames. Write down the logic that you will be implementing to search and remove the passed surname. How do you know for sure that your current remove(String) method works correctly without any issues? Did you try and print the list before and after the call to remove(String)?
~ Mansukh
Mansukhdeep Thind wrote:Show me your printDir() method. Also, think how will you cater for the scenario where one would like to delete an entry based on en extension and not a surname.
Daniel Perera wrote: However I then read up about binary search and realized it would be more efficient to use binary search than to look through the whole array to find the matching entry. I'm not sure how I can do it for two possibilities though but one thing that I thought of was perhaps if I did something like this:
I'm not sure if making two separate entries makes sense, and I'm not sure how I'd add another for loop and make it choose the right for loop depending on what info the user passes to the method.
Daniel Perera wrote:Here is my printDir() method:
~ Mansukh
Mansukhdeep Thind wrote:Why not check what is being passed right at the beginning of the method? If it is a surname, create the entry for the first type else of the second type. Why to create two entries? Use an if check in the beginning itself. As a thumb rule, try and filter input as much as possible before implementing the actual business logic in any java method.
You do not need to write two for loops either. Once you have the correct entry with you from the if-else construct, go ahead, search for it and remove it. Are you with me? You can't, as you said, choose a for loop depending on the input. With the switch-case construct, you can do selective execution though. But that is not needed here.
~ Mansukh
Mansukhdeep Thind wrote:What exactly are you searching for Daniel? Is there any entry ("BROWN", "ilove", "csc1022" ) in your directory? How do you expect the binarySearch() to find it? You have to tweak the logic to find the entry only on the basis of a surname or an extension? Getting the idea? Because you only have that part of the information as a passed argument. Think.
~ Mansukh
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Jeff Verdegan wrote:I don't see your size variable changing during a delete operation. When you delete an element, does size remain the same?
Daniel Perera wrote:
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Checked several times, it works, no exception no nothing, did printDir() before and afterwards as well and the entry is deleted... Provided the Entry class only compares surnames in the compareTo method, it works absolutely fine.
~ Mansukh
Daniel Perera wrote:
Jeff Verdegan wrote:I don't see your size variable changing during a delete operation. When you delete an element, does size remain the same?
Does that matter? I can do size--; at the end I suppose?
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Checked several times, it works, no exception no nothing, did printDir() before and afterwards as well and the entry is deleted... Provided the Entry class only compares surnames in the compareTo method, it works absolutely fine.
I have the compareTo() method that compares both surnames and initials. Hence, the exception. Do you intend to keep it that way? If you change the compareTo() later, it might break your code as a lot of the implemented logic is based on searching that uses this method. Also, cater for reducing the size for each entry removed as Jeff rightly said.
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Checked several times, it works, no exception no nothing, did printDir() before and afterwards as well and the entry is deleted... Provided the Entry class only compares surnames in the compareTo method, it works absolutely fine.
I have the compareTo() method that compares both surnames and initials. Hence, the exception. Do you intend to keep it that way? If you change the compareTo() later, it might break your code as a lot of the implemented logic is based on searching that uses this method. Also, cater for reducing the size for each entry removed as Jeff rightly said.
Daniel Perera wrote:
Jeff Verdegan wrote:I don't see your size variable changing during a delete operation. When you delete an element, does size remain the same?
Does that matter?
Daniel Perera wrote:
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Checked several times, it works, no exception no nothing, did printDir() before and afterwards as well and the entry is deleted... Provided the Entry class only compares surnames in the compareTo method, it works absolutely fine.
I have the compareTo() method that compares both surnames and initials. Hence, the exception. Do you intend to keep it that way? If you change the compareTo() later, it might break your code as a lot of the implemented logic is based on searching that uses this method. Also, cater for reducing the size for each entry removed as Jeff rightly said.
Hey again, ok I just spoke with people on my course and re-read the specs and it appears that we should be deleting entries like this: d.removeEntry("PERERA", "D");... So, I changed the removeEntry(String deleteMe) to removeEntry(String deleteSurname, String deleteInitials). Works fine for deleting a name like d.removeEntry("PERERA","D") but if I wanted to delete an entry by extension, a friend of mine said that I can't do it by using binarySearch because it is sorted by surname and not by extension and binarySearch requires the stuff to be sorted properly. So he said I should use a for loop to delete by extensions. However, since I made the removeEntry method take two parameters I don't know how I could also make it work for one parameter - an extension.. Please advise, really need to get this part finished asap
~ Mansukh
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:Are you sure it works? I am not so sure though. Check again. You will get an exception on the console.
Checked several times, it works, no exception no nothing, did printDir() before and afterwards as well and the entry is deleted... Provided the Entry class only compares surnames in the compareTo method, it works absolutely fine.
I have the compareTo() method that compares both surnames and initials. Hence, the exception. Do you intend to keep it that way? If you change the compareTo() later, it might break your code as a lot of the implemented logic is based on searching that uses this method. Also, cater for reducing the size for each entry removed as Jeff rightly said.
Hey again, ok I just spoke with people on my course and re-read the specs and it appears that we should be deleting entries like this: d.removeEntry("PERERA", "D");... So, I changed the removeEntry(String deleteMe) to removeEntry(String deleteSurname, String deleteInitials). Works fine for deleting a name like d.removeEntry("PERERA","D") but if I wanted to delete an entry by extension, a friend of mine said that I can't do it by using binarySearch because it is sorted by surname and not by extension and binarySearch requires the stuff to be sorted properly. So he said I should use a for loop to delete by extensions. However, since I made the removeEntry method take two parameters I don't know how I could also make it work for one parameter - an extension.. Please advise, really need to get this part finished asap
There is something called varargs in Java which is used for precisely this kind of a use case. Do you know what are varargs Daniel? Instead of remove(String, String) or remove (String), I can say remove (String...). This means that the method can accept a variable number of Strings. Try and see if your problem is solved.
Mansukhdeep Thind wrote:
Daniel Perera wrote:Works fine for deleting a name like d.removeEntry("PERERA","D") but if I wanted to delete an entry by extension, a friend of mine said that I can't do it by using binarySearch because it is sorted by surname and not by extension and binarySearch requires the stuff to be sorted properly. So he said I should use a for loop to delete by extensions. However, since I made the removeEntry method take two parameters I don't know how I could also make it work for one parameter - an extension.. Please advise, really need to get this part finished asap
There is something called varargs in Java which is used for precisely this kind of a use case. Do you know what are varargs Daniel? Instead of remove(String, String) or remove (String), I can say remove (String...). This means that the method can accept a variable number of Strings. Try and see if your problem is solved.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
~ Mansukh
Mansukhdeep Thind wrote:What's the reason that you say varargs are not the way to go Andrew? Any particular cause?
Mansukhdeep Thind wrote:
Daniel Perera wrote:
Hey again, ok I just spoke with people on my course and re-read the specs and it appears that we should be deleting entries like this: d.removeEntry("PERERA", "D");... So, I changed the removeEntry(String deleteMe) to removeEntry(String deleteSurname, String deleteInitials). Works fine for deleting a name like d.removeEntry("PERERA","D") but if I wanted to delete an entry by extension, a friend of mine said that I can't do it by using binarySearch because it is sorted by surname and not by extension and binarySearch requires the stuff to be sorted properly. So he said I should use a for loop to delete by extensions. However, since I made the removeEntry method take two parameters I don't know how I could also make it work for one parameter - an extension.. Please advise, really need to get this part finished asap
There is something called varargs in Java which is used for precisely this kind of a use case. Do you know what are varargs Daniel? Instead of remove(String, String) or remove (String), I can say remove (String...). This means that the method can accept a variable number of Strings. Try and see if your problem is solved.
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|