Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it better to use a for-each loop in a function, or a while loop and a “found” variable?
    primarykey
    data
    text
    <p> </p> <p>I'm taking an online intro-to-Java course. In the lesson on <code>ArrayList</code>s, there's an exercise in which we're given an <code>ArrayList</code> of <code>Picture</code> objects, and asked to call <code>draw()</code> on the first <code>Picture</code> that's in portrait orientation. We're given some code to start with:</p> <pre class="lang-java prettyprint-override"><code>// BlueJ project: lesson7/gallery8 // Find and draw the first portrait in the gallery. // Notice the pseudocode from the instructions has been started for you; your task is to complete it. import java.util.ArrayList; public class ListOfPictures { public static void main(String[] args) { ArrayList&lt;Picture&gt; gallery = new ArrayList&lt;Picture&gt;(); gallery.add(new Picture("degas1.jpg")); gallery.add(new Picture("gaugin1.jpg")); gallery.add(new Picture("monet1.jpg")); gallery.add(new Picture("monet2.jpg")); gallery.add(new Picture("renoir1.jpg")); int i = 0; boolean found = false; while () { } if (found) { ....draw(); } } } </code></pre> <p>It seemed pretty clear to me what they expected us to do. Still, I thought it would be better to make a function and <code>return</code> from a for-each loop, rather than using a <code>found</code> and a counter variable. This is what I came up with:</p> <pre class="lang-java prettyprint-override"><code> public static void main(String[] args) { // declare/initialize/populate gallery Picture firstPortrait = findFirstPortrait(gallery); if (firstPortrait != null) firstPortrait.draw(); } private Picture findFirstPortrait(ArrayList&lt;Picture&gt; gallery) { for (Picture pic : gallery) if (pic.getHeight() &gt; pic.getWidth()) // pic is in portrait return pic; return null; // there aren't any images in portrait } </code></pre> <p>They seemed to really dislike it, though. When I submitted my code, I got this back:</p> <pre class="lang-none prettyprint-override"><code>ListOfPictures.java: Keep looking for matches while found is false Score 0 </code></pre> <p>Which is the better way of doing this? In general, should I avoid using “flag” variables like that?</p> <p><strong>EDIT:</strong> Yes, it is automatically graded. I've actually gotten an error on other exercises saying that the grader didn't know how to deal with my code and that I should post it on the forum (which looks suspiciously like Stack Overflow, only that no one seems to know <a href="https://stackoverflow.com/about">how to use it</a>).</p> <p>I <em>will</em> be linking to this question on the forum. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload