patternjavaMinor
Bell Triangle computation in Java
Viewed 0 times
javatrianglebellcomputation
Problem
In mathematics, the Bell triangle is a triangle of numbers analogous to Pascal's triangle, whose values count partitions of a set in which a given element is the largest singleton. It is named for its close connection to the Bell numbers, which may be found on both sides of the triangle, and which are in turn named after Eric Temple Bell.
(Wikipedia article)
I have a model class for a Bell Triangle together with a test class below. Similar to my previous question, I have chosen the less conventional method of representing my standard and exception test cases using
I would also like to explain first the following line in my
To compute the next field of the current row, we need to sum up the current field of the current row and the same field of the previous row. The only reason I ended up with the final version is to arguably make the formula look more symmetric. Have I taken things a little too far, and perhaps stick with the original form?
I also have a second question with regards to my
Please review the Javadocs and the overall readability of code+test too, as I will like to know if I am being too verbose in these departments.
```
/**
* Computes a Bell Triangle.
*
* A Bell
(Wikipedia article)
I have a model class for a Bell Triangle together with a test class below. Similar to my previous question, I have chosen the less conventional method of representing my standard and exception test cases using
Enums, only this time my test methods specify the exact inputs for testing.I would also like to explain first the following line in my
compute() method and ask a follow-up question about it:result[i][j + 1] = result[--i][j] + result[++i][j];i represents the current row, and j the current field. The original form is probably easier to understand:result[i][j + 1] = result[i - 1][j] + result[i][j];To compute the next field of the current row, we need to sum up the current field of the current row and the same field of the previous row. The only reason I ended up with the final version is to arguably make the formula look more symmetric. Have I taken things a little too far, and perhaps stick with the original form?
I also have a second question with regards to my
displayTriangle() method in my BellTriangleTest class. I am using Java 8 streams to map each triangle row, with each row of long values (the triangle is a long[][] array after all) being streamed as a LongStream to be in turn collected into a String via Collectors.joining(", "). The String output for each triangle row is then consumed by log::debug. Is there a more fluent way to do this?Please review the Javadocs and the overall readability of code+test too, as I will like to know if I am being too verbose in these departments.
BellTriangle```
/**
* Computes a Bell Triangle.
*
* A Bell
Solution
-
-
-
You should probably be more verbose on the
-
Don't implement
-
In
-
Maybe you should not have the method
-
I don't really see the need for
-
You should move the code that outputs to console and the
result[i][j + 1] = result[--i][j] + result[++i][j]; Don't ever use something like that.- You can get into very serious trouble if you modify the code slightly and your
--are not well "aligned" with your++.
- It's less efficient because you are making two useless assignments.
-
final long[][] result = new long[++n][]; Same thing as above. That's very dangerous. Just use n + 1.-
You should probably be more verbose on the
LIMIT variable name.-
Don't implement
Cloneable. That interface was just a big mistake. It is deprecated. If you need that functionality just define a copy constructor.-
In
getTriangle(), instead of defining i and looping over the rows, it would be simpler to just loop over i.-
Maybe you should not have the method
compute, but just do that work in the constructor. But that's arguable. If you keep it as a separate method, you should make it static.-
I don't really see the need for
ROW_ZERO. I think it would be simpler to just define that in compute.-
You should move the code that outputs to console and the
main method outside of the test file.Context
StackExchange Code Review Q#63488, answer score: 5
Revisions (0)
No revisions yet.