0% found this document useful (0 votes)
206 views

Java - Chocolate Distribution in A School - Code Review Stack Exchange PDF

The document discusses different approaches to modeling the distribution of chocolate squares in a Java program. It suggests using a more top-down, abstract approach where the Chocolate class focuses on breaking off squares rather than internal details. Two alternative Chocolate class implementations are provided: one that counts squares broken off in a loop, and another that uses an iterator pattern with hasNext() and getNext() methods.

Uploaded by

Sayantan Pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views

Java - Chocolate Distribution in A School - Code Review Stack Exchange PDF

The document discusses different approaches to modeling the distribution of chocolate squares in a Java program. It suggests using a more top-down, abstract approach where the Chocolate class focuses on breaking off squares rather than internal details. Two alternative Chocolate class implementations are provided: one that counts squares broken off in a loop, and another that uses an iterator pattern with hasNext() and getNext() methods.

Uploaded by

Sayantan Pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9/15/2019 java - Chocolate Distribution in a school - Code Review Stack Exchange

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.

Your method numberOfChildrenThatCanBeFedFromTheChocolate for example deals with blocks of


chocolate. A block of chocolate is the atomic element a chocolate bar is made from. The problem
here is that the question doesn't ask for chocolate blocks. Sure, the number of blocks and how
they are positioned in a grid determines how often a square can be broken off the chocolate bar,
but thinking about it in terms of blocks is a bottom-up way of thinking. I as the user of your class
could not care less about blocks. The problem asks for squares, not blocks. Why do I have to deal
with blocks when using your 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:

public class Chocolate


{
private int min;
private int max;

public Chocolate(int width, int height)


{
min = Math.min(width, height);
max = Math.max(width, height);
}

public Chocolate remainderAfterSquareBreakoff()


{
if ((min == 1 && max == 1) || min == max)
{
return null;
}

return new Chocolate(max - min, min);


}

public static void main(String[] args)


{

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);

System.out.println("# squares: " + numberOfSquares);


}
}

The two important things are:

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:

Each Chocolate bar in carton is unique in length (i) and breath(j).

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.

public class Chocolate


{
private int min;
private int max;

private boolean isLandscapeFormat;

public Chocolate(int width, int height)


{
min = Math.min(width, height);
max = Math.max(width, height);

isLandscapeFormat = width > height;


}

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

And now for something completely different.

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.

A more common approach for this call-method-until-null-is-returned structure is an iterator.

Here's a different version of Chocolate.java that incorporates that principle.

public class Chocolate


{
private int min;
private int max;

public Chocolate(int width, int height)


{
setMinMax(width, height);
}

private void setMinMax(int a, int b)


{
min = Math.min(a, b);
max = Math.max(a, b);
}

public boolean hasNextSquare()


{
return min > 0 && max > 0;
}

public Chocolate getNextSquare()


{
if (!hasNextSquare())
{
return null;
}

setMinMax(max-min, min);

return new Chocolate(min, min);


}

public static void main(String[] args)


{
Chocolate chocolate = new Chocolate(5, 3);

int numberOfSquares = 0;

while(chocolate.hasNextSquare())
{
++numberOfSquares;

chocolate.getNextSquare();
}

System.out.println("# squares: " + numberOfSquares);


}
}

The important differences are:

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

You might also like