Eric Barnhill

Rancher
+ Follow
since Feb 25, 2000
Eric likes ...
Ubuntu
The University of Edinburgh
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
6
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Eric Barnhill

Many, many thanks for these terrific responses. I will give all this a re-think.
2 years ago
I'm working on the Complex() class over at apache commons-numbers, currently conforming multiplication and division methods to comply to C99 standards (G.5.1 to be specific).

Some C-specific methods are called for and I am searching for the best equivalent in Java; for example, the complex multiplication standard calls for scalbn() and Java has the equivalent in Math.scalb().

The complex division standard calls for  logb() which is an efficient way of taking the 2-log by shifting bits, that avoids under- and overflows that can occur from a simpler formulation such as Math.log(x) / Math.log(2) .

I cannot find a similar method in the Math API, but maybe I don't have the right name. Has anyone come across a standardized logb() or similar bit-shifting method of taking log2 in Java?

This thread elsewhere has some methods for integers, but I need to handle doubles.
2 years ago
Thank you very much for the tips. Use of Double[] was key, then this works:



Although as you also suggested, calling a 1D method within the stream will make more design sense most of the time.
2 years ago
I found this thread https://coderanch.com/t/679348/java/Java-Streams-Convert-multidiamentional-array which gives a nice example of converting a multidimensional array to a nested List. I am interested in a similar application but rather applying mathematical operations to arrays of higher dimension.

Here is my best shot. The idea is just to double each entry of a 2D array using streams.

import java.util.Arrays;



Error is "bad return type in lambda expression". Thanks for any help.
2 years ago
Unfortunately the Bar methods are somehow not right. The void keyword should not be there and those are constructors not methods. When they are constructors you get the compiler message seen there. I must have posted the wrong vim register but don't see a way to edit my post.

But, I think your answers would still be the same. It looks like the most elegant solution is probably to take an Object argument and then using getClass() cast it and pass it on to the relevant subtype and method. Thanks!
2 years ago
I want to design a library that takes as input arrays of varying dimension and variable type, then processes them in different pipelines according to a preferences file. The idea is that the switch method will choose one of these pipelines, and an overload will handle the varying types.

I have written some toy code that abstracts the problem. I think if I could get this code to work I would know how to design my project.

I have a base class Foo which takes an argument of either Float or Double type, and an integer for preferred pipeline. Here is the code I have written for this. Note it does not compile.



I then have two pipeline classes Bar1 and Bar2 . These both compile.



and



The desired output is

Bar 1 Double
Bar 1 Float
Bar 2 Double
Bar 2 Float

However the compiler isn't having any



I would really like to do this some more elegant way than casting the Object. Am I stuck casting the Object or is there a more elegant way to handle these cases?
2 years ago
I have a simple code like so, which imports a single class



and I can compile it like so (hopefully the references to a typical Unix maven repository will be familiar enough to everyone)



where I link to the jar. But, when I run it something interesting happens. The following command, with jar followed by local directory dot, runs:



with the output



which is correct. However this same command, with the dir dot and the jar path switched:



throws the following error:



In otherwords, I think java is "giving up" after the dot and not looking for the jar for some reason. Is this expected behavior? Is there a principle here I need to understand?
2 years ago
That clarifies matters a great deal, thank you.
2 years ago
You are right, I was looking at an outdated overview page from a previous Java version.

This discussion helped me understand the keyword this a lot better though.

Thank you for your help.
6 years ago
Hi, sorry to be asking the simplest possible question about this API, but I am getting a complier error right off the bat and not sure why.

Java Preferences API Overview states:

The following examples illustrate how you might obtain the Preferences objects (system and user) pertaining to the enclosing class. These examples would work only inside instance methods.



It then cites:



The above example obtains per-user preferences. If a single, per-system value were desired, the first line in foo would be replaced by: Preferences prefs = Preferences.systemNodeForPackage(this);





So I added the following method to my class ViscoSimGUI:



But I immediately get the error

The method systemNodeForPackage(Class<?>) in the type Preferences is not applicable for the arguments (ViscoSimGUI)



It's definitely an instance method as the constructor is



I figure I am missing something too obvious for even the API overview, and would appreciate any help. Thanks, Eric
6 years ago
Thank you for the reply. The matter is cleared up.

Eclipse has automatic override placeholder text that it places if a new anonymous ActionPerformed is placed within a new anonymous ActionListener. That code threw in an extra bracket, and it was quite hard to spot.

That was why I mentioned Eclipse -- I figured it was messing with me as I have coded plenty of swing!
6 years ago
Hi,

Can anyone explain why, for a javax.swing.JCheckBox jcb, the statement



produces the error



Syntax error on ";" , assert expected after this token



The API says isSelected() returns boolean, so I would think this is okay?

Many thanks, Eric

I am compiling in Eclipse and running Java 8.
6 years ago
Hi,

I'm pretty inexperienced with code strategy so I just wondered if this was a good one. For something like rounding, in which in my experience a few steps are needed to get around Java arithmetic issues, is a static method that takes the necessary few steps a good way to handle the difficulty.

So my code is



Run using



Output obviously is


Just wondering if my idea of a static method is a good way to handle this situation? In my package, I would just stick it in a class of static utility methods. Thanks for any feedback!
7 years ago
Dear Anayonkar, thank you very much for your reply and I am happy to post the entire method.

I implemented a Hashtable with integer keys since I may want to later re-key the groups by a factor in a later method. But, I could use an ArrayList of ArrayLists if you think the snag may lie there.

Obviously all the println's are a desperate attempt to debug it!



Output in a 4x4 test matrix yields:


Considering pixel: 1 1 on groups size 0
adding to new group: 1 1
New group groupkey: 0
New group size is: 1
0 1 outside threshold
2 1 outside threshold
1 0 outside threshold
1 2 outside threshold
put in group number 0
Considering pixel: 1 2 on groups size 1
m is now: 0
test group size: 1
item number 0 is 1 2
test group size: 1
we are on item: 0
the contents of this are: 1 2
1 2 is the same as 1 2
already grouped :1 2
groupKey is 0
0 2 outside threshold
2 2 outside threshold
1 1 outside threshold
1 3 outside threshold
put in group number 0
Considering pixel: 2 1 on groups size 1
m is now: 0
test group size: 1
item number 0 is 2 1
test group size: 1
we are on item: 0
the contents of this are: 2 1
2 1 is the same as 2 1
already grouped :2 1
groupKey is 0
1 1 outside threshold
3 1 added to group
New group size is: 2
2 0 outside threshold
2 2 outside threshold
put in group number 0
Considering pixel: 2 2 on groups size 1
m is now: 0
test group size: 2
item number 0 is 2 2
test group size: 2
item number 1 is 2 2
test group size: 2
we are on item: 0
the contents of this are: 2 2
2 2 is the same as 2 2
already grouped :2 2
groupKey is 0
test group size: 2
we are on item: 1
the contents of this are: 2 2
2 2 is the same as 2 2
already grouped :2 2
groupKey is 0
1 2 outside threshold
3 2 added to group
New group size is: 3
2 1 outside threshold
2 3 added to group
New group size is: 4
put in group number 0


Many thanks!

Eric
7 years ago
Greetings, I am wondering if anyone can give me a nudge with some code I have been debugging since yesterday.

I am sorting pixels stored as int[2] into groups. Each group is an ArrayList<int[]> called group. The groups are collected into a Hashtable called groups.

For each new pixel x,y I want the routine to run through all the groups in the Hashtable and see if x,y is already in one of the groups . If the pixel is already in one of the groups, it breaks this loop and if it is not there, it starts a new group.

My ArrayList appears to think every pixel it comes across is already in the group. I cannot figure it out! Perhaps the problem is visible from this snippet:





Considering pixel: 1 1 on groups size 0
adding to new group: 1 1
New group groupkey: 0
New group size is: 1
...
put in group number 0
Considering pixel: 1 2 on groups size 1
m is now: 0
test group size: 1
item number 0 is 1 2
...
Considering pixel: 2 1 on groups size 1
m is now: 0
test group size: 1
item number 0 is 2 1
...


And so, the routine always thinks the new number and nothing else is in the group. I am totally stumped. Any help would be most welcome. Thank you, Eric.
7 years ago