Java - Chocolate Distribution in A School - Code Review Stack Exchange PDF
Java - Chocolate Distribution in A School - Code Review Stack Exchange PDF
I'd try a more top-down approach. At the moment, you make a lot of internal information of
a Chocolate object available to public in order to do some calculation and decision making
outside that class.
If you model the data/code according to the question, you get a more top-down approach,
which is more abstract, but easier to digest.
You have a chocolate bar in your hand. You can break off a square piece and be left with a different
sized chocolate bar or nothing. You don't really care about the size of the square broken off or
the remainder. All you really care about is how often you can perform that action until there's no
chocolate bar remaining. Of course you have to care about it internally somehow, but again, this is
top-down thinking. Look at how your class (its objects) should be used
Another idea that you can use to your advantage is information hiding. As it turns out, the logic is
often concerned with what's the longer side and what's the shorter one. Then why not store
exactly that information?
Here's a version of Chocolate.java that works with the ideas mentioned above:
https://codereview.stackexchange.com/questions/129599/chocolate-distribution-in-a-school 1/4
9/15/2019 java - Chocolate Distribution in a school - Code Review Stack Exchange
Chocolate chocolate = new Chocolate(6, 3);
int numberOfSquares = 0;
do
{
++numberOfSquares;
chocolate = chocolate.remainderAfterSquareBreakoff()
}
while (chocolate != null);
1. Count how often a square can be broken off until no remainder remains. This is very close to
how you would break the chocolate in real life and is thus hopefully intuitive and easy to
understand.
2. The size of the chocolate is stored in terms of longest and shortest side, not width and length.
You use long descriptive names for your methods, which is good. But you have so much logic
outside of the Chocolate class that you need many such long descriptive names to keep track of
everything. That bloats the code and reduces readability. With only a few things exposed
to public , the code becomes less bloated and you need fewer identifiers.
This is clearly not providing the same functionality that your code has. Most importantly, the
following doesn't hold any more:
By only storing them as longest and shortest side, the orientation is lost. I'd say the orientation is
not necessarily necessary to solve the task at hand. The problem only arises if a Set should be
used to store the Chocolate objects, because you cannot define an equals() method to
distinguish two objects only by their max and min properties. If you put them into a different data
structure that does not require uniqueness like ArrayList for example, everything is fine.
If you insist on uniqueness and the use of Set , you can add another
property isLandscapeFormat which can then be used to distinguish between different orientations.
https://codereview.stackexchange.com/questions/129599/chocolate-distribution-in-a-school 2/4
9/15/2019 java - Chocolate Distribution in a school - Code Review Stack Exchange
The above assumes that you don't care about what square is broken off the bar. It also makes it
necessary to reassign the return value of the method to the chocolate object.
setMinMax(max-min, min);
int numberOfSquares = 0;
while(chocolate.hasNextSquare())
{
++numberOfSquares;
chocolate.getNextSquare();
}
https://codereview.stackexchange.com/questions/129599/chocolate-distribution-in-a-school 3/4
9/15/2019 java - Chocolate Distribution in a school - Code Review Stack Exchange
1. The new method setMinMax , which I introduced because its logic is now necessary at multiple
places in the class. Creating a method prevents duplicated code.
2. The method hasNextSquare . As long as there is still one block remaining, a square can be
broken off. As long as that's the case, this method will return true;
3. getNextSquare , which breaks off the next square from the bar.
4. The while loop to iterate over all the squares:
while(chocolate.hasNextSquare())
{
++numberOfSquares;
chocolate.getNextSquare();
}
The idea is still the same: break off squares as long as that's possible. But now the square is
explicitly returned and can be used in the program.
There are more fancy ways to create iterators, with an interface to be implemented, but for this
simple example, I think it is sufficient to provide a hasNext() and getNext() method, which
implicitly removes the returned object.
https://codereview.stackexchange.com/questions/129599/chocolate-distribution-in-a-school 4/4