tag:blogger.com,1999:blog-7850692473012665527.post8739296074414045975..comments2023-10-18T05:07:16.474-04:00Comments on the "How Can Renee Make This Code Better?" blog: FindLargest ProblemHello, I'm Renee.http://www.blogger.com/profile/02081872116763616695noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7850692473012665527.post-86176107615682432422010-02-28T16:26:50.991-05:002010-02-28T16:26:50.991-05:00I was thinking more of something like this:
...
i...I was thinking more of something like this:<br /><br />...<br />int value = readInt("Enter integer:");<br />int top = value;<br />while (value!=SENTINEL){<br /> value = readInt("Enter integer:");<br /> if (value>top) top = value;<br />}<br />...Shridhar Jayanthihttps://www.blogger.com/profile/04470784837246826480noreply@blogger.comtag:blogger.com,1999:blog-7850692473012665527.post-32961599178324327142010-02-28T14:39:43.143-05:002010-02-28T14:39:43.143-05:00Hmm. Do you mean this?
import acm.program.*;
...Hmm. Do you mean this?<br /><br />import acm.program.*;<br /><br /> int top = readint;<br /> <br /> while (true){<br /> int value = readInt("Enter integer: ");<br /> if (value == SENTINEL) break;<br /><br /> if (value >= top) top = value;<br /> <br /> }<br /> <br /> println("The highest is " + top + ".");<br /> <br /> }<br /> //private constants<br /> private static final int SENTINEL = 0;<br /><br /><br />}<br /><br />If we start with "int top = readint", then what if the person enters 0 first, and then they enter another number and keep going? That's a problem because they've entered the sentinel, but the program didn't stop like it should! (Let me know if this isn't what you meant, shridhar :)<br /><br />I think the right way to do it is to initialize "top" as zero. I had been wary of using the sentinel as the initial value, but that's silly because the value is set OUTSIDE the while loop, the the point of the sentinel is only to break us out of the while loop.<br /><br />So, here is my revised code; i think it is bug-free:<br /><br />import acm.program.*;<br /><br />public class FindLargest extends ConsoleProgram{<br /> public void run () {<br /> println ("This program finds the largest of a list of integers.");<br /> println("Enter integers, one per line, entering 0 to signify the end of the list.");<br /> <br /> int top = 0;<br /> <br /> while (true){<br /> int value = readInt("Enter integer: ");<br /> if (value == SENTINEL) break;<br /><br /> if (value >= top) top = value;<br /> <br /> }<br /> <br /> println("The highest is " + top + ".");<br /> <br /> }<br /> //private constants<br /> private static final int SENTINEL = 0;<br /><br /><br />}Hello, I'm Renee.https://www.blogger.com/profile/02081872116763616695noreply@blogger.comtag:blogger.com,1999:blog-7850692473012665527.post-41987591952828487862010-02-25T20:06:04.762-05:002010-02-25T20:06:04.762-05:00Instead of initializing top with -1, you can initi...Instead of initializing top with -1, you can initialize with the first input. That way your program would work for negative integers too. That would also remove the "highest is -1" answer.Shridhar Jayanthihttps://www.blogger.com/profile/04470784837246826480noreply@blogger.comtag:blogger.com,1999:blog-7850692473012665527.post-48443391458600314112010-02-24T16:35:19.024-05:002010-02-24T16:35:19.024-05:00hmm, what if the user only enters 0? then we get a...hmm, what if the user only enters 0? then we get an answer, "the highest is -1"; crappers.Hello, I'm Renee.https://www.blogger.com/profile/02081872116763616695noreply@blogger.com