Karla Carr

Ranch Hand
+ Follow
since Sep 17, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
10
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Karla Carr

salvin francis wrote:This should be the correct way to do it :



Are you sure you want to leave those methods and constructors with default access modifiers instead of public/private/protected ?
Also why is Mammal setting T to null ? Conceptually, I would be very confused by this program since it tells me nothing about T and U



Thanks for your reply. How do I actually use Generic constructors?



Would you be kind enough to debug this for me too?
5 months ago


Help me debug this code.
Thank you to everyone who helps.
5 months ago


Help me debug this code.
Thank you to everyone who helps.
5 months ago
What type of reference variable can reference to this object?
Is it any different from having my return type as my class name?

I got confused when the method navigableKeySet() returns a NavigableSet as its return type.

I referenced it using Set interface but now I am unsure which methods I can invoke on this object using my Set reference?
6 months ago


First of all, I am getting an InputMismatchException thrown right after Scanner takes in the first input.
Secondly, is my logic alright? I know the standard method of iterating using the entrySet() method but I am just trying to explore a bit and see if this works as well.
Thank you and I appreciate all the help provided to me.
6 months ago

Rob Spoor wrote:There's something else you need to know about TreeMap and TreeSet. Its keys either need to be Comparable, or you need to provide a custom Comparator when constructing the TreeMap / TreeSet. This is how the TreeMap / TreeSet determines order. In your case your class is not Comparable, and you do not provide a Comparator. Because you didn't provide a Comparator the TreeMap still tries to cast your keys to Comparable. That's where the ClassCastException comes from.

I think this is a small flaw in the API of TreeMap and TreeSet combined with some limitations of generics. Ideally the constructors that do not take a Comparator should be bound to only accept T extends Comparable<? super T>. That's not possible with the current generics implementation though. Otherwise you would have gotten a compiler error instead of an exception.

<br /> <br /> My code doesn't work when I insert a comparator in the constructor. Could you please debug the code for me? Thank you.
I also tried using this. However, I think only a comparator works and not a comparable.

6 months ago

Rob Spoor wrote:There's something else you need to know about TreeMap and TreeSet. Its keys either need to be Comparable, or you need to provide a custom Comparator when constructing the TreeMap / TreeSet. This is how the TreeMap / TreeSet determines order. In your case your class is not Comparable, and you do not provide a Comparator. Because you didn't provide a Comparator the TreeMap still tries to cast your keys to Comparable. That's where the ClassCastException comes from.

I think this is a small flaw in the API of TreeMap and TreeSet combined with some limitations of generics. Ideally the constructors that do not take a Comparator should be bound to only accept T extends Comparable<? super T>. That's not possible with the current generics implementation though. Otherwise you would have gotten a compiler error instead of an exception.

<br /> <br /> Thank you for the answer. Inserting a comparator in the constructor should work then. <br /> Could you also be kind enough to answer the last part of my original question? Thank you.
6 months ago

Campbell Ritchie wrote:I have got rid of the <b></b> tags for you. I don't know what caused them, but I am told there is a solution in preparation. Sorry about that.
At least your Team objects are immutable, so they can be used as “K”s in a Map, but why are you using a tree map? If you look in its documentation, particularly about this method you will find there is a specific requirement for all types used as “K”s. You will find it impossible to use Team as it stands as a “K” in a tree map.
Consider a hash map instead; that will require a decent hash code method however. Don't use the length of the name as a hash code. Try something like this:-The part before the plus sign may be omitted. Your equals() method is incorrect because it might throw an exception. You must precede the cast with a test for class equality or instanceof. Don't write if (b) return true; else return false; Write return b;Note that will still fail if name is ever null, and you ought to take precautions against nulls. It is important to use || and && not | or &, and the parts must go in that order.
Why do you have a points field in your Team objects? You don't ever seem to use it. What's more, because you have made the class immutable, that field will only ever be correct for the worst team which always loses and therefore has a points count of 0



How is my class immutable? I don't really understand. Could you also be kind enough to answer the last part of my original question? Thank you.
6 months ago

Campbell Ritchie wrote:I have got rid of the <b></b> tags for you. I don't know what caused them, but I am told there is a solution in preparation. Sorry about that.
At least your Team objects are immutable, so they can be used as “K”s in a Map, but why are you using a tree map? If you look in its documentation, particularly about this method you will find there is a specific requirement for all types used as “K”s. You will find it impossible to use Team as it stands as a “K” in a tree map.
Consider a hash map instead; that will require a decent hash code method however. Don't use the length of the name as a hash code. Try something like this:-The part before the plus sign may be omitted. Your equals() method is incorrect because it might throw an exception. You must precede the cast with a test for class equality or instanceof. Don't write if (b) return true; else return false; Write return b;Note that will still fail if name is ever null, and you ought to take precautions against nulls. It is important to use || and && not | or &, and the parts must go in that order.
Why do you have a points field in your Team objects? You don't ever seem to use it. What's more, because you have made the class immutable, that field will only ever be correct for the worst team which always loses and therefore has a points count of 0



Thanks for your answer. I am unsure of how 'name.hashCode()' works. It kinda looks like recursion ending with the StackOverflowException. I also don't get why I necessary need to check ob == this? Isn't that the whole point of overriding the equals() method in Object class? To design our own condition for equality since in Object class, equality is only when two references refer to the same object (==).
6 months ago
Here is the code.



I also wanted to know what this bit of code exactly does? I'm confused that how can I actually get a Set of a Map? I am not sure what actually happens there but I found this to be the way to iterate over Maps.


Thank you. Any help is appreciated.
6 months ago

Junilu Lacar wrote:One more comment about your code. You don't actually need line 16. The code will run perfectly well without it. That's because of polymorphism. Even when you reference something as an Object in your code, if it has specific behaviors for methods that it inherited from Object, then that specific behavior will be invoked. In this case, it's the polymorphic behavior of the toString method that will be invoked. So you don't actually have to worry about casting to a String in this particular program.

Now, it would be a different story if you were trying to invoke behavior that was specific to String objects. For example, you couldn't write this:

In the above code, you know that o is actually a String. However, the reference variable o is seen by the Java compiler only as an Object and therefore, it will complain because there is no length() method in Object. In this case, you will have to cast o to a String, like what have now:

You could do this as well:




Thank you for your detailed response to me.
I think I have a better understanding of things now.
What I could infer from from your explanation is that we actually need to be conscious of the type of the object and not the type of the reference. Am I correct in my interpretation?
6 months ago

Tim Moores wrote:Check the javadocs of the Comparable.compareTo method - under certain circumstances it returns a negative number, which your method never does.


Thank you for helping me out. It worked now!
6 months ago


I really get confused with how do I actually check if the cast is valid or not. This one seems totally wrong to me yet this works.
6 months ago


I tried to sort on basis of their length.
Code compiles and runs but I don't get a sorted list.
Any Help is appreciated.
Thank you to all those who take the time to read and help me out.
6 months ago

Rob Spoor wrote:To answer your question directly: no, you can't use Arrays.binarySearch, but you can use Collections.binarySearch. Arrays is only for (duh) arrays, Collections is for Lists etc.



Thank you for the answer. However, how do I search using Collections.binarySearch() for an playerName object whose name parameter is "jorginho" for example? Could you manually write the code here?
7 months ago