Bob Sherry

Ranch Hand
+ Follow
since Jun 27, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
12
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bob Sherry

I do not know what the worse case running time of the insertion into a HashMap is. It depends on the algorithm used. It should be a function of how much of the HashMap is already used. The problem is collisions.
3 years ago
I do not understand how HashMap can have an O(1) (worst case) insertion time even with an Integer key because the insertion routine is going to do a mod operation on that hash value and that could result in a collision.
3 years ago
I totally agree with you that if you need 32 bits to represent a character my solution is no good. I thought characters were 16 bits in Java. The array is going to be faster, maybe much faster. Insertion into a map is not going to be O(1).
3 years ago
The reason I thought <T> was needed because of Post number 23041 which can be found at the following URL:
https://coderanch.com/t/738693/java/Generic-classes-simple-types
[Edit]The topics have been merged so you can find the post by Tim above.

The post is by Tim Holloway.
3 years ago
Please consider the following code:

In this case, the code does not compile. However if I were to change:
public T1<T>()
to:
public T1<T>()
It would compile. However, I believe I need the <T> to have it as a constructor.
3 years ago
The method given is suppose to be a constructor hence it would not return a value. I suspect I got the syntax wrong. Did I?
3 years ago
What counts as better is subjective. If I was doing this, I would us a loop to look at each character in the string. I would convert each character to a number (which is about 0 time) and instead of a map, I would use an array of 256 elements. This assumes that all characters will fit in 8 bits which is probably out of date thinking. Better would be use an array of 2^16 elements. Each element of the array would be either 0, 1 or 2. If it is 0, that means we have not seen the character yet. 1 means we have but we have not moved printed it yet. 2 means we have seen it before and printed it.

What do people think of my solution?
3 years ago
The following class does not compile:



I would like to create a template class where the template class will be a number. That is, when it is instantiated the type used will be an int, double or something like that. The above code does not compile. I believe the reason for this is that the type T must be a class. It could be Double but it cannot be double. Do I have that right?
3 years ago

Mike Simmons wrote:For example, what happens if this.value = 42.0 and name.value = 42.5?  Then dTmp = 0.5, but what gets returned?


Mike you are right. I have a problem. I changed the if to compare with 0 and now it works.
4 years ago

Carey Brown wrote:Can you provide us with a SSCCE (simple self contained compilable example) that we can compile and run that would demonstrate your issue? Other than Norm's comment, I don't see anything wrong, so I'm wondering about the larger context of your code.


I will if you still want me to (it will no be all that simple) but after making the change suggested by Norm, my problem is fixed.
4 years ago

Norm Radder wrote:Why does statement on line 15 compare against 1?


I should be comparing with 0. I changed the code to compare with 0 and it now works.
4 years ago
I am trying to sort an array of objects of type Name. Name is a class that I defined. When I call the sort function, I need to pass the comparison function to sort. I wrote two
comparison functions. Here they are:



The first one works. Second one does not. The second one does not print: bad input. When I use the second one, I get the following error messages:

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:406)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:213)
at java.util.Arrays.sort(Arrays.java:1246)
at Test2.main(Test2.java:84)
C:\Users\rsher\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)


Here is how the class Name is defined:


It is a generic class and it is not public. Does this matter?

Thanks,
Bob
4 years ago
Please consider the following Java Fragment:



In this case, I am calling close on both the printWriter object and the fileWriter object. That seems redundant to me. Is it?

Thanks,
Bob
4 years ago
Please consider this routine:


The variable currentPrice is defined in the class as:

The if statement above the following warning:
   Invert If
   Flip operands of the binary operator
If you switch the order of the operands to != the warning goes away. The warming says to switch the order of the operands to !=. The warning does not make sense to me. What am I missing?

I am using NetBeans.
Thanks,
Bob
4 years ago
I am working on a program that is computational intensive and in certain cases, I am getting wrong results. I believe it is due to a floating point underflow. I can and should test for the division to be 0.0. I am not doing that.

It seems to me, that in the modern era, programmers want to use try and catch. In this case, I will use an if. It really does not matter.

Bob
4 years ago