<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7850692473012665527</id><updated>2012-01-28T20:42:11.548-05:00</updated><category term='average'/><category term='temp variables'/><category term='sentinel'/><category term='while loop'/><category term='fib sequence'/><title type='text'>the "How Can Renee Make This Code Better?" blog</title><subtitle type='html'>Sharing code from beginner CS class. Please leave a comment if you have ways to make them better!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>39</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2750031202177901887</id><published>2011-12-29T14:46:00.000-05:00</published><updated>2011-12-29T15:06:01.294-05:00</updated><title type='text'>Program to Make the Plural of a Word</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Problem:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="p1"&gt;Write a method &lt;span class="s1"&gt;&lt;b&gt;regularPluralForm(word) &lt;/b&gt;&lt;/span&gt;that returns the plural of &lt;span class="s1"&gt;&lt;b&gt;word &lt;/b&gt;&lt;/span&gt;formed by following these standard English rules:&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;a) If the word ends in &lt;i&gt;s, x, z, ch, &lt;/i&gt;or &lt;i&gt;sh, &lt;/i&gt;add &lt;i&gt;es &lt;/i&gt;to the word.&amp;nbsp;&lt;/div&gt;&lt;div class="p1"&gt;b) If the word ends in &lt;i&gt;y &lt;/i&gt;and the &lt;i&gt;y &lt;/i&gt;is preceded by a consonant, change the &lt;i&gt;y &lt;/i&gt;to &lt;i&gt;ies.&amp;nbsp;&lt;/i&gt;&lt;/div&gt;&lt;div class="p1"&gt;c) In all other cases, add just an &lt;i&gt;s.&lt;/i&gt;&lt;/div&gt;&lt;div class="p1"&gt;Write a test program and design a set of test cases to verify that your program works.&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;(Roberts ch 10, problem 7).&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;What it looks like:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;pre class="brush:java;"&gt;/*File: MakePlural.java&lt;br /&gt; * ---------------------&lt;br /&gt; * This program lets the user enter in any word and it will print out the plural form according to the basic,&lt;br /&gt; * not totally comprehensive rules outlined in the problem from ch. 10. It makes use of the private methods&lt;br /&gt; * makePlural (which take in a string and returns another string, the plural form of the word).&lt;br /&gt; */&lt;br /&gt;import acm.program.ConsoleProgram;&lt;br /&gt;public class MakePlural extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;       while (true){&lt;br /&gt;           String word = readLine("Enter word and we'll make it plural. Enter '0' to stop: ");&lt;br /&gt;           if (word.equals("0")){&lt;br /&gt;               break;&lt;br /&gt;           }&lt;br /&gt;       print(makePlural(word) + " ");&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    //Takes the submitted string, inspects the last letter (or last 2 letters in case of "ch" and "sh"),&lt;br /&gt;    //and returns the appropriate plural form.&lt;br /&gt;    private String makePlural (String singularWord){&lt;br /&gt;        String pluralWord = "";&lt;br /&gt;        String strippedWord = singularWord.substring(0, singularWord.length()-1);&lt;br /&gt;        char lastLetter = singularWord.charAt(singularWord.length()-1);&lt;br /&gt;        switch (lastLetter){&lt;br /&gt;            case 's':&lt;br /&gt;            case 'x':&lt;br /&gt;            case 'z':&lt;br /&gt;                pluralWord = singularWord + "es";&lt;br /&gt;                break;&lt;br /&gt;            case 'h': // checking for if the word ends with "ch" or "sh"&lt;br /&gt;                if ((singularWord.charAt(singularWord.length()-2)== 'c') || (singularWord.charAt(singularWord.length()-2)== 's')) {&lt;br /&gt;                    pluralWord = singularWord + "es";&lt;br /&gt;                    break;&lt;br /&gt;                } &lt;br /&gt;            case 'y':&lt;br /&gt;                if (isEnglishConsonant(singularWord.charAt(singularWord.length()-2))) {&lt;br /&gt;                    pluralWord = strippedWord + "ies";&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;            default: pluralWord = singularWord + "s";&lt;br /&gt;            break;&lt;br /&gt;        }&lt;br /&gt;        return pluralWord;&lt;br /&gt;    }&lt;br /&gt;    private boolean isEnglishConsonant(char ch) {&lt;br /&gt;        switch (Character.toLowerCase(ch)) {&lt;br /&gt;            case 'a': case 'e': case 'i': case 'o': case 'u': &lt;br /&gt;                return false;&lt;br /&gt;            default: &lt;br /&gt;                return true;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-WXJDqHrlsAo/TvzAI6nSLAI/AAAAAAAAAjg/Vc5ev8ZHmt4/s1600/MakePlural.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="202" src="http://4.bp.blogspot.com/-WXJDqHrlsAo/TvzAI6nSLAI/AAAAAAAAAjg/Vc5ev8ZHmt4/s640/MakePlural.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;What made it tricky:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The program is structured to work well as a "switch" statement, and I had wanted to create a neat table that took a substring "lastLetter" (last 2 letters in the case of "ch" and "sh") and neatly return the proper plural form. &amp;nbsp;However you can't switch on a string as I learned-- damn! So had to use the character only and switch on that, which made checking for "ch" and "sh" slightly less convenient.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Come to think of it, why *can't* you switch on a string? You'd think that it's a similar situation to comparing strings via "if (s1 == s2)", namely that it would often give you the wrong answer since they're comparing actual objects, not values. However I got an error when I attempted...anyone know why?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Another bug I ran into came from&amp;nbsp;attempting&amp;nbsp;to&amp;nbsp;inspect&amp;nbsp;the last letter and second-to-last-letter by doing charAt(-1) or charAt(-2) and making a substring of all but the last letter of the original word by doing "singularWord.substring(0, -1)". Those were throwing out-of-bounds errors. Oops-- sure would have been&amp;nbsp;convenient.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The main bug I got stuck on before it worked is that it kept giving me the&amp;nbsp;default&amp;nbsp;case. So "sky" gave me "skys", "box" gave me "boxs" etc. I didn't know why and whether the non-default case was even getting run so I added debugging lines to print out the plural form as soon as it gets run.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-ZyXVtrC_1BQ/TvzDz6V-86I/AAAAAAAAAjs/n39SaqbKMKg/s1600/Skitch-2-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-ZyXVtrC_1BQ/TvzDz6V-86I/AAAAAAAAAjs/n39SaqbKMKg/s1600/Skitch-2-1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;It appeared that the special cases were getting run, but the default was getting run every time. Why?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;I suppose I needed to add "break" statements to my special cases in the "switch" statement. I looked up confirmation for this in the text and this is what it said:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;"Java is defined so that if the break statement is missing, the program starts executing statements from the next clause after it finishes the selected one. While this design can be useful in some cases, it tends to cause more problems than it solves. To reinforce the importance of remembering to include the break statement, every case clause in this text ends with an explicit break statement (or sometimes with a return statement, as discussed in Chapter 5)."&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;That kind of makes sense... depending on what the syntax means when it checks for "default," it's posible that "default" means every single case *in addition to* the special cases that do apply. So yes, once I did add a "break" statement to each case, the problem worked correctly.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;It's weird though because one example in the book does a simple switch statement with a default, but does not have a break statement after the special cases:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;pre class="brush:java;"&gt;private boolean isEnglishVowel(char ch) {&lt;br /&gt;     switch (Character.toLowerCase(ch)) {&lt;br /&gt;         case 'a': case 'e': case 'i': case 'o': case 'u':&lt;br /&gt;            return true;&lt;br /&gt;         default: &lt;br /&gt;            return false;&lt;br /&gt;    }&lt;br /&gt;}   &lt;/pre&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;I can confirm that this private method works correctly since I used it in my own program. So I'm not quite sure when a break statement is required. I do realize that the author may not have meant to include a confusing example in the final version of the book, so I'll check this PDF against my hard copy when I'm back home. Any ideas on this switch statement&amp;nbsp;conundrum&amp;nbsp;however?&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2750031202177901887?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2750031202177901887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-make-plural-of-word.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2750031202177901887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2750031202177901887'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-make-plural-of-word.html' title='Program to Make the Plural of a Word'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-WXJDqHrlsAo/TvzAI6nSLAI/AAAAAAAAAjg/Vc5ev8ZHmt4/s72-c/MakePlural.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-7607876622135767733</id><published>2011-12-28T18:53:00.000-05:00</published><updated>2011-12-28T18:59:09.855-05:00</updated><title type='text'>Program to Determine the Ordinal Value of a Number</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Problem:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Like most other languages, English include two types of numbers: &lt;/span&gt;&lt;b style="font-family: 'Trebuchet MS', sans-serif;"&gt;cardinal numbers &lt;/b&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;(such as &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;one, two, three, &lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;and &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;four&lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;) that are used in counting, and &lt;/span&gt;&lt;b style="font-family: 'Trebuchet MS', sans-serif;"&gt;ordinal numbers &lt;/b&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;(such as &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;first, second, third, &lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;and &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;fourth&lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;) that are used to indicate a position in a sequence. In numeric form, ordinals are usually indicated by writing the digits in the number, followed by the last two letters of the English word that names the corresponding ordinal. Thus, the ordinal numbers &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;first, second, third, &lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;and &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;fourth &lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;often appear in print as &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;1st, 2nd, 3rd, &lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;and &lt;/span&gt;&lt;i style="font-family: 'Trebuchet MS', sans-serif;"&gt;4th&lt;/i&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The general rule for determining the suffix of an ordinal can be defined as follows:&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Numbers ending in the digit 1, 2, and 3, take the suffixes &lt;span class="s1"&gt;&lt;b&gt;"st"&lt;/b&gt;&lt;/span&gt;, &lt;span class="s1"&gt;&lt;b&gt;"nd"&lt;/b&gt;&lt;/span&gt;, and &lt;span class="s1"&gt;&lt;b&gt;"rd"&lt;/b&gt;&lt;/span&gt;, respectively, unless the number ends with the two- digit combination 11, 12, or 13. Those numbers, and any numbers not ending with a 1, 2, or 3, take the suffix &lt;span class="s1"&gt;&lt;b&gt;"th"&lt;/b&gt;&lt;/span&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Your task in this problem is to write a function &lt;span class="s1"&gt;&lt;b&gt;ordinalForm(n) &lt;/b&gt;&lt;/span&gt;that takes an integer &lt;span class="s1"&gt;&lt;b&gt;n &lt;/b&gt;&lt;/span&gt;and returns a string indicating the corresponding ordinal number.&amp;nbsp; (Roberts ch 10, problem 9).&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;What it looks like:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;pre class="brush:java;"&gt;/* File: ordinalForm.java&lt;br /&gt; * -----------------&lt;br /&gt; *This console program lets the user enter in any integer and will print out the ordinal form of that number. For example&lt;br /&gt; *the ordinal form of 1 is "1st," 2 is "2nd" etc. The user enters the sentinel value "-1" to stop running the program.&lt;br /&gt; *&lt;br /&gt; *The program and the method of the same name takes an integer as the entered value and returns a string. We use the ACM library's (?)&lt;br /&gt; *built-in Integer class and its toString method. Because the suffix of an ordinal value depends on the last digit&lt;br /&gt; *of a number our program has to strip out and inspect the last digit of the supplied integer so we use the method &lt;br /&gt; *"getLastDigit" to inspect the last digit. In the case of numbers ending with "11", "12" and "13" we need to see the last&lt;br /&gt; **two* digits so we also use a similiarly-constructed getSecondToLastDigit.&lt;br /&gt; */&lt;br /&gt;import acm.program.ConsoleProgram;&lt;br /&gt;public class ordinalForm extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;       while (true){&lt;br /&gt;       int cardinal = readInt("Enter number and we'll give you the ordinal form: ");&lt;br /&gt;       if (cardinal == -1){&lt;br /&gt;           break;&lt;br /&gt;       }&lt;br /&gt;       print(makeOrdinal(cardinal));&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;    private String makeOrdinal(int n) {&lt;br /&gt;        String ordinal = "";&lt;br /&gt;        int lastDigit = getLastDigit(n);&lt;br /&gt;        int secondToLastDigit = getSecondToLastDigit(n);&lt;br /&gt;        if ((lastDigit == 1) &amp;amp;&amp;amp; secondToLastDigit !=(1)){&lt;br /&gt;            ordinal = Integer.toString(n) + "st"; //Not sure why it's "Integer.toString(n)" instead of "n.toString" or "toString(n)"&lt;br /&gt;        }&lt;br /&gt;        else if (lastDigit ==2 &amp;amp;&amp;amp; secondToLastDigit !=(1)) {&lt;br /&gt;            ordinal = Integer.toString(n) + "nd"; &lt;br /&gt;        }&lt;br /&gt;        else if (lastDigit == 3 &amp;amp;&amp;amp; secondToLastDigit !=(1)) {&lt;br /&gt;            ordinal = Integer.toString(n) + "rd";&lt;br /&gt;        }&lt;br /&gt;        else ordinal = Integer.toString(n) + "th";&lt;br /&gt;        return ordinal;&lt;br /&gt;    }&lt;br /&gt;    private int getLastDigit(int n) {&lt;br /&gt;        int remainder = n % 10;&lt;br /&gt;        return remainder;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private int getSecondToLastDigit(int n) {&lt;br /&gt;        int remainder = 0;&lt;br /&gt;        for (int i = 0; i &amp;lt; 2; i++){&lt;br /&gt;            remainder = n % 10;&lt;br /&gt;            n /= 10;&lt;br /&gt;        }&lt;br /&gt;        return remainder;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;}&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-MzLfDnLONbE/TvutPufXemI/AAAAAAAAAjU/gHaUQKsV6gA/s1600/Shared+Menubar.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="232" src="http://3.bp.blogspot.com/-MzLfDnLONbE/TvutPufXemI/AAAAAAAAAjU/gHaUQKsV6gA/s640/Shared+Menubar.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;W&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;hat made it tricky:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;My first version of this was running just fine....it just kept returning the wrong answer. Have a look at this sample run when the bug existed and guess what the problem was:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Wh1qzifLVYk/Tvus-5fs4gI/AAAAAAAAAjI/PKS1Z5gcF5Q/s1600/Shared+Menubar-13.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="558" src="http://3.bp.blogspot.com/-Wh1qzifLVYk/Tvus-5fs4gI/AAAAAAAAAjI/PKS1Z5gcF5Q/s640/Shared+Menubar-13.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Here's a clue: this is what my private methods&amp;nbsp;getSecondToLastDigit(n) and&amp;nbsp;getLastDigit(n) looked like:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;    private int getLastDigit(int n) {&lt;br /&gt;        int remainder = 0;&lt;br /&gt;        while (n &amp;gt; 0) {&lt;br /&gt;            remainder = n % 10;&lt;br /&gt;            n /= 10;&lt;br /&gt;        }&lt;br /&gt;        return remainder;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private int getSecondToLastDigit(int n) {&lt;br /&gt;        int remainder = 0;&lt;br /&gt;        while (n &amp;gt; 10) {&lt;br /&gt;            remainder = n % 10;&lt;br /&gt;            n /= 10;&lt;br /&gt;        }&lt;br /&gt;        return remainder;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;So while&amp;nbsp;getLastDigit(n)&amp;nbsp;should have just divided by n *once* and returned the remainder, instead it kept dividing by n until there was nothing left. So it was really stripping off and returning the *first* digit, not the last!&amp;nbsp;getSecondToLastDigit(n) was behaving the same way, except it stopped one iteration sooner so it returned the second digit, not the second-to-last. Ugh, so&amp;nbsp;embarrassing, blame it on the Christmas food abundance.&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;I didn't see the pattern or understand what was wrong for a while so I made this quick debugging program that printed out the results of just getLastDigit(n):&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;import acm.program.ConsoleProgram;&lt;br /&gt;public class testLastDigit extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;       int n = readInt("Enter an int and we'll give you the last digit");&lt;br /&gt;       print (getLastDigit(n));&lt;br /&gt;    }&lt;br /&gt;    private int getLastDigit(int n) {&lt;br /&gt;        int remainder = 0;&lt;br /&gt;        while (n &amp;gt; 0) {&lt;br /&gt;            remainder = n % 10;&lt;br /&gt;            n /= 10;&lt;br /&gt;        }&lt;br /&gt;        return remainder;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;I ran it a bunch of times and saw that indeed,&amp;nbsp;testLastDigit &amp;nbsp;was giving me the wrong answer.&amp;nbsp;Once I saw that it was wrong and predictably wrong, the fix was easy.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-7607876622135767733?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/7607876622135767733/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-determine-ordinal-value-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7607876622135767733'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7607876622135767733'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-determine-ordinal-value-of.html' title='Program to Determine the Ordinal Value of a Number'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-MzLfDnLONbE/TvutPufXemI/AAAAAAAAAjU/gHaUQKsV6gA/s72-c/Shared+Menubar.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-1491923126077837349</id><published>2011-12-17T19:13:00.001-05:00</published><updated>2011-12-17T19:13:15.548-05:00</updated><title type='text'>Program to Search for a Substring inside a String</title><content type='html'>&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Problem:&amp;nbsp;If the designers of the String class had not defined the version of &lt;/span&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;indexOf&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt; that takes a string argument, you could implement it using the other methods available in the library.&amp;nbsp;Without calling &lt;/span&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;indexOf&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt; directly, implement a method &lt;/span&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;myIndexOf&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt; that behaves in exactly the same way.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;What it looks like:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-5riEUCvZYjU/Tu0lyy_jW6I/AAAAAAAAAis/tmbDof9EjXM/s1600/myIndexOfTester.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-5riEUCvZYjU/Tu0lyy_jW6I/AAAAAAAAAis/tmbDof9EjXM/s1600/myIndexOfTester.jpg" /&gt;&lt;/a&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/* File: myIndexOfTester.java&lt;br /&gt; * ---------------&lt;br /&gt; * This program has a run method that lets the user enter in a (supposed to be) big string and a&lt;br /&gt; * little string. Then it uses the private method myIndexOf to see whether the little string exists&lt;br /&gt; * inside the big string and if so, return the index within the big string that the little string&lt;br /&gt; * exists at.&lt;br /&gt; * &lt;br /&gt; * There is already an indexOf method in Java's standard String class, but the Stanford folks &lt;br /&gt; * thought it would be good for us to try implementing it ourselves with other methods in the&lt;br /&gt; * String class.&lt;br /&gt; */&lt;br /&gt;import acm.program.*;&lt;br /&gt;public class myIndexOfTester extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;       String enteredString = readLine("Enter a string: ");&lt;br /&gt;       String searchedForString = readLine("Enter the string you're searching for inside it: ");&lt;br /&gt;       print(myIndexOf(searchedForString, enteredString));&lt;br /&gt;    }&lt;br /&gt;    private int myIndexOf(String littleString, String bigString) {&lt;br /&gt;//"Real" program would check that littleString is longer than bigString &amp;amp; raise and exception of it weren't&lt;br /&gt;&lt;br /&gt;//For every letter in bigString MINUS the length of littleString (to guard against out-of-bounds&lt;br /&gt;//errors), check if littleString equals the segment of bigString that starts at that index.&lt;br /&gt;        for (int i = 0; i &amp;lt; bigString.length()-1-littleString.length()-1; i++) {      &lt;br /&gt;            if (littleString.equals(bigString.substring(i, i+littleString.length() ))) {&lt;br /&gt;                return i;&lt;br /&gt;            }           &lt;br /&gt;        }&lt;br /&gt;        return -1; //Signifies that littleString doesn't exist inside bigString         &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The basic gist of this problem, or at least my approach to it, is you want to take your little string and compare it to substrings inside the big string. In particular, you want to iterate through each letter of the big string and carve out a substring starting at that index and of an equal length to the little string. If they are equivalent, then return the index. If you never get an equivalency, then return -1 (as is the existing convention).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;What made it tricky:&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Two things! 1) Off-by-one errors, and 2) Out-of-bounds errors.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;** Off-by-one Errors **&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Let's say your big string is "HELLO WORLD". This string has 11 characters, including the space, so the method bigString.length() would return 11. If you iterated from 0 to 11, however, your last iteration would be looking at nothing since there's no character at space "11"! Therefore, you have to make the outer bound of your loop is the length of the big string minus one.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-5L58U19NcaA/Tu0sLF0RNfI/AAAAAAAAAi0/gVmBSaoW5NQ/s1600/offByOne.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://4.bp.blogspot.com/-5L58U19NcaA/Tu0sLF0RNfI/AAAAAAAAAi0/gVmBSaoW5NQ/s320/offByOne.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;** Out-of-bounds Errors **&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;Let's say your big string is "HELLO", and the little string you're inspecting "HELLO" for is "HI." You go thru every letter in "HELLO," seeing if the 2-letter substring at that letter is equivalent to "HI".&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;However, what happens on the last couple of iterations of your loop? If your index is the letter "O" in "HELLO", you're comparing "HI" with "O" and then nothing! To keep this from happening, you need to stop your comparisons so that all the letters in "HI" have something in the string "HELLO" to get compared to.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-UIhzcAhyHcs/Tu0tBwh0ezI/AAAAAAAAAi8/vQjlxZ-9mys/s1600/outOfBounds.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="300" src="http://2.bp.blogspot.com/-UIhzcAhyHcs/Tu0tBwh0ezI/AAAAAAAAAi8/vQjlxZ-9mys/s400/outOfBounds.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;The last tricky thing is, the problem specified that the method has to behave the *same* way as the existing indexOf. That takes in just one argument (the little string) and you apply it to the big string. My implementation takes in both the big string and the little string as arguments, so I had been worried that I hadn't actually solved the requirement.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Trebuchet MS', sans-serif;"&gt;However, how is it possible to write your own&amp;nbsp;implementation as a method by itself&amp;nbsp;without "knowing" the length of the big string? Not sure whether this is do-able.... I checked the textbook to see whether the authors printed this problem. (I have to confess that I've been studying with a PDF of the pre-print copy of the textbook since it's easier to have on hand, and I leave the heavy text at home). I saw that the printed version omitted this problem, so I'm assuming that the editors decided that it wasn't quite a perfectly designed problem because of that mismatch. Thoughts?&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-1491923126077837349?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/1491923126077837349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-search-for-substring-inside.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1491923126077837349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1491923126077837349'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-search-for-substring-inside.html' title='Program to Search for a Substring inside a String'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-5riEUCvZYjU/Tu0lyy_jW6I/AAAAAAAAAis/tmbDof9EjXM/s72-c/myIndexOfTester.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-6013084185279712922</id><published>2011-12-12T01:27:00.001-05:00</published><updated>2011-12-12T01:54:27.147-05:00</updated><title type='text'>Program to Calculate the Scrabble Score of a Word</title><content type='html'>&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;Problem:&lt;/div&gt;&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;In most word games, each letter in a word is scored according to its point value, which is inversely proportional to its frequency in English words. In ScrabbleTM, the points are allocated as follows:&lt;br /&gt;&lt;img alt="" src="data:image/png;base64,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" /&gt;&lt;br /&gt;For example, the Scrabble word "FARM" is worth 9 points: 4 for the F, 1 each for the A and the R, and 3 for the M. Write a ConsoleProgram that reads in words and prints out their score in Scrabble, not counting any of the other bonuses that occur in the game. You should ignore any characters other than uppercase letters in computing the score. In particular, lowercase letters are assumed to represent blank tiles, which can stand for any letter but which have a score of 0. (Robers ch 9, problem 5)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-coRZVzVVyOU/TuWhMagfVXI/AAAAAAAAAik/yqif1L4fk8I/s1600/ScrabbleCalc.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-coRZVzVVyOU/TuWhMagfVXI/AAAAAAAAAik/yqif1L4fk8I/s1600/ScrabbleCalc.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre class="brush:java;"&gt;/* File: ScrabbleCalc&lt;br /&gt; * -----------------&lt;br /&gt; * This program takes in an arbitrary word from the end-user and calculates its value according to the points alloted&lt;br /&gt; * to each letter by the game Scrabble. Characters must be upper-case; lowercase letters are ignored.&lt;br /&gt; * &lt;br /&gt; * The program makes use of the private method 'scabbleScore' that takes in the input string and iterates through&lt;br /&gt; * each charcter. The method also establishes the integer 'score,' an instance variable that keeps track of the&lt;br /&gt; * accumulating points for the word. In each loop, it asses character via a switch statement and adds the appropriate &lt;br /&gt; * points value for the character to 'score.'&lt;br /&gt; */&lt;br /&gt;import acm.program.*;&lt;br /&gt;public class ScrabbleCalc extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;       String word = readLine("Enter Scrabble word (all in uppercase) and we'll calculate your score: ");&lt;br /&gt;       print(scrabbleScore(word));&lt;br /&gt;    }&lt;br /&gt;    private int scrabbleScore(String scrabbleWord) {&lt;br /&gt;        int score = 0;&lt;br /&gt;        for (int i = 0; i &amp;lt; scrabbleWord.length(); i++){&lt;br /&gt;            char calculatedLetter = scrabbleWord.charAt(i);&lt;br /&gt;            switch (calculatedLetter) {&lt;br /&gt;                case 'A':&lt;br /&gt;                case 'E':&lt;br /&gt;                case 'I':&lt;br /&gt;                case 'L':&lt;br /&gt;                case 'N':&lt;br /&gt;                case 'O':&lt;br /&gt;                case 'R':&lt;br /&gt;                case 'S':&lt;br /&gt;                case 'T':&lt;br /&gt;                case 'U': //Jesus this is fugly&lt;br /&gt;                    score +=1; break;&lt;br /&gt;                case 'D':&lt;br /&gt;                case 'G':&lt;br /&gt;                    score +=2; break;&lt;br /&gt;                case 'B':&lt;br /&gt;                case 'C':&lt;br /&gt;                case 'M':&lt;br /&gt;                case 'P':&lt;br /&gt;                    score +=3; break;&lt;br /&gt;                case 'F':&lt;br /&gt;                case 'H':&lt;br /&gt;                case 'V':&lt;br /&gt;                case 'W':&lt;br /&gt;                case 'Y':&lt;br /&gt;                    score +=4; break;&lt;br /&gt;                case 'K':&lt;br /&gt;                    score +=5; break;&lt;br /&gt;                case 'J':&lt;br /&gt;                case 'X':&lt;br /&gt;                    score +=8; break;&lt;br /&gt;                case 'Q':&lt;br /&gt;                case 'Z':&lt;br /&gt;                    score +=10; break;&lt;br /&gt;                default: break;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return score;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;What made it tricky:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;Whenever I tell other programmers I'm learning programming with Java, they express much exasperation about the language, and I begin to see why. The statement that I had wanted to express was,&amp;nbsp; "If the letter at increment "i" is A, E, I, L, etc, then add 1 to the instance variable "score." I was looking for the switch statement to support an "or" statement (ie "case 'A' || 'E' || 'I' || 'L' ") but alas, the syntax isn't that neat. I had to list all the separate cases individually, though fortunately I didn't have to do the action block repeatedly for cases with identical points! I don't exactly know how you'd handle this in Python or Ruby but I expect it would be more succinct.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;I think if building a Scrabble calculator in the "real world," you'd want to use a hash table&amp;nbsp; to easily turn the letter:points match into key:value pairs. We haven't gotten to hashes and arrays yet, so the tools for this problem were either a) switch statements or b) cascading "if" statements.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-6013084185279712922?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/6013084185279712922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-calculate-scrabble-score-of.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6013084185279712922'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6013084185279712922'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/12/program-to-calculate-scrabble-score-of.html' title='Program to Calculate the Scrabble Score of a Word'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-coRZVzVVyOU/TuWhMagfVXI/AAAAAAAAAik/yqif1L4fk8I/s72-c/ScrabbleCalc.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-6135199273230543291</id><published>2011-11-12T20:27:00.001-05:00</published><updated>2011-11-12T21:02:01.641-05:00</updated><title type='text'>Program to Create Five Random "Words"</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Problem:&amp;nbsp;Write a method &lt;span class="s1"&gt;randomWord &lt;/span&gt;that returns a randomly constructed “word” consisting of randomly chosen letters. The number of letters in the word should also be chosen randomly by picking a number between the values of the named constants &lt;span class="s1"&gt;MIN_LETTERS&amp;nbsp;&lt;/span&gt;and&amp;nbsp;&lt;span class="s1"&gt;MAX_LETTERS&lt;/span&gt;. Write a&amp;nbsp;&lt;span class="s1"&gt;ConsoleProgram&amp;nbsp;&lt;/span&gt;that tests your method by displaying five random words. (Roberts ch 10, problem 2).&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-FhIineL29D0/Tr8kekUBF6I/AAAAAAAAAh8/bsLUKVSQ3F8/s1600/FiveRandomWords.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-FhIineL29D0/Tr8kekUBF6I/AAAAAAAAAh8/bsLUKVSQ3F8/s1600/FiveRandomWords.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class="brush:java;"&gt;/* File: fiveRandomWords&lt;br /&gt; * --------------&lt;br /&gt; * This program generates random "words," just random letters stuck together. The characters of the word are &lt;br /&gt; * random, but must be an upcase letter-- these are integers that correspond to the ASCII characters of the &lt;br /&gt; * upcase alphabet. The length of the word is also random, but bounded by the constants MIN_LETTERS and&lt;br /&gt; * MAX_LETTERS.&lt;br /&gt; * &lt;br /&gt; * The run method of the program simply starts of with blank String instance and keeps pulling random letters&lt;br /&gt; * out of the random generator until the word length is reached. It relies on a private method to generate the &lt;br /&gt; * random characters and to set the word length.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import acm.util.RandomGenerator;&lt;br /&gt;&lt;br /&gt;public class FiveRandomWords extends ConsoleProgram { &lt;br /&gt;    public void run() {&lt;br /&gt;        for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;            int wordLength = rgen.nextInt(MIN_LETTERS, MAX_LETTERS);&lt;br /&gt;            String myRandomWord = " ";&lt;br /&gt;            for (int j = 0; j &amp;lt; wordLength; j++) {&lt;br /&gt;                myRandomWord += randomLetter();&lt;br /&gt;            }&lt;br /&gt;            println(myRandomWord);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private char randomLetter() { &lt;br /&gt;        return (char) rgen.nextInt((int) 'A', (int) 'Z');&lt;br /&gt;    }&lt;br /&gt;    private RandomGenerator rgen = RandomGenerator.getInstance();&lt;br /&gt;    private static final int MIN_LETTERS = 3;&lt;br /&gt;    private static final int MAX_LETTERS = 10;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;What made it tough:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This simple problem is all about getting comfortable with scalar types: data values that can be represented as numbers. The key snippet in the program is this:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&lt;span class="s1"&gt;private&lt;/span&gt; &lt;span class="s1"&gt;char&lt;/span&gt; randomLetter() {&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class="s1"&gt;return&lt;/span&gt; (&lt;span class="s1"&gt;char&lt;/span&gt;) &lt;span class="s2"&gt;rgen&lt;/span&gt;.nextInt((&lt;span class="s1"&gt;int&lt;/span&gt;) &lt;span class="s3"&gt;'A'&lt;/span&gt;, (&lt;span class="s1"&gt;int&lt;/span&gt;) &lt;span class="s3"&gt;'Z'&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;How do you return a *character* that's random but bounded by the *integers* A and Z? Because the "char" data type in Java maps directly to Unicode and ASCII. Unicode and ASCII are both systems that match characters (letters, punctuations marks, etc) to integers so that it's easier for the computer to understand.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Fun facts I learned:&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Precursors to ASCII literally mapped 'A' to 1, 'Z' to 26, and everything in-between, but ASCII didn't do so since there are additional characters that&amp;nbsp;precede&amp;nbsp;letters.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;The ASCII system came first, Unicode second. ASCII characters don't encode most non-Western characters, so Unicode was created with&amp;nbsp;thousands&amp;nbsp;of new characters to account for these.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Unicode contains everything that's in ASCII, and to be completely backwards-compatible, the first 128 digits of Unicode map directly to ASCII&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"&gt;Because characters are mapped this way, you can perform some&amp;nbsp;numeric&amp;nbsp;operations on *letters* as&amp;nbsp;specifying&amp;nbsp;a range like I did above, or taking a letter 'A' and adding 5 to get 'F.' &amp;nbsp;When specifying the range of characters, you could either provide the character or the number equivalent-- the computer can handle each one and in fact the (int) cast isn't necessary- it's there to make things clearer for humans so they don't think, "Why are you bounding nextInt with characters?" You must specify the (char) cast however to make sure that when you do rgen.nextInt, you get a letter back, not a number.&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-6135199273230543291?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/6135199273230543291/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/11/program-to-create-five-random-words.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6135199273230543291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6135199273230543291'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/11/program-to-create-five-random-words.html' title='Program to Create Five Random &quot;Words&quot;'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-FhIineL29D0/Tr8kekUBF6I/AAAAAAAAAh8/bsLUKVSQ3F8/s72-c/FiveRandomWords.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5746356175679957133</id><published>2011-11-12T16:59:00.001-05:00</published><updated>2011-11-12T19:24:34.057-05:00</updated><title type='text'>Program to Create Acronyms</title><content type='html'>This came from the book as an example, but I wanted to  make it better :)&lt;br /&gt;&lt;br /&gt;This program, taken from the unit in the book that deals with characters and string manipulation, lets the user input a string (usually a sentence of multiple words) and get an acronym back. The method that builds the acronym works by taking the very first character, and after that, hunting for space characters. It looks at the character right *after* each space and slices that following character to add to the acronym.&lt;br /&gt;&lt;br /&gt;There is a bug however-- what happens if the user enters in "hello**world" (say that "*" is a space here)? The acronym SHOULD be "hw", but instead you get "h*w." No good.&lt;br /&gt;&lt;br /&gt;I wrote a program, first to test out the buggy method to verify what the acronym for "hello**world" would be, and then to fix that edge case. &lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-RgEpEZdPBFo/Tr8K_U83poI/AAAAAAAAAh0/Vvg9Yn814DE/s1600/Shared+Menubar-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="84" src="http://2.bp.blogspot.com/-RgEpEZdPBFo/Tr8K_U83poI/AAAAAAAAAh0/Vvg9Yn814DE/s640/Shared+Menubar-1.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/* File: acronymBuilder&lt;br /&gt;* --------------------&lt;br /&gt;* This program takes a string and returns the acronym, or, a string made up of the first character of each&lt;br /&gt;* word. It works via a private method that takes the first character and saves it to the instance variable&lt;br /&gt;* "result." Then it finds each space, takes the character directly after the space, and takes that following &lt;br /&gt;* character and appends it to "result" until there are no spaces left.&lt;br /&gt;* &lt;br /&gt;* This method originally appeared in ch 10 of Roberts' "The Art and Science of Java," but the original method&lt;br /&gt;* didn't account for inputs where there were 2 spaces in a row, so I'm making the modification and writing a&lt;br /&gt;* run method to use it.&lt;br /&gt; */&lt;br /&gt;import acm.program.*;&lt;br /&gt; &lt;br /&gt;public class acronymBuilder extends ConsoleProgram {&lt;br /&gt;    public void run(){&lt;br /&gt;        String initialString = readLine("Enter a string you want to generate the acronym for:");&lt;br /&gt;        String result = acronym(initialString);&lt;br /&gt;        println(result);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private String acronym(String str) {&lt;br /&gt;        String result = str.substring(0, 1); &lt;br /&gt;        int pos = str.indexOf(' '); &lt;br /&gt;        while (pos != -1) {&lt;br /&gt;            if (str.charAt(pos + 1) != ' ') { &lt;br /&gt;                result += str.substring(pos + 1, pos + 2); &lt;br /&gt;            }&lt;br /&gt;            pos = str.indexOf(' ', pos + 1); //Finds the next space sign           &lt;br /&gt;        } &lt;br /&gt;    return result;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;What made it tough:There were a couple bugs that stumped me along the way.&lt;br /&gt;First, I wanted to write an "if" statement checking to see if there are two space signs in a row. Specifically, you look at aspace sign and see if the character after it is also a space sign. The "if" statement looked like this:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;if (str.charAt(pos + 1) != " ");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Eclipse highlighted this statement in red and did not like it. Why?&lt;br /&gt;&lt;br /&gt;My first thought was because of that problem that you can't compare strings with each other. The "==" and "!=" operatorsare mathematical comparisons that you can only use on primitives like ints, whereas for strings that operator willexamine the underlying object in memory that stores the string; it won't examine the value.&lt;br /&gt;&lt;br /&gt;But wait a minute, that can't be it. First of all, comparing strings is technically a valid argument...it will just usually return an inaccurate result. Like if there were two strings "hello" and "hello" that pointed to two different objects, it will return "False" when the developer probably wanted it to return "True".&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Also, &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;str.charAt(pos + 1)&lt;/span&gt;is actually a char, not a string, and chars are primitives, so it should be OK to use the "!=" on it.&lt;br /&gt;&lt;br /&gt;The problem was that the second part of my comparison, " ", is a string, so I was comparing a char against a string which isn't ok.All I had to do to fix it was put the space sign in single quotes, since Java denotes chars with single quotes. Easy :)&lt;br /&gt;&lt;br /&gt;The second bug came up as I tried to run the program. This is what the code looked like at that point:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;import acm.program.*;&lt;br /&gt; &lt;br /&gt;public class acronymBuilder extends ConsoleProgram {&lt;br /&gt;    public void run(){&lt;br /&gt;        String initialString = readLine("Enter a string you want to generate the acronym for:");&lt;br /&gt;        String result = acronym(initialString);&lt;br /&gt;        println(result);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private String acronym(String str) {&lt;br /&gt;        String result = str.substring(0, 1); &lt;br /&gt;        int pos = str.indexOf(' '); &lt;br /&gt;        while (pos != -1) {&lt;br /&gt;            if (str.charAt(pos + 1) != ' ') { &lt;br /&gt;                result += str.substring(pos + 1, pos + 2); &lt;br /&gt;                pos = str.indexOf(' ', pos + 1); //Finds the next space sign&lt;br /&gt;            }&lt;br /&gt;        } &lt;br /&gt;    return result;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;When I ran the code, it compiled and I input the string "ne**s****w". (There were no "*" signs really, I just used them to demonstrate) where spaces were. When I hit "return", the program didn't print anything! Why?&lt;br /&gt;&lt;br /&gt;At first I thought there was an unclosed loop or something, but then I realized: I had inserted an "if" statement checking for if the character following the space sign is NOT another space, but I didn't tell the program what to do if that following character WAS a space! The direction to move on to the next space sign (ie "pos = str.indexOf(' ', pos + 1)" was inside the new "if" statement! Once I adjusted the stuff in my loops I was fine.&lt;br /&gt;&lt;br /&gt;Strange though-- on that second bug, my return statement WAS outside the "if" statement, and I had already initiated "result", so why didn't my program atleast print the first character...?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5746356175679957133?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5746356175679957133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/11/program-to-create-acronyms.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5746356175679957133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5746356175679957133'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/11/program-to-create-acronyms.html' title='Program to Create Acronyms'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-RgEpEZdPBFo/Tr8K_U83poI/AAAAAAAAAh0/Vvg9Yn814DE/s72-c/Shared+Menubar-1.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-1172392744682275706</id><published>2011-10-30T23:12:00.001-04:00</published><updated>2011-10-30T23:46:05.071-04:00</updated><title type='text'>Program to Test if a Word is a Palindrome.</title><content type='html'>Problem:"A palindrome is a word that reads identically backward and forward, such as level or noon. Write a predicate method isPalindrome(str) that returns true if the string str is a palindrome. In addition, design and write a test program that calls isPalindrome to demonstrate that it works. In writing the program, concentrate on how to solve the problem simply rather than how to you make your solution more efficient." (Roberts ch 10, problem 6.)&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;pre class="brush:java;"&gt;/* File: PalindromeTester&lt;br /&gt; * ----------------------------&lt;br /&gt; * This program lets a user enter in a string, and will respond with confirmation that the word is&lt;br /&gt; * or is not a palindrome. It uses a private boolean method isPalindrom(). This method goes &lt;br /&gt; * halfway down each character of the word and compares to see whether that character and the&lt;br /&gt; * character at the mirror position on the string equal each other. If they don't, then break and &lt;br /&gt; * returnn false, but if they do for all letters in the first half of the string, then return true.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt; &lt;br /&gt;public class PalindromeTester extends ConsoleProgram {&lt;br /&gt;    public void run(){&lt;br /&gt;        String str = readLine("Is it a palindrome? Enter string to test here:");&lt;br /&gt;        if (isPalindrome(str)) {&lt;br /&gt;            println("Yes it is!");&lt;br /&gt;        }&lt;br /&gt;        else println("No it isn't.");&lt;br /&gt;    }&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;    private boolean isPalindrome(String str) {&lt;br /&gt;        for (int i = 0; i &amp;lt; str.length()/2; i++) {          &lt;br /&gt;            if (str.charAt(i) != str.charAt(str.length()-i-1)) {          &lt;br /&gt;                return false;&lt;br /&gt;            }                  &lt;br /&gt;        }&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-5X_6wRLZgwM/Tq4Y3dZDqhI/AAAAAAAAAhY/tF06vqHTQeE/s1600/PalindromeTester-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="171" src="http://1.bp.blogspot.com/-5X_6wRLZgwM/Tq4Y3dZDqhI/AAAAAAAAAhY/tF06vqHTQeE/s400/PalindromeTester-1.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;What made it tough:&lt;br /&gt;&lt;br /&gt;When I first wrote this, I had a bug. When checking to see if a word is a palindrome, the general approach is, for all charachters "i", to compare the "ith" character  with its mirror character and see if they are equal, for all characters "i". So if your word is "kook", you'd compare the first last one to see if the characters are not the same (break if they're not), see if the two middle ones are the same, and so on.&lt;br /&gt;&lt;br /&gt;In my buggy version, this is how I did the mirror check:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;"if (str.charAt(i) != str.charAt(str.length()-i))"&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This is the right version:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;"if (str.charAt(i) != str.charAt(str.length()-i-1))"&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Why do you have to subtract the extra 1 when you're trying inspect "i"'s mirror? In short because the "str.length" method counts starting at 1 when it returns how many characters a string contains, and meanwhile the charAt(i) method returns the "ith" character in a string, but counting as if the first letter of that string is 0. So because of this, the character at the second half of the word was actually always one after it should have been.&lt;br /&gt;&lt;br /&gt;The funny thing is, when I wrote this and ran into the error, it didn't manifest itself via a warning in the Eclipse editor, or as red text showing up in my program's response. Instead the problem killed my program completely. I'd click to run the program, it would let me load up the interface for the user and enter in a word to test, and then instead of returning a result it would abort.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-Rn467QFA78Q/Tq4ZDat0t1I/AAAAAAAAAhk/-dhP8q0N2QY/s1600/Shared%2BMenubar-5-1-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="180" src="http://2.bp.blogspot.com/-Rn467QFA78Q/Tq4ZDat0t1I/AAAAAAAAAhk/-dhP8q0N2QY/s400/Shared%2BMenubar-5-1-1.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;I'm sure there's a reason why my program reacted to the error in that particular way, but I'm not sure what that is. Thoughts?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-1172392744682275706?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/1172392744682275706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/10/program-to-test-if-word-is-palindrome.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1172392744682275706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1172392744682275706'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/10/program-to-test-if-word-is-palindrome.html' title='Program to Test if a Word is a Palindrome.'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-5X_6wRLZgwM/Tq4Y3dZDqhI/AAAAAAAAAhY/tF06vqHTQeE/s72-c/PalindromeTester-1.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-4840817395524745440</id><published>2011-10-16T20:26:00.000-04:00</published><updated>2011-10-17T15:00:45.117-04:00</updated><title type='text'>Class definition to represent playing cards</title><content type='html'>&lt;br /&gt;Problem:&lt;br /&gt;&lt;br /&gt;Implement a new class called Card that includes the following entries:&lt;br /&gt;*&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES) and the four ranks that are traditionally represented in words (ACE, JACK, QUEEN, KING). The values of the rank constants should be 1, 11, 12, and 13, respectively.&lt;br /&gt;*&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;A constructor that takes a rank and a suit and returns a Card with those values. •&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Accessor methods getRank and getSuit to retrieve the rank and suit components&lt;br /&gt;of a card.&lt;br /&gt;*&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;An implementation of the toString method that returns the complete name of the card as in exercise 1. Remember that you can use the + operator to connect the different parts of the string together, as shown in the toString implementation for the Rational class in Figure 6-9.&lt;br /&gt;&lt;br /&gt;Roberts ch 6, problem 7.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;&lt;br /&gt;/* File: Card class&lt;br /&gt; * ----------------------------&lt;br /&gt; * Class to represent a playing card. There are named constants for suit and face card values to translate integers into respective values,&lt;br /&gt; * so a client of this class can represent this information with either the integer or the constant.&lt;br /&gt; * &lt;br /&gt; * How you'd use this class:&lt;br /&gt; * a) Create a new playing card: "myCard = new Card (12, 2)" or "myCard = new Card (Card.QUEEN, Card.HEARTS)"&lt;br /&gt; * b) Get the rank or suit of cards: "myCardSuit = myCard.getSuit()" or "myCardRank = myCard.getRank()"&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public class Card {&lt;br /&gt;    &lt;br /&gt;    public Card (int rank, int suit){ //Constructor&lt;br /&gt;        cardRank = rank; // A real program would check whether "rank" is between 1 and 13, and if not, raise an illegal arguments exception&lt;br /&gt;        cardSuit = suit; // A real program would check whether "rank" is between 1 and 4, and if not, raise an illegal arguments exception&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    public String getRank() {&lt;br /&gt;        switch (cardRank) {&lt;br /&gt;            case ACE: return "Ace";&lt;br /&gt;            case JACK: return "Jack";&lt;br /&gt;            case QUEEN: return "Queen";&lt;br /&gt;            case KING: return "King";&lt;br /&gt;            default: return Integer.toString(cardRank); &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;   &lt;br /&gt;    public String getSuit() {&lt;br /&gt;        switch (cardSuit) {&lt;br /&gt;            case SPADES: return "Spades";&lt;br /&gt;            case HEARTS: return "Hearts";&lt;br /&gt;            case CLUBS: return "Clubs";&lt;br /&gt;            case DIAMONDS: return "Diamonds";&lt;br /&gt;            default: return "no suit."; // Won't actually happen but Java needs it or it thinks the switch statement is incomplete&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public String toString() {&lt;br /&gt;        return ("The" + getRank() + " of " + getSuit() + ".");&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    // Instance variables keep track of the suit and rank values for the lifetime of the card object&lt;br /&gt;    private int cardSuit; // Keeps track of the Card object's suit&lt;br /&gt;    private int cardRank; // Keeps track of the object's rank&lt;br /&gt;    &lt;br /&gt;    // Named Constants to establish int values for each of the suits -- Static variables are class variables&lt;br /&gt;    public static final int SPADES = 1; //does this need to be "void"...?&lt;br /&gt;    public static final int HEARTS = 2;&lt;br /&gt;    public static final int CLUBS = 3;&lt;br /&gt;    public static final int DIAMONDS = 4;&lt;br /&gt;  &lt;br /&gt;    // Named constants to establish int values for each of the suits &lt;br /&gt;    public static final int ACE = 1; //How does this named constant interact w/ the rest of the program? This part is a little messed up&lt;br /&gt;    public static final int JACK = 11;&lt;br /&gt;    public static final int QUEEN = 12;&lt;br /&gt;    public static final int KING = 13;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This problem came before the "Employee" class problem in the book, but I definitely think it's more confusing than writing the Employee class and only really understood it after doing problem 8. At heart, the main point of both classes is to store information about an employee instance or playing card instance and let clients of the class access the information via public methods.&lt;br /&gt;&lt;br /&gt;This one had an extra wrench thrown in because we needed to associate integers with suit or facecard values. I completed the class definition and I get now that the benefit of having the named constants is that, even though the constructor method is expecting integers for the suit and number values, you can pass the constants instead so it's a bit more human-readable.&lt;br /&gt;&lt;br /&gt;For example, suit information for any particular card is represented in four different places:&lt;br /&gt;1) In the constructor method, the client passes the suit value through as a parameter that instantly gets stored to the instance variable.&lt;br /&gt;2) The instance variable that keeps track of the suit for the lifetime of the object&lt;br /&gt;3) The named constants series translates suit integers into strings. ** Question: Why do we have this level of translation instead of having the constructor look for a string instead of an integer for the "suit" argument? ** From my understanding it's because it's easier to check whether ints are between 1 and 4 than to check all the different casings and spellings people might use to input a string, so it's more bug-tolerant.&lt;br /&gt;4) The getSuit() method returns the current value of the cardSuit instance variable, so a client of the class can store that information in a variable of their own.&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-4840817395524745440?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/4840817395524745440/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/10/class-definition-to-represent-playing.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4840817395524745440'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4840817395524745440'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/10/class-definition-to-represent-playing.html' title='Class definition to represent playing cards'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2761659325096403153</id><published>2011-10-16T19:40:00.001-04:00</published><updated>2011-10-16T19:46:44.842-04:00</updated><title type='text'>Class to represent employees</title><content type='html'>Problem:&lt;br /&gt;&lt;br /&gt;"Write the definition for a new class named Employee that stores the following data for a single employee:&lt;br /&gt;&amp;nbsp;* The name of the employee (a String)&lt;br /&gt;&amp;nbsp;* The name of the employee’s supervisor (also a String)&lt;br /&gt;&amp;nbsp;*&amp;nbsp;The employee’s annual salary (a double)&lt;br /&gt;&lt;br /&gt;As with the other classes defined in this book, you should make sure that the instance variables containing these values are private and instead provide get and set methods to retrieve or change any of the values." (Roberts ch 6, problem 8)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/* File: Employee&lt;br /&gt; * ----------------------------&lt;br /&gt; * Class to store information about employees, retrieve that information, and reset the information.&lt;br /&gt; * How you'd use this class:&lt;br /&gt; * a) Enter in a new employee, ie " emp = new Employee('Bob Cratchit', 'Ebenezer Scrooge', 25.00) "&lt;br /&gt; * b) get information, ie " name = emp.getName() "&lt;br /&gt; * c) set information, ie " emp.setSupervisor('Mickey Mouse') "&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public class Employee {&lt;br /&gt;    public Employee (String name, String supervisor, double salary) { &lt;br /&gt;        //LOCAL variables. Exist just for the run of this constructor method. The client passes name/suvervisor/salary information&lt;br /&gt;        // in initially and it immediately gets used to set the instnace variable value.&lt;br /&gt;        employeeName = name;&lt;br /&gt;        supervisorName = supervisor;&lt;br /&gt;        employeeSalary = salary;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    //Getter methods:&lt;br /&gt;    &lt;br /&gt;    public String getName() { &lt;br /&gt;        return employeeName;&lt;br /&gt;    }&lt;br /&gt;    public String getSupervisor() {&lt;br /&gt;        return supervisorName;&lt;br /&gt;    }&lt;br /&gt;    public double getSalary() {&lt;br /&gt;        return employeeSalary;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    //Setter methods: These have no return type, they just reset the instance variables&lt;br /&gt;    &lt;br /&gt;    public void setName(String newName) { &lt;br /&gt;        employeeName = newName;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public void setSupervisor(String newSupervisor) { &lt;br /&gt;        supervisorName = newSupervisor; &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public void setSalary(double newSalary) { &lt;br /&gt;        employeeSalary = newSalary;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    // INSTANCE variables: Stuff that we're keeping track of for the lifetime of the object. &lt;br /&gt;    // It gets set using the temp (aka local) variables in the constructor&lt;br /&gt;    &lt;br /&gt;    private String employeeName;&lt;br /&gt;    private String supervisorName;&lt;br /&gt;    private double employeeSalary;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;This chapter in the book gets students started with writing their own classes. The problem was very simple, but it's good refresher on how objects work see the difference between instance variables and local variables in action.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;It took me a while to get used to the fact that each piece of information is represented 3 times:&lt;br /&gt;* In the constructor method, the client of this class creates a new instance of the "Employee" class and sets the initial name, supervisor and salary values. These are passed through as local variables that exist just for the life of the constructor method and instantly get stored to the instance variable.&lt;br /&gt;* Instance variables that store the name, supervisor and salary values for the lifetime of the object.&lt;br /&gt;* The getter method returns the name/supervisor/salary information from the current value of the instance variable, so clients of the class can store the information in a variable of their own. The setter methods update the instance variable based on new values that the client of the class passes (using a local value that exists just for the life of the setter method).&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Once I got a firm hold on why name/supervisor/salary information is distinct when represented in the local variables versus the instance variables, I got a better understanding of how classes work and how you'd write a class that other programs can use.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2761659325096403153?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2761659325096403153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/10/class-to-represent-employees.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2761659325096403153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2761659325096403153'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/10/class-to-represent-employees.html' title='Class to represent employees'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2837336921514523626</id><published>2011-09-23T15:49:00.000-04:00</published><updated>2011-09-23T15:49:07.951-04:00</updated><title type='text'>Arithmatic Tutor, updated with variable congrats messages</title><content type='html'>Problem: "To add variety to the interaction, modify your solution to exercise 5 so that it randomly chooses among four or five different messages when the student gets the right answer."&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-asZ2Djba-Pw/TnzgTQe4nAI/AAAAAAAAAg8/dmt8xLyTuFU/s1600/ArithmaticTutor.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="123" src="http://4.bp.blogspot.com/-asZ2Djba-Pw/TnzgTQe4nAI/AAAAAAAAAg8/dmt8xLyTuFU/s320/ArithmaticTutor.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The code was all the same except that instead of printing the hard-coded string "Right" if the user got the answer right, it printed answerAffirmation. This was a method that generated one of 4 random integers and mapped each integer to a different congrats message.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;&lt;br /&gt; if (userAnswer == programSolution) {&lt;br /&gt;     println(answerAffirmation());&lt;br /&gt; }&lt;br /&gt; else {&lt;br /&gt;     println("No, the correct answer is " + programSolution); &lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I used a switch statement. At first the method was buggy because the program thought I wasn't returning a result:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-El11dm279ec/TnzhabPGeBI/AAAAAAAAAhA/SWyM9yfA76c/s1600/Java+-+Assignment2_ArithmaticTutor.java+-+Eclipse+-+_Users_reneechu_Development_Stanford_Java.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="161" src="http://3.bp.blogspot.com/-El11dm279ec/TnzhabPGeBI/AAAAAAAAAhA/SWyM9yfA76c/s640/Java+-+Assignment2_ArithmaticTutor.java+-+Eclipse+-+_Users_reneechu_Development_Stanford_Java.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The problem was, even though having the strings map to one of four integers that I knew would be exhaustive, the program didn't realize that answers could only come from that random integer, so I must include a default string to get returned:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-VA9PvkYKYaU/TnzhrtA_O5I/AAAAAAAAAhE/VBxI-QCFQGc/s1600/Java+-+Assignment2_ArithmaticTutor.java+-+Eclipse+-+_Users_reneechu_Development_Stanford_Java-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="161" src="http://2.bp.blogspot.com/-VA9PvkYKYaU/TnzhrtA_O5I/AAAAAAAAAhE/VBxI-QCFQGc/s640/Java+-+Assignment2_ArithmaticTutor.java+-+Eclipse+-+_Users_reneechu_Development_Stanford_Java-1.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;In reality the message "Grrrrreat!" won't get returned unless there's a bug with my method loops or with the rgen factory resource.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2837336921514523626?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2837336921514523626/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/09/arithmatic-tutor-updated-with-variable.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2837336921514523626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2837336921514523626'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/09/arithmatic-tutor-updated-with-variable.html' title='Arithmatic Tutor, updated with variable congrats messages'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-asZ2Djba-Pw/TnzgTQe4nAI/AAAAAAAAAg8/dmt8xLyTuFU/s72-c/ArithmaticTutor.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5401957162414446161</id><published>2011-09-18T01:22:00.000-04:00</published><updated>2011-09-18T01:22:15.574-04:00</updated><title type='text'>Arithmetic Tutor Program (or, structuring and debugging loops)</title><content type='html'>Problem:&lt;br /&gt;&lt;br /&gt;Write a program that poses a series of simple arithmetic problems for a student to answer.&lt;br /&gt;&lt;br /&gt;Your program should meet these requirements:&lt;br /&gt;• It should ask a series of five questions. As with any such limit, the number of questions should be coded as a named constant so that it can easily be changed.&lt;br /&gt;• Each question should consist of a single addition or subtraction problem involving just two numbers, such as “What is 2 + 3?” or “What is 11 – 7?”. The type of problem—addition or subtraction—should be chosen randomly for each question.&lt;br /&gt;• To make sure the problems are appropriate for students in the first or second grade, none of the numbers involved, including the answer, should be less than 0 or greater than 20. This restriction means that your program should never ask questions like “What is 11 + 13?” or “What is 4 – 7?” because the answers are outside the legal range. Within these constraints, your program should choose the numbers randomly.&lt;br /&gt;• The program should give the student three chances to answer each question. If the student gives the correct answer, your program should indicate that fact in some properly congratulatory way and go on to the next question. If the student does not get the answer in three tries, the program should give the answer and go on to another problem. &lt;br /&gt;&lt;br /&gt;(Roberts Ch 6, problem 7)&lt;br /&gt;&lt;br /&gt;This was a fun problem to write, though debugging took a long time. Here was how I split up the problem, given the requirements. There are three major parts of this program: &lt;br /&gt;1) Generate the arithmatic program.&lt;br /&gt;2) Present it to the student and evaluate his answer&lt;br /&gt;3) Stop the program after 5 presented problems.&lt;br /&gt;&lt;br /&gt;These were my smaller steps:&lt;br /&gt;* Generate two numbers (0-20) and an operator (comes from boolean true/false). Use random generator instance.&lt;br /&gt;* Put them together and assess them so the program "knows" the correct answer&lt;br /&gt;* If the result (“problemAnswer”) is not between 0-20, don't print anything and start again&lt;br /&gt;* If it's between 0-20, then present the problem to the user. Declare "int userAnswer = readln(print the equation here)". &lt;br /&gt;* To keep track of user attempts, start a local variable "countUserAttempts" and make it start at 1&lt;br /&gt;* If problemAnswer == userAnswer, break from the loop (print that it's right) and start again&lt;br /&gt;* If problemAnswer != userAnswer, then print it's wrong. Add +1 to the “countUserAttempts” variable and send them back thru the inner loop to try again.&lt;br /&gt;* Let user submit a second answer. If it's right then print that it's correct and start again. If it's wrong then add +1 to the countUserAttempts variable (so now it’s three) and send them back thru.&lt;br /&gt;* If it’s wrong again, then it will make countUserAttempts equal 4 so it will break from the inner “while” loop and roll down to the next argument and print the correct answer.&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-GGV5YAmW2k0/TnUuOXjXKTI/AAAAAAAAAgg/NsrDxmbzSeM/s1600/ArithmaticTutor.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="226" width="400" src="http://4.bp.blogspot.com/-GGV5YAmW2k0/TnUuOXjXKTI/AAAAAAAAAgg/NsrDxmbzSeM/s400/ArithmaticTutor.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/* File: ArithmaticTutor&lt;br /&gt; * This program shows a series of arithmetic problems to the user and evaluates his or her input.&lt;br /&gt; * In accordance with the problem specs, we only present components between 0 and 20, and make&lt;br /&gt; * sure that the answer (sum or difference) is between 0 and 20 as well before giving it to the user.&lt;br /&gt; * We make up a problem on the fly using 2 methods that each use a random generator instance;&lt;br /&gt; * one to generate the numbers to in our problem, and a second to decide whether the operator is plus &lt;br /&gt; * or minus. There's also a method to collect the user's input. There are two count-keeping variables:&lt;br /&gt; * one, "countPresentedProblems," makes sure that we give the user only 5 problems (or &lt;br /&gt; * however many are allowed by the "maxProblems" constant. The other, "countUserAttempts,"&lt;br /&gt; * which is inside the inner "while" loop, keeps track of user attempts to make sure &lt;br /&gt; * he or she does only 3 attempts. If the user gets the answer right in 3 or fewer attempts, we print &lt;br /&gt; * in 3 or fewer attempts, we print "Right!" and move on to the next problem. If not,&lt;br /&gt; * we just tell the right answer and move on.&lt;br /&gt; * &lt;br /&gt; */&lt;br /&gt;    import acm.program.*;&lt;br /&gt;    import acm.util.RandomGenerator;&lt;br /&gt;&lt;br /&gt;    public class ArithmaticTutor extends ConsoleProgram {&lt;br /&gt;        public void run() {&lt;br /&gt;            println("Welcome to Math Tutor! Prepare to be challenged.");&lt;br /&gt;            int countPresentedProblems = 0; &lt;br /&gt;            while (countPresentedProblems &lt; maxProblems) {&lt;br /&gt;                // First we put together all the stuff needed for our problem and assess the first problem ourselves &lt;br /&gt;                int firstNumber = generateNumber(0,20); &lt;br /&gt;                int secondNumber = generateNumber(0,20); &lt;br /&gt;                boolean isPlusNotMinus = plusNotMinus();&lt;br /&gt;                int programSolution = evaluateProblem(firstNumber, secondNumber, isPlusNotMinus);&lt;br /&gt;                // Then we present it to the student (if the answer is between 0 and 20)&lt;br /&gt;                if (programSolution &gt; 0 &amp;&amp; programSolution &lt; 20){&lt;br /&gt;                    countPresentedProblems +=1;&lt;br /&gt;                    int userAnswer = getUserAnswer(firstNumber, secondNumber, isPlusNotMinus);&lt;br /&gt;                    int countUserAttempts = 1;&lt;br /&gt;                    while (countUserAttempts &lt; 3 &amp;&amp; userAnswer != programSolution) {&lt;br /&gt;                        userAnswer = readInt ("Wrong. Try another answer:");&lt;br /&gt;                        countUserAttempts += 1;&lt;br /&gt;                    }&lt;br /&gt;                    if (userAnswer == programSolution) {&lt;br /&gt;                        println( "Right!");&lt;br /&gt;                    }&lt;br /&gt;                    else {&lt;br /&gt;                        println("No, the correct answer is " + programSolution); &lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;        private final static int maxProblems = 5;&lt;br /&gt;  &lt;br /&gt;        private RandomGenerator rgen = RandomGenerator.getInstance();&lt;br /&gt;  &lt;br /&gt;        private int generateNumber(int min, int max) {&lt;br /&gt;            int number = rgen.nextInt(min, max);&lt;br /&gt;            return number;&lt;br /&gt;        }&lt;br /&gt;        private boolean plusNotMinus() {&lt;br /&gt;            return rgen.nextBoolean();&lt;br /&gt;        }&lt;br /&gt;        private int evaluateProblem (int firstNumber, int secondNumber, boolean plusNotMinus){&lt;br /&gt;            if (plusNotMinus) {&lt;br /&gt;                return firstNumber + secondNumber;&lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;                return firstNumber - secondNumber;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        private int getUserAnswer (int firstNumber, int secondNumber, boolean plusNotMinus){&lt;br /&gt;            if (plusNotMinus) {&lt;br /&gt;                return readInt("What is " + firstNumber + "+" + secondNumber + "?");&lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;                return readInt("What is " + firstNumber + "-" + secondNumber + "?");&lt;br /&gt;            }&lt;br /&gt;        } &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;What made it hard: Keeping track of your loops. Poorly structured loops kept on causing bugs!For example there was a bug where if you entered 2 wrong answers in a row, it fell back to the “Wrong, the correct answer is [programAnswer]” part, even though it should wait till 3 wrong entries. The problem was that the part of the code that says “Wrong, the correct answer is [programAnswer]” was *inside* the "while" loop, so once we evaluated that it was wrong it would print the correct answer then still re-prompt for another attempt!Then I moved that “No, the correct answer is [correct answer]” part to outside the nearest loop, but then it started getting run for every submitted answer, even if they got it right!Here are my other bloopers, along with a brief explanations of what was wrong with the code when they appeared.&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-mpibVb9iBh4/TnVj6u_22QI/AAAAAAAAAgo/CeYvO55I_MQ/s1600/ArithmaticTutor-2.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="170" width="400" src="http://4.bp.blogspot.com/-mpibVb9iBh4/TnVj6u_22QI/AAAAAAAAAgo/CeYvO55I_MQ/s400/ArithmaticTutor-2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-h51S4np52-A/TnVlaf0jTxI/AAAAAAAAAgw/zxqckxw4w1c/s1600/ArithmaticTutor-4.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="239" width="400" src="http://4.bp.blogspot.com/-h51S4np52-A/TnVlaf0jTxI/AAAAAAAAAgw/zxqckxw4w1c/s400/ArithmaticTutor-4.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-YuvCYUv1i5I/TnVpSoEXsEI/AAAAAAAAAg4/ln3MAObDTxU/s1600/Shared%2BMenubar-3.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="155" width="400" src="http://1.bp.blogspot.com/-YuvCYUv1i5I/TnVpSoEXsEI/AAAAAAAAAg4/ln3MAObDTxU/s400/Shared%2BMenubar-3.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5401957162414446161?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5401957162414446161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/09/arithmetic-tutor-program-or-structuring.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5401957162414446161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5401957162414446161'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/09/arithmetic-tutor-program-or-structuring.html' title='Arithmetic Tutor Program (or, structuring and debugging loops)'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-GGV5YAmW2k0/TnUuOXjXKTI/AAAAAAAAAgg/NsrDxmbzSeM/s72-c/ArithmaticTutor.jpg' height='72' width='72'/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5733767852815951748</id><published>2011-08-07T19:56:00.001-04:00</published><updated>2011-08-07T21:02:43.282-04:00</updated><title type='text'>Program to Flip a Coin (until you get 3 "heads" in a row):</title><content type='html'>"Write a program that simpulates flipping a coin repeatedly and continues until three consecutive heads are tossed. At that point your program should display the total number of times the coin was flipped." (Roberts ch 6, problem 2).&lt;br /&gt;&lt;br /&gt;What it looks like: &lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-j0-sPe7xA7w/Tj8nCjDJi8I/AAAAAAAAAgY/dPdkB9jEVLs/s1600/Shared%2BMenubar-1.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="312" width="400" src="http://1.bp.blogspot.com/-j0-sPe7xA7w/Tj8nCjDJi8I/AAAAAAAAAgY/dPdkB9jEVLs/s400/Shared%2BMenubar-1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/*&lt;br /&gt;/* File: FlipThreeHeads.java&lt;br /&gt; * ----------------------------&lt;br /&gt; * This program simulates flipping coin repeatedly until you get 3 heads in a row, at which point you&lt;br /&gt; * print how many flips it took to get those 3. It uses an instance of the RandomGenerator class and keeps count using a local variable 'count.'&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import acm.util.*;&lt;br /&gt;public class FlipThreeHeads extends ConsoleProgram{&lt;br /&gt; &lt;br /&gt;    public void run() {&lt;br /&gt;     &lt;br /&gt;        // Placeholders X, Y and Z to store the most recent flip and the past 2 flips (when they happen):&lt;br /&gt;        String flipX = flipCoin(); &lt;br /&gt;        println(flipX);&lt;br /&gt;        String flipY = null;&lt;br /&gt;        String flipZ = null;&lt;br /&gt;        &lt;br /&gt;        // Local variable to keep track of how many flips we've done so far:&lt;br /&gt;        int count = 1;&lt;br /&gt;  &lt;br /&gt;       //"As long as X, Y and Z aren't equal to each other and aren't 'heads'..."&lt;br /&gt;        while ((flipX != flipY) || (flipX != flipZ) || (flipZ != "heads")){&lt;br /&gt;            count += 1;&lt;br /&gt;            String nextCoin = flipCoin();&lt;br /&gt;            println(nextCoin);&lt;br /&gt;            //Shove down the values of the placeholders to make flipX the most recent result  &lt;br /&gt;            flipZ = flipY;&lt;br /&gt;            flipY = flipX;&lt;br /&gt;            flipX = nextCoin;&lt;br /&gt;        }&lt;br /&gt;        println("It took " + count + " tries to get 3 consecutive heads.");  &lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    private RandomGenerator rgen = RandomGenerator.getInstance();&lt;br /&gt;&lt;br /&gt;    private String flipCoin() {&lt;br /&gt;        boolean booleanResult = rgen.nextBoolean();&lt;br /&gt;  &lt;br /&gt;        String flipResult;&lt;br /&gt;        if (booleanResult == true) {&lt;br /&gt;            flipResult =  "heads";&lt;br /&gt;        } else {&lt;br /&gt;            flipResult = "tails";&lt;br /&gt;        }&lt;br /&gt;        return flipResult;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What made this hard/interesting:&lt;br /&gt;&lt;br /&gt;Naming things is hard! Because this program requires that you keep track of the 3 most recent flips so that you can compare them and see if there were 3 heads in a row, you have to create a lot of variables. However, I kept being tempted to use variable names like "flipCoin" and "nextFlip" since they all do similar things related to the actual coin flip. If there's a way to make this program more spartan and not use as many variables that would be great.&lt;br /&gt;&lt;br /&gt;Here's how a run of the program works and each step needs its own variable or set of variables:&lt;br /&gt;&lt;br /&gt;1) You establish 3 local variables "&lt;b&gt;flipX&lt;/b&gt;," "&lt;b&gt;flipY&lt;/b&gt;" and "&lt;b&gt;flipZ&lt;/b&gt;." As we keep flipping our programmatic coin these will keep track of the 3 latest flips. We will build a shove-over mechanism so that with each new coin flip, we'll turn flipX into the latest result, turn flipY into the old value for flipX, and turn flipZ into the old value for flipY.&lt;br /&gt;&lt;br /&gt;Also, you create flipX with an initial value by calling the flipCoin() method.&lt;br /&gt;&lt;br /&gt;2) The&lt;b&gt; flipCoin()&lt;/b&gt; method is a private method that we call that just simulates flipping a coin. It works by pulling a random boolean out of our RandomGenerator instance and translating the returned "true" or "false" into "heads" or "tails."&lt;br /&gt;&lt;br /&gt;3) &lt;b&gt;booleanResult&lt;/b&gt; is a local variable&amp;nbsp;inside the flipCoin() method&amp;nbsp;that stores the "true" or "false" that gets returned from calling nextBoolean(). &lt;b&gt;flipResult&lt;/b&gt; is a local variable that takes the boolean and stores the "head" or "tail" translation.&lt;br /&gt;&lt;br /&gt;4) &lt;b&gt;nextCoin&lt;/b&gt; is part of our main method and always stores the most recent flip. We keep resetting it as we keep on flipping coins (and resetting flipX to equal it and going on with the shove-down mechanism&amp;nbsp;described&amp;nbsp;in step (1).&lt;br /&gt;&lt;br /&gt;The "flipCoin" method was particularly hard to make up names for inside because there are 3 variables that mean essentially the same thing (those that turned out to be called "flipResult," "booleanResult" and "flipCoin." I kept on being tempted to name them "flipResult", but all the distinct variables are necessary in order to return the desired result.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5733767852815951748?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5733767852815951748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/08/program-to-flip-coin-until-you-get-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5733767852815951748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5733767852815951748'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/08/program-to-flip-coin-until-you-get-3.html' title='Program to Flip a Coin (until you get 3 &quot;heads&quot; in a row):'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-j0-sPe7xA7w/Tj8nCjDJi8I/AAAAAAAAAgY/dPdkB9jEVLs/s72-c/Shared%2BMenubar-1.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5772634603633791182</id><published>2011-07-31T01:37:00.006-04:00</published><updated>2011-08-03T23:51:38.822-04:00</updated><title type='text'>Program to Draw a Random Card</title><content type='html'>Problem: Write a program that displays the name of a card randomly chosen from a complete deck of 52 cards. You program should display the complete name of the card. (Roberts ch 6, problem 1).&lt;br /&gt;&lt;br /&gt;What it looks like (2 sample runs):&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Zf2JQIpNUIw/ThpZK7iz1nI/AAAAAAAAAgQ/gETBG9ELu-8/s1600/DrawCard.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="133" src="http://3.bp.blogspot.com/-Zf2JQIpNUIw/ThpZK7iz1nI/AAAAAAAAAgQ/gETBG9ELu-8/s400/DrawCard.jpg" width="268" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;How the code looks:&lt;br /&gt;&lt;pre class="brush:java;"&gt;/*&lt;br /&gt;/*File: DrawCard.java&lt;br /&gt; * This program simulates drawing a card from a standard deck. It uses an instance of acm.util's &lt;br /&gt; * RandomGenerator class to create two new random integers: one helping to supply the value (2-10, Ace, Jack, Queen, &lt;br /&gt; * King), and the other integer helps determine the suit (Spades, Clubs, Hearts, Diamonds). Each integer value maps&lt;br /&gt; * to a card value or suit.&lt;br /&gt; *  * */&lt;br /&gt;import acm.program.*;&lt;br /&gt;import acm.util.*;&lt;br /&gt;&lt;br /&gt;public class DrawCard extends ConsoleProgram{&lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;   &lt;br /&gt;    int d1 = rgen.nextInt(1,4);&lt;br /&gt;    int d2 = rgen.nextInt(1,12);&lt;br /&gt;   &lt;br /&gt;        String suit = null; //Declare a variable of null value. &lt;br /&gt;        //Then use switch statement to set a value&lt;br /&gt;        switch (d1) {&lt;br /&gt;            case 1: suit = "Spades"; break;&lt;br /&gt;            case 2: suit = "Clubs"; break;&lt;br /&gt;            case 3: suit = "Hearts"; break;&lt;br /&gt;            case 4: suit = "Diamonds"; break;  &lt;br /&gt;        }&lt;br /&gt;   &lt;br /&gt;        String value = null;&lt;br /&gt;        switch (d2) { &lt;br /&gt;            case 1: value = "Ace"; break;&lt;br /&gt;            case 11: value = "Jack"; break;&lt;br /&gt;            case 12: value = "Queen"; break;&lt;br /&gt;            case 13: value = "King"; break;&lt;br /&gt;            default: value = Integer.toString(d2); break; // Returns a numeric value in string form&lt;br /&gt;                // if a non-face card gets drawn&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;    println("The " + value + " of " + suit);&lt;br /&gt;   &lt;br /&gt;    }&lt;br /&gt;    //Instance variable for our random generator macheeeeen&lt;br /&gt;    private RandomGenerator rgen = RandomGenerator.getInstance();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What made this interesting:&lt;br /&gt;&lt;br /&gt;Two interesting concepts you exercise here:&lt;br /&gt;&lt;br /&gt;1) Compartmentalizing your program. Instead of just storing 52 potential card values and randomly picking one of them, you can build something neater. There are 4 suits of equal distribution and 13 numeric values, so we can generate two random numbers for the face and numeric numbers and map them to card values. This is better than simply storing 52 potential card values because...&lt;br /&gt;&lt;br /&gt;2) The concept of using an instance of a class to do stuff. In this case is a class of objects available to me called RandomGenerator. An instance of the RandomGenerator class is not one random number itself. It is an object that you can create in your program and then call upon to return random numbers. (It will recognize the method getInt() method). So the individual random numbers are a layer of abstraction away from the class that we get from the ACM library.&lt;br /&gt;&lt;br /&gt;Why is this important?&lt;br /&gt;&lt;br /&gt;This was actually familiar to me based on my work at Twilio. Twiio's REST API lets you make calls, send SMS messages, and pull information about your Twilio account. You can  make a request to the API by a raw HTTP request to Twiio. Or, you can be clever and use a wrapper library that creates a "REST client". This is actually a class of objects that know how to make HTTP requests to Twilio, so instead of piecing together a big URL every time you need to communicate to Twilio, you can send commands to your instance of the REST client and communicate via methods instead of a raw URL.&lt;br /&gt;&lt;br /&gt;Examples: &lt;br /&gt;&lt;br /&gt;A1) Making a phone call directly via a direct HTTP POST to Twilio's API:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;POST https://api.twilio.com/2010-04-01/Accounts/account123/Calls/&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Auth Headers: username account123, password token456&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;POST parameters: &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* To: 503-111-2222&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* From: 503-222-3333&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* Url: http://mycoolapp.renee.com&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;A2) Sending an SMS:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;POST https://api.twilio.com/2010-04-01/Accounts/account123/SMS/Messages&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Auth Headers: username account123, password token456&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;POST parameters: &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* To: 503-111-2222&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* From: 503-222-3333&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;* Body: Hello World&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;VERSUS&lt;br /&gt;&lt;br /&gt;B) Making a phone call, then sending an SMS message using an instance of the TwilioClient class (from &lt;a href="http://code.google.com/p/twilio-java-client/"&gt;Sean Sullivan's Java wrapper library)&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/*import twilio.client.*;&lt;br /&gt;TwilioClient c = new TwilioClient("account123", "token456");&lt;br /&gt;&lt;br /&gt;c.call("503-111-2222", "503-222-3333");&lt;br /&gt;c.sendSMSMessage("503-111-2222", "503-222-3333", "Hello world");&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;See, isn't that better? The advantage is don't have to put together the URL every time or worry about validating each request. You just create your object, which has the account SID and auth token baked in and can recognize the methods "call" and "sendSMSMessage." Instead of needing to be familiar with the Twilio API (which is actually very simple), you can just be familiar with methods in your language of choice that let you make calls or send SMS.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5772634603633791182?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5772634603633791182/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/07/problem-write-program-that-displays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5772634603633791182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5772634603633791182'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/07/problem-write-program-that-displays.html' title='Program to Draw a Random Card'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-Zf2JQIpNUIw/ThpZK7iz1nI/AAAAAAAAAgQ/gETBG9ELu-8/s72-c/DrawCard.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5929668425509359523</id><published>2011-07-10T21:50:00.002-04:00</published><updated>2011-07-10T21:51:57.344-04:00</updated><title type='text'>Program to Print Perfect Numbers</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;Problem: Greek mathematicians took special interest in numbers that are equal to the sum of their proper divisors (a proper divisor of n is any divisor less than n itself). They called such numbers *perfect numbers*. For example, 6 is a perfect number because it is the sum of 1, 2 and 3, which are numbers less than 6 that divide equally into 6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7, and 14.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;Write a predicate method isPerfect(n) that returns *true* if the integer n is perfect, and *false* otherwise. Test your implementation by writing a main program that uses the isPerfect method to check for perfect numbers in the range 1 to 9999 by testing each number in turn. Whenever it identifies a perfect number, your program should display that number on your screen. Your program should find two other perfect numbers in that range as well. (Roberts ch 5, problem 12).&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;What it looks like:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-mhsx_KW26b0/TgGMW79EjgI/AAAAAAAAAgI/xiM6m-234j0/s1600/printPerfects.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="146" src="http://4.bp.blogspot.com/-mhsx_KW26b0/TgGMW79EjgI/AAAAAAAAAgI/xiM6m-234j0/s320/printPerfects.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/*&lt;br /&gt;* File: printPerfects.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: &lt;br /&gt;* This program prints the numbers between 1 and 9999 that are perfect numbers. Perfect numbers are those&lt;br /&gt;* where the divisors (integers that divide evenly into it) all sum up to that number. It uses the private&lt;br /&gt;* predicate method isPerfect.&lt;br /&gt;* &lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class printPerfects extends ConsoleProgram {&lt;br /&gt;    public void run() {    &lt;br /&gt;        for (int i = 1; i &amp;lt; 9999; i++) {&lt;br /&gt;            if (isPerfect(i)) {&lt;br /&gt;                println(i);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    private boolean isPerfect (int n) {&lt;br /&gt;        int sum = 0;&lt;br /&gt;        for (int i = 1; i &amp;lt; n; i++) {&lt;br /&gt;            if (n % i == 0) {&lt;br /&gt;            sum += i; }&lt;br /&gt;            }&lt;br /&gt;            if (sum == n) {&lt;br /&gt;                return true;&lt;br /&gt;            }&lt;br /&gt;        else return false;&lt;br /&gt;        &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This was a simple little program that simply re-enforced the practice of breaking certain operations of a program into their own methods. The method "isPerfect" creates a new variable "sum", initialized at a value of zero. For any number that you're testing to see if it is perfect, you find all the divisors via brute force. Whenever you find a divisor, you add it to "sum." If, after finding all the divisors, the value of "sum" equals the number you're testing, then you know that the number is perfect.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5929668425509359523?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5929668425509359523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/07/program-to-print-perfect-numbers.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5929668425509359523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5929668425509359523'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/07/program-to-print-perfect-numbers.html' title='Program to Print Perfect Numbers'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-mhsx_KW26b0/TgGMW79EjgI/AAAAAAAAAgI/xiM6m-234j0/s72-c/printPerfects.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-1498665557380372735</id><published>2011-06-19T15:08:00.001-04:00</published><updated>2011-06-19T18:18:11.976-04:00</updated><title type='text'>Program to Find Whether a Number is Prime</title><content type='html'>Problem: "An integer greater than 1 is said to be *prime* if it has no divisors other than itself and one. The number 17, for example, is prime because it has no factors other than 1 and 17. The number 91, however, is not prime because it is divisible by 7 and 13. Write a predicate methode isPrime(n) that returns *true* if the integer n is prime, and *false* otherwise. As an initial strategy implement isPrime using a brute-force algorithm that simply tests every possible divisor. Once you have that version working, try to come up with improvements to your algorithm that increase its efficiency without sacrificing its correctness." (Roberts ch 5, problem 11).&lt;br /&gt;&lt;br /&gt;Code: &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java;"&gt;/*&lt;br /&gt;* File: isPrimeSmarty.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: &lt;br /&gt;* Description: This program lets the user enter in as many positive integers as she likes and calls the &lt;br /&gt;* private boolean "isPrimeSmarty" to evaluate whether the integer is prime or not. We also had the &lt;br /&gt;* isPrimeBrute method evaluate it, but optimized the method so that instead of testing all divisors&lt;br /&gt;* you only have to iterate up to the square root of the tested number.&lt;br /&gt;* &lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class primeTesterSmarty extends ConsoleProgram {&lt;br /&gt;   &lt;br /&gt;    public void run() {&lt;br /&gt;        println("This program evaluates whether positive integer is prime or not.");&lt;br /&gt;        &lt;br /&gt;        while (true) {        &lt;br /&gt;            int number = readInt("Enter any positive integer, and enter 0 to stop:");&lt;br /&gt;                if (number == 0) {&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;                if (number &amp;lt; 0) {&lt;br /&gt;                    println ("Positive integer please! Why are you messin with me, thug? No more primes for you.");&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;                if (isPrimeSmarty(number)) {&lt;br /&gt;                    println ("Yes," + number + " is a prime number.");&lt;br /&gt;                }&lt;br /&gt;            else println ("Nope, not a prime number.");&lt;br /&gt;            }&lt;br /&gt;        println("Thanks for playing.");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private boolean isPrimeSmarty(int number) {&lt;br /&gt;        int k = (int)Math.sqrt(number); //Find the square root of the number you're testing for, &lt;br /&gt;        //return it rounded down to the integer&lt;br /&gt;        for (int i = 2; i &amp;lt; k; i++){&lt;br /&gt;            if (number % i == 0) {&lt;br /&gt;                return false;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    // The old brute force way, commented out&lt;br /&gt;    //private boolean isPrimeBrute (int number){&lt;br /&gt;    //    for (int i = 2; i &amp;lt; number; i++){&lt;br /&gt;    //        if (number % i == 0) {&lt;br /&gt;    //            return false;&lt;br /&gt;    //        }&lt;br /&gt;    //    }&lt;br /&gt;    //    return true;&lt;br /&gt;    //}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-WJsbSTjRaNg/Tf5zLBNAlUI/AAAAAAAAAgE/x0c9uBw0fGA/s1600/primeTesterSmarty-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="111" src="http://1.bp.blogspot.com/-WJsbSTjRaNg/Tf5zLBNAlUI/AAAAAAAAAgE/x0c9uBw0fGA/s320/primeTesterSmarty-1.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="background-color: transparent; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Consolas;"&gt;&lt;span class="Apple-style-span" style="font-size: 15px; white-space: pre-wrap;"&gt;It's fun to solve problems and methodically optimize them as you think about them more, or as you learn more revisit old problems with new knowledge. This problem had a brute method solution and two potential optimizations, one that I implemented and one that I think I need to hold off on implementing until I learn more.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: transparent; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: transparent; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Brute Method:&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: transparent; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;For any given number, iterate from 2 to that number and divide your tested number by the iterator. Is there a remainder? If you ever find a divisor where the remainder ==0, then return false, because it was divisible by a number *other than* itself and 1. If you don’t find a divisor other than itself an one, then return true.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optimization1:&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: transparent; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Instead of iterating from 2 to the tested number, you only need to go from 1 to the square root (rounded down to the integer) that number, so it cuts down the number of operations you have to do. &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Why are we able to only test up to the square root? &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Think of the tested number 21 for example. &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Its divisors are: [1, 3, 7, 21]. &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The square root is 4.58. Under the brute force way we'd test all numbers between and including 2 and 20. Now we just test the numbers between and including 2 and 4. When we test dividing 21 by 3, the result is 7, so if we were to test out 7, we'd get the result of 3. Divisors come in pairs, and one is always less than and the other is always greater than the square root, and the square root’s value is in the middle of the list of divisors. If there was a number greater than the square root that would have shown us that the tested number is not a prime, we would have already caught it by dividing its smaller side of the pair.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Optimization2:&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Once you’ve tested dividing a number by one of the iterators, you don’t need to test dividing it by a previously-tested-iterator to the n. So if you already tested 3, then you don’t need to test for 9, because if it’s divisible by 9 (aka 3 to the ^2) it’s definitely divisible by 3.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;I’m not sure how to implement this second optimization. I suspect you’d have to mess with the line “for (int i = 2; i &amp;lt; k; i++)”, and instead of “i++” (iterating up by increments of 1) you’d want to iterate up but exclude iterators that have a previous iterator as one of its factors.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Consolas; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;I think the way you’d do this is by creating an array that is made up of prime numbers (so that no number has any of the others as a factor) and instead of doing “i++”, you’d iterate through each value in the array. However, you don’t really know how big the array has to be, since it depends on how high the number you need to test is, so this array will probably be built on the fly as you run the program (recursive function?) I think that the syntax to implement this optimization is beyond what I know so far with Java, but I’ll come back to it later...&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-1498665557380372735?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/1498665557380372735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/06/program-to-find-whether-number-is-prime.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1498665557380372735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1498665557380372735'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/06/program-to-find-whether-number-is-prime.html' title='Program to Find Whether a Number is Prime'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-WJsbSTjRaNg/Tf5zLBNAlUI/AAAAAAAAAgE/x0c9uBw0fGA/s72-c/primeTesterSmarty-1.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-1538161027954392921</id><published>2011-06-04T02:29:00.000-04:00</published><updated>2011-06-04T02:29:40.687-04:00</updated><title type='text'>Fib Sequence with Method</title><content type='html'>Problem: "The Fibonacci sequence, in which each new term is the sum of the preceding two, was introduced in Ch4, exercise 9. Rewrite the program requested in that exercise, changing the implementation so that your program calls a method "fibonacci(n) to calculate the nth Fibonacci number. In terms of the number of mathematical calculations required, is your new implementation more or less efficient than the one you used in Ch 4?" (Roberts Ch 5, problem 2).&lt;br /&gt;&lt;br /&gt;What it looks like when you run:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-fJIqfgfywSs/TenQzWszFAI/AAAAAAAAAgA/9rldnNL4AUY/s1600/FibSequenceMethods-1.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="173" width="400" src="http://4.bp.blogspot.com/-fJIqfgfywSs/TenQzWszFAI/AAAAAAAAAgA/9rldnNL4AUY/s400/FibSequenceMethods-1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the code:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: FibSequenceMethods.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: &lt;br /&gt;* -----------------&lt;br /&gt;* The Fibonacci sequence is defined as a sequence of numbers where each integer is the sum of *the two previous integers in the sequence.&lt;br /&gt;* &lt;br /&gt;* This program prompts the user for an integer N and calls the private, recursive method fib(n) &lt;br /&gt;* to print out the first "n" digits of the Fibonacci Sequence. &lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class FibSequenceMethods extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;    println("This program prints the first 'n' digits of the Fibbonacci Sequence.");&lt;br /&gt;         int n = readInt("How many digits do you want to print?");         &lt;br /&gt;         for (int i = 0; i &lt; n; i++) {&lt;br /&gt;          println("Fib of " + i + " is " + fib(i) + ".");&lt;br /&gt;         }&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;    &lt;br /&gt;    private int fib(int n) {&lt;br /&gt;     if (n &lt; 2) {&lt;br /&gt;         return n;&lt;br /&gt;     }&lt;br /&gt;         return fib(n - 1) + fib(n - 2);&lt;br /&gt;     }&lt;br /&gt;     &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;Here is the old version (not using a private method):&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;public class FibSequence extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;        //Getting the first two in the sequence started...&lt;br /&gt;        int n0 = 0;&lt;br /&gt;        int n1 = 1;&lt;br /&gt;        println(n0);&lt;br /&gt;        println(n1);&lt;br /&gt;&lt;br /&gt;        //Now kicking off the fun part by changing moving down the variable values; n1 becomes&lt;br /&gt;//n0, n2 becomes n1, and a new n2 value is created.&lt;br /&gt;&lt;br /&gt;        for (int i = 2; i &lt; END; i++) {&lt;br /&gt;            int n2 = n1 + n0;&lt;br /&gt;            println(n2);&lt;br /&gt;            n0=n1;&lt;br /&gt;            n1=n2;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //Private constant; "End" is the the nth number in the Fib sequence displayed.&lt;br /&gt;    private static final double END = 15;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;In the new version of the program, because the part that actually calculates each entry in the sequence is a separate method call, you can use a recursive method. In other words, when you're trying to find fib(6), you have at your disposal the ability to calculate fib(5) and fib(4) whereas you didn't before. The new version of the program is easier to write because the code looks like the way we coloquially define the fib sequence. However, it is not actually more efficient (there aren't fewer operations) than the old version because to arrive at any integer in the sequence, you have to calculate *all* the previous integers before it. I drew out the operations to calculate fib(6) using both programs, below.New Version:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-RwW9j4r8XfM/TenMNfbwqxI/AAAAAAAAAfo/-8T1u0zJRKM/s1600/fibOld.jpeg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="166" width="221" src="http://1.bp.blogspot.com/-RwW9j4r8XfM/TenMNfbwqxI/AAAAAAAAAfo/-8T1u0zJRKM/s400/fibOld.jpeg" /&gt;&lt;/a&gt;&lt;/div&gt;Old Version:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-v1vn_zFS3zA/TenPC3JSLBI/AAAAAAAAAf4/Y63w-rvZRHk/s1600/fibOld.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="300" width="400" src="http://1.bp.blogspot.com/-v1vn_zFS3zA/TenPC3JSLBI/AAAAAAAAAf4/Y63w-rvZRHk/s400/fibOld.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-1538161027954392921?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/1538161027954392921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/06/fib-sequence-with-method.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1538161027954392921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1538161027954392921'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/06/fib-sequence-with-method.html' title='Fib Sequence with Method'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-fJIqfgfywSs/TenQzWszFAI/AAAAAAAAAgA/9rldnNL4AUY/s72-c/FibSequenceMethods-1.jpg' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-4197884464815534976</id><published>2011-01-23T19:59:00.000-05:00</published><updated>2011-01-23T19:59:25.752-05:00</updated><title type='text'>Predicate Method for Yes/No: The David Version</title><content type='html'>After I posted my predicate method for categorizing yes/no questions as true or false, David challenged me to rewrite the solution with a single call to readLine(prompt) and a single test for reply.equals("yes").&lt;br /&gt;&lt;br /&gt;Here's my shot at it (tested and it works, oorah):&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;    private boolean askYesOrNoQ(String prompt){   &lt;br /&gt;        while (true) {&lt;br /&gt;            String reply = readLine(prompt);&lt;br /&gt;         &lt;br /&gt;            if (reply.equals("yes")) {&lt;br /&gt;                return true;&lt;br /&gt;            }&lt;br /&gt;            if (reply.equals("no")) {&lt;br /&gt;                return false;&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            println("Please enter a yes or no answer.");&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The old approach said, "As long as the user enters a non-yes/no answer, keep prompting him to enter yes or no; once he does, return true or false, respectively." This version says, "Keep looping. If he enters yes, break from the loop and return true. If he enters no, break from the loop and return false. Else, keep re-prompting and looping."&lt;br /&gt;&lt;br /&gt;I don't know if this was the strategy David was thinking of, but I'm glad I figured out how to solve a problem two ways. Comparing this method with the old version, I'm trying to figure out which one is better and why.&lt;br /&gt;&lt;br /&gt;For reference, here is the old version:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;private boolean askYesOrNoQ(String prompt){ &lt;br /&gt;        String reply = readLine(prompt);&lt;br /&gt;      &lt;br /&gt;        while ((!reply.equals("yes"))&amp;&amp;(!reply.equals("no"))) {&lt;br /&gt;            println("Please enter a yes or no answer.");&lt;br /&gt;            reply = readLine(prompt);&lt;br /&gt;        }&lt;br /&gt;        return (reply.equals("yes"));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Merits of the new version:&lt;br /&gt;&lt;br /&gt;1) Clearer to read; you don't really need to know the syntax of Java or how booleans work to know what's going on.&lt;br /&gt;&lt;br /&gt;2) Doesn't repeat the line "reply = readLine(prompt)". In general it's a good practice to avoid repeating code so you don't introduce bugs if you decide to change the name of the variable "reply", etc.&lt;br /&gt;&lt;br /&gt;Merits of the old version:&lt;br /&gt;&lt;br /&gt;1) Fewer lines of code. &lt;br /&gt;&lt;br /&gt;2) I like the clean and elegant "return" statement. Instead of breaking down the true or false possibilities into two "if" statements, we say return(boolean); it lets the program figure out true vs false for itself. I guess you could write, "If reply equals yes or no, return(boolean)", though that would violate the challenge of only inspecting reply.equals("yes") only once.&lt;br /&gt;&lt;br /&gt;What do you guys think? Also are there other ways of solving this problem?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-4197884464815534976?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/4197884464815534976/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/predicate-method-for-yesno-david.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4197884464815534976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4197884464815534976'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/predicate-method-for-yesno-david.html' title='Predicate Method for Yes/No: The David Version'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-687560858053761386</id><published>2011-01-23T01:15:00.005-05:00</published><updated>2011-01-23T01:24:08.120-05:00</updated><title type='text'>Predicate Method to Categorize a Yes or No Question (with Monkey Brains program)</title><content type='html'>Problem: "Write a predicate method "askYesNoQuestion(prompt)" that prints a string "prompt" as a question for the the user and then waits for a response. If the user enters a string "yes" the askYesNoQuestion method should return true. If the user enters a string "no" the askYesNoQuestion method should return false. If the user enters anything else, the method should remind the user that it is seeking a yes-or-no answer and then repeat the question." (Roberts ch 5, problem 7).&lt;br /&gt;&lt;br /&gt;This problem was a good illustration of how boolean methods work as part of a larger program. Let's say you want a program to say, "If True, do X, if False, do y," but the mechanics of evaluating a situation to "True" or "False" take multiple steps. If that's the case, then the "if" part would best be handled by a method call.&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_UagTlN5LQ1U/TTvBvfoT0jI/AAAAAAAAAfM/XOaR6FEPcjc/s1600/Screen%2Bshot%2B2011-01-22%2Bat%2B9.40.15%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="286" width="400" src="http://2.bp.blogspot.com/_UagTlN5LQ1U/TTvBvfoT0jI/AAAAAAAAAfM/XOaR6FEPcjc/s400/Screen%2Bshot%2B2011-01-22%2Bat%2B9.40.15%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;* File: YesNoPredicate.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: &lt;br /&gt;* -----------------&lt;br /&gt;* &lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class YesNoPredicate extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;        if (askYesOrNoQ ("Do you have monkey brains?")) {&lt;br /&gt;            println ("Have a bananna");&lt;br /&gt;        }&lt;br /&gt;        else println ("Get outta here");&lt;br /&gt;        }&lt;br /&gt;    &lt;br /&gt;    /*This boolean pushes the user to enter a yes or no answer to an arbitrary string (hopefully&lt;br /&gt;    a question) called "prompt." If the user enters anything but yes or no, the method&lt;br /&gt;    will keep looping until the user enters yes or no. If the user enters "yes", the boolean&lt;br /&gt;will return "true." If the user enters "no", the boolean will return "false."*/&lt;br /&gt;&lt;br /&gt;    private boolean askYesOrNoQ(String prompt){ &lt;br /&gt;        String reply = readLine(prompt);&lt;br /&gt;      &lt;br /&gt;        while ((!reply.equals("yes"))&amp;&amp;(!reply.equals("no"))) {&lt;br /&gt;            println("Please enter a yes or no answer.");&lt;br /&gt;            reply = readLine(prompt);&lt;br /&gt;        }&lt;br /&gt;        return (reply.equals("yes"));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What made it tough: At first I thought this problem was super-simple, but it stumped me for 30 min before everything clicked. I knew that I wanted the boolean to say, "If the user enters 'yes,' return 'true.' If he enters anything else, return 'false.' Moreover, if the false answer is not 'no', keep re-prompting for a yes-or-no answer." But I wasn't sure how to build that re-prompt into the predicate method and at first built the re-prompt into the run() method, which isn't the proper place because the predicate part is supposed to take care of that. &lt;br /&gt;&lt;br /&gt;I was getting close when it looked like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;    private boolean askYesOrNoQ(String prompt){ &lt;br /&gt;        String reply = readLine(prompt);&lt;br /&gt;        return (reply.equals("yes"));&lt;br /&gt;        if (!reply.equals("no")) {&lt;br /&gt;            println("Please enter a yes or no answer.");        &lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;but I kept getting an error "Unreachable code." I had the "return true if yes" part come before the "push the user for a yes-or-no answer" part, though it appears you must have the "return" method be the last part of your private method? That makes sense, because if the user entered "gibberish" and we returned "false", the run() method would act upon the "false" without ever getting a yes-or-no answer from the predicate method.&lt;br /&gt;&lt;br /&gt;This was also another good illustration of different ways to pass an object to a method. It's interesting that the argument you pass to the method which inspects the user's input isn't the answer itself, but the prompt. You could easily have designed this program so that you print out the prompt as part of the run() method and pass the user input to the private boolean to evaluate it to true or false; I'm trying to think what the merits of each approach are...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-687560858053761386?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/687560858053761386/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/predicate-method-to-categorize-yes-or.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/687560858053761386'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/687560858053761386'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/predicate-method-to-categorize-yes-or.html' title='Predicate Method to Categorize a Yes or No Question (with Monkey Brains program)'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_UagTlN5LQ1U/TTvBvfoT0jI/AAAAAAAAAfM/XOaR6FEPcjc/s72-c/Screen%2Bshot%2B2011-01-22%2Bat%2B9.40.15%2BPM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-8154380371226354395</id><published>2011-01-22T21:08:00.000-05:00</published><updated>2011-09-28T14:16:12.649-04:00</updated><title type='text'>Program to Display the Number of Digits in an integer</title><content type='html'>Problem: Write a method countDigits(n) that returns the number of digits in the integer 'n', which you may assume is positive. Design a main program to test your method. For hints about how to write this program, you might want to look back at the DigitSum program in Fig 4.6.&lt;br /&gt;&lt;br /&gt;What I did: This program interacts with the user, taking in the digit input and sending that input to the countDigits method. I also made the program let the user keep entering in digits to his or her hearts' content, using the value "0" as the sentinel. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_UagTlN5LQ1U/TTuNDg8DIYI/AAAAAAAAAfE/PRmSKuedZwI/s1600/Screen%2Bshot%2B2011-01-22%2Bat%2B6.04.37%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="194" width="400" src="http://3.bp.blogspot.com/_UagTlN5LQ1U/TTuNDg8DIYI/AAAAAAAAAfE/PRmSKuedZwI/s400/Screen%2Bshot%2B2011-01-22%2Bat%2B6.04.37%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;/*&lt;br /&gt;* File: DigitCountWithMethod.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: &lt;br /&gt;* Description: This exercise prints the number of digits in an integer that&lt;br /&gt;* the user enters in. The main/run method uses the digitCount method to &lt;br /&gt;* accomplish this.&lt;br /&gt;* &lt;br /&gt;* The countDigits method is pretty clever and reuses the technique from the DigitSum program&lt;br /&gt;* in the textbook from section 4.6. In order to count the number of digits in an &lt;br /&gt;* integer, you keep dividing the integer by 10 and adding 1 to a variable "dcount" that gets initiated&lt;br /&gt;* at 0. You keep doing this iteration until the integer is zero.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class DigitCountWithMethod extends ConsoleProgram {&lt;br /&gt; &lt;br /&gt;    public void run() {&lt;br /&gt;        println("This program counts the digits in an integer.");&lt;br /&gt;        //Hm, get "illegal numeric format" if it's a long integer...Java limitation? Eew.&lt;br /&gt;  &lt;br /&gt;        while (true) {&lt;br /&gt;            int n = readInt ("Enter any integer, entering '0' if you want to exit the program:");&lt;br /&gt;            if (n == 0) {&lt;br /&gt;                println("Goodbye");&lt;br /&gt;                break; //Oh, once you break, you can't continue in the loop; otherwise it's "unreachable code"       &lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;            println("There are " + countDigits(n) + " integers in " + n + ".");&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    private int countDigits(int n){&lt;br /&gt;        int dcount = 0;&lt;br /&gt;        while (n &gt; 0) {&lt;br /&gt;            dcount += 1;&lt;br /&gt;            n /= 10;&lt;br /&gt;        }&lt;br /&gt;        return dcount;&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What made it tough: Nothing much; a lot of the countDigits strategy came from another program, DigitSum. I do see 2 curious things however. First; if the user enters in more than 10 digits, we get an error message "Illegal numeric format." Why??&lt;br /&gt;&lt;br /&gt;Also, I wanted to let my program loop so that the user could enter in multiple digits; the only way I could think of how to do this was with establishing a sentinel with the value "0." However, this is not elegant at all; for one thing, what if I want to know how many digits zero has? However, I can't make the sentinel a string, since my prompt will only accept integers. Ideas on a better way for the user to terminate the program?&lt;style type="text/css"&gt;	#twilio-connect-button {		background: url(https://www.twilio.com/packages/connect-apps/images/connect-button.png);		width: 130px; height: 34px; display: block;	margin: 0 auto;	}	#twilio-connect-button:hover { background-position: 0 34px; }&lt;/style&gt;&lt;a href="https://www.twilio.com/authorize/CN1ae359b0275949ceb4ef8ed7132baa5f" id="twilio-connect-button"&gt;&lt;/a&gt;			&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-8154380371226354395?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/8154380371226354395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-to-display-number-of-digits-in.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8154380371226354395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8154380371226354395'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-to-display-number-of-digits-in.html' title='Program to Display the Number of Digits in an integer'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_UagTlN5LQ1U/TTuNDg8DIYI/AAAAAAAAAfE/PRmSKuedZwI/s72-c/Screen%2Bshot%2B2011-01-22%2Bat%2B6.04.37%2BPM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-3325633978634394172</id><published>2011-01-04T02:20:00.002-05:00</published><updated>2011-01-04T02:29:25.084-05:00</updated><title type='text'>2 Programs to Draw a Target Graphic</title><content type='html'>Problem: "Rewrite the Target program given in Ch2 so that it uses the createFilledCircle method that appears in Figure 5.3. In addition, change the program so that the target is always centered in the window and so that the number and dimensions of the circles are controlled y the following named constants:&lt;br /&gt;&lt;br /&gt;private static final int N_CIRCLES = 5;&lt;br /&gt;private static final double OUTER_RADIUS = 75;&lt;br /&gt;private static final double INNER_RADIUS = 10;&lt;br /&gt;&lt;br /&gt;(Roberts ch 5, problem 5)&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_UagTlN5LQ1U/TSLGs6g7NbI/AAAAAAAAAe8/iGzMYyb3h9s/s1600/Screen%2Bshot%2B2011-01-03%2Bat%2B11.04.50%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="400" width="346" src="http://3.bp.blogspot.com/_UagTlN5LQ1U/TSLGs6g7NbI/AAAAAAAAAe8/iGzMYyb3h9s/s400/Screen%2Bshot%2B2011-01-03%2Bat%2B11.04.50%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;What the code looks like: New Version:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * File: TargetWithMethod.java&lt;br /&gt; * Name: &lt;br /&gt; * Section Leader: &lt;br /&gt; * -----------------&lt;br /&gt;  * This is the new and improved version of the Target program. As with the old version, we draw &lt;br /&gt;  * the graphic by laying a bunch of concentric circles on top of each other. However, unlike the &lt;br /&gt;  *  old version, we draw the program with a "while" loop and a method to create the circles. The&lt;br /&gt;  *  static contants give us number of circles, the outer radius, and the inner radius. Once those are established, &lt;br /&gt;  *  we derive the x and y coordinates to center our target graphic and the width of each ring. Then we go into a &lt;br /&gt;  *  "while" loop to keep adding new concentric circles on top of each other until the innermost circle is too small &lt;br /&gt;  *  (the radius is smaller than the target's rings). At the end of each loop, we change the "radius" &lt;br /&gt;  *  varible to make the next, smaller circle and we switch the boolean so that the colors can switch from red&lt;br /&gt;  *  to white and back.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;package ch6_practice;&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class TargetWithMethod extends GraphicsProgram {&lt;br /&gt;    private static final int N_CIRCLES = 5;&lt;br /&gt;    private static final double OUTER_RADIUS = 75;&lt;br /&gt;    private static final double INNER_RADIUS = 10;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;    public void run() {&lt;br /&gt;  &lt;br /&gt;        int center_x = getWidth()/2;&lt;br /&gt;        int center_y = getHeight()/2;&lt;br /&gt;        double band_width = OUTER_RADIUS/N_CIRCLES;&lt;br /&gt;  &lt;br /&gt;        double radius = OUTER_RADIUS;&lt;br /&gt;        boolean is_colored_band = true;&lt;br /&gt;  &lt;br /&gt;        while (radius &gt;= band_width) {&lt;br /&gt;            if(is_colored_band) {&lt;br /&gt;                add(createFilledCircle(center_x, center_y, radius, Color.RED));&lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;                add(createFilledCircle(center_x, center_y, radius, Color.WHITE));&lt;br /&gt;            }   &lt;br /&gt;            is_colored_band = !is_colored_band;&lt;br /&gt;            radius -= band_width;&lt;br /&gt;       }&lt;br /&gt; &lt;br /&gt;   }&lt;br /&gt; &lt;br /&gt;    private GOval createFilledCircle (int x, int y, double r, Color color) {&lt;br /&gt;  &lt;br /&gt;        GOval circle = new GOval (x-r, y-r, 2*r, 2*r); //The method input becomes the center&lt;br /&gt;        circle.setColor(color);&lt;br /&gt;        circle.setFilled(true);&lt;br /&gt;        return circle;&lt;br /&gt;  &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Old Version:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;/*&lt;br /&gt; * File: Target.java&lt;br /&gt; * Name: &lt;br /&gt; * Section Leader: &lt;br /&gt; * -----------------&lt;br /&gt; * This is the old, static version of the target program. It makes three red concentric circles, named&lt;br /&gt; * "outer," "white," and "center." It uses the GOval method and the arguments for position and size are&lt;br /&gt; * explicitly hard-coded in.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class Target extends GraphicsProgram { &lt;br /&gt;    public void run() {&lt;br /&gt;    GOval outer = new GOval(75, 50, 100, 100);&lt;br /&gt;    outer.setColor(Color.RED);&lt;br /&gt;    outer.setFilled(true);&lt;br /&gt;    outer.setFillColor(Color.RED);&lt;br /&gt;    add (outer);&lt;br /&gt; &lt;br /&gt;    GOval white = new GOval(95, 70, 60, 60);&lt;br /&gt;    white.setColor(Color.white);&lt;br /&gt;    white.setFilled(true);&lt;br /&gt;    white.setFillColor(Color.white);&lt;br /&gt;    add (white);&lt;br /&gt; &lt;br /&gt;    GOval center = new GOval(115, 90, 20, 20);&lt;br /&gt;    center.setColor(Color.red);&lt;br /&gt;    center.setFilled(true);&lt;br /&gt;    center.setFillColor(Color.red);&lt;br /&gt;    add (center);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;How long it took: 40 min&lt;br /&gt;&lt;br /&gt;What makes the old version not-so-good: We had to hard-code in the coordinates for each of the rings, which is a pain to figure out. If we miscalculate, or if later on we want each of the rings to be a bit bigger/smaller, we have to redo a bunch of annoying arithmetic over again.&lt;br /&gt;&lt;br /&gt;Later on I redid this code making the x and y coordinates variables that used the getHeight() and getWidth() methods, so the position of the board depended on the graphics window we were in. This was a bit better. However, the version that uses the createFilledCircle method is even better because besides always being sure the target is centered, we can be flexible with the number of rings, the overall size of the target, and the width of each ring.&lt;br /&gt;&lt;br /&gt;What made it difficult: Nothing too difficult here; I did get stumped at the part where you switch the color from red to white. I knew that I wanted to make it the last step in my loop to switch the boolean, but I didn't know that you can switch booleans with the "!" operator (like this: "is_colored_band = !is_colored_band;" ), thanks Jane/Jason for the tip. I was going to go about it with 1 and -1, but this is much better.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-3325633978634394172?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/3325633978634394172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/2-programs-to-draw-target-graphic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/3325633978634394172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/3325633978634394172'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/2-programs-to-draw-target-graphic.html' title='2 Programs to Draw a Target Graphic'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_UagTlN5LQ1U/TSLGs6g7NbI/AAAAAAAAAe8/iGzMYyb3h9s/s72-c/Screen%2Bshot%2B2011-01-03%2Bat%2B11.04.50%2BPM.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-3051563919881921706</id><published>2011-01-03T01:57:00.000-05:00</published><updated>2011-01-03T01:57:19.602-05:00</updated><title type='text'>Program to Print the First 10 values 2 to the kth Power</title><content type='html'>Problem: Write a method raiseIntToPower that takes two integers, "n" and "k" and returns n to the kth power. Use your method to display a table of values 2 to the kth power for all values of k from 0 to 10. (Robers ch 5, problem 3).&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_UagTlN5LQ1U/TSFxldw541I/AAAAAAAAAe0/z2W9eKrnY64/s1600/Screen%2Bshot%2B2011-01-02%2Bat%2B10.49.21%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="226" width="174" src="http://2.bp.blogspot.com/_UagTlN5LQ1U/TSFxldw541I/AAAAAAAAAe0/z2W9eKrnY64/s400/Screen%2Bshot%2B2011-01-02%2Bat%2B10.49.21%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;How long it took: 30 min or so&lt;br /&gt;&lt;br /&gt;What made it difficult: There are two loops in this problem. One says "From i being 0 to 10, print 2 to the i." The second loop is inside the private method, and it says "From i = 0 to the specified kth power, multiply the base by itself." Both loops use the same variable "i," and I can see a program like this getting confusing. However, it's unavoidable that you'll use the same variable name within a program; that I assume is because methods are built to be re-usable, so you'll never know which program you'll plunk them into.&lt;br /&gt;&lt;br /&gt;I felt pretty comfortable with this re-use of "i" because both the book and Mehran's lecture went through tracing a program similar to this throughout multiple method calls and parameter passes. Going through strack frames in excruciating detail like this made me really aware of the "life cycle" of a program. Definitely a good foundation when learning about using private methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: RaisePowerWithMethod.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: &lt;br /&gt;* Description: This program asks us to write a program that writes out 2 to the nth power for&lt;br /&gt;* n = 1 to n = 10. We accomplish this using the private method 'raiseIntToPower', which can take in&lt;br /&gt;* any base and any raised power.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class RaisePowerWithMethod extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;        for(int i = 0; i &lt; 10; i++) {&lt;br /&gt;            println (raiseIntToPower(2, i));&lt;br /&gt;        }  &lt;br /&gt;  &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private int raiseIntToPower(int n, int k) {&lt;br /&gt;     int result = 1;&lt;br /&gt;     for (int i = 0; i &lt; k; i++) {&lt;br /&gt;      result *=n;      &lt;br /&gt;     }&lt;br /&gt;     return result;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-3051563919881921706?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/3051563919881921706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-to-print-first-10-values-2-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/3051563919881921706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/3051563919881921706'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-to-print-first-10-values-2-to.html' title='Program to Print the First 10 values 2 to the kth Power'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_UagTlN5LQ1U/TSFxldw541I/AAAAAAAAAe0/z2W9eKrnY64/s72-c/Screen%2Bshot%2B2011-01-02%2Bat%2B10.49.21%2BPM.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-6694634711138709848</id><published>2011-01-01T21:08:00.003-05:00</published><updated>2011-01-01T22:12:07.914-05:00</updated><title type='text'>Program (and method!) to evaluate Quadratic Equation.</title><content type='html'>Problem: "In high-school algebra you learned that the standard quadratic equation (ax^2 + bx + c = 0) has two solutions given by the formula: x = (-b +/- sqrt(b^2 -4ac)) / 2a.&lt;br /&gt;&lt;br /&gt;The first solution is obtained by using + in place of +/-. The second is obtained by using - in place of +/-.&lt;br /&gt;&lt;br /&gt;Write a Java program that accepts values for "a", "b" and "c", and then calculates the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assume that "a" is nonzero." (Roberts ch 5, problem 1).&lt;br /&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_UagTlN5LQ1U/TR_oAhPooxI/AAAAAAAAAes/mkvlB7HoowI/s1600/Screen%2Bshot%2B2011-01-01%2Bat%2B6.49.53%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="166" width="377" src="http://4.bp.blogspot.com/_UagTlN5LQ1U/TR_oAhPooxI/AAAAAAAAAes/mkvlB7HoowI/s400/Screen%2Bshot%2B2011-01-01%2Bat%2B6.49.53%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;This chapter is all about methods: Understanding how methods work within the context of a bigger program, writing your own, and re-writing old homework problems to use methods. I think I'll cement a lot of what I've learned about what makes code efficient/flexible. &lt;br /&gt;&lt;br /&gt;I solved the problem by calling a method that I write. The "run" main method is there to take in the user inputs and print out the two answers. The method "quadratic" actually does the calculation.&lt;br /&gt;&lt;br /&gt;How long it took: 1.5 hrs&lt;br /&gt;&lt;br /&gt;What made it difficult: As a straight-up program, this wouldn't be hard; it's just a lot of arithmetic and "println" methods.&lt;br /&gt;&lt;br /&gt;However, making "quadratic" a method made things a bit more complicated. First, there are two solutions to any quadratic forumla, but a method only can return one result. Arnab suggested fixing this by taking in "sign" as an argument to the method, and so I wrote a program that calls the method twice, once by entering -1 and once by entering 1.&lt;br /&gt;&lt;br /&gt;Second, if the chunk under the square root evaluates to negative, there are no real number solutions, so I have to print out a string "There are no real number solutions." However, a method has to have a type (string, integer, etc), and my method type is a double, so I couldn't return a string. Arnab suggested using null-- null is becoming quite my friend these days.&lt;br /&gt;&lt;br /&gt;Lingering questions:&lt;br /&gt;&lt;br /&gt;1) I don't like that the part which checks for the square root chunk being negative only inspects "plus_sign_answer", even though it works. Is there a more elegant way about this?&lt;br /&gt;2) I don't like that the "int sign" argument MUST take in -1 or 1; if this method was used in a program that put in, say, -100, it would return an inaccurate answer. Is there a way to stop this?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: QuadradicEquation.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: Prof. Arnab&lt;br /&gt;* Description: This is a program to evaluate the quadradic equation, giving inputs a, b and c.&lt;br /&gt;* The 'run' method takes in the user inputs, then it calls the private method 'quadradic'.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;package ch6_practice;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class QuadradicEquation extends ConsoleProgram {&lt;br /&gt;    &lt;br /&gt;    public void run (){&lt;br /&gt;        println("Enter the coefficients for the quadradic equation:");&lt;br /&gt;        int a = readInt("a:");&lt;br /&gt;        int b = readInt("b:");&lt;br /&gt;        int c = readInt("c:");&lt;br /&gt;        &lt;br /&gt;        Double plus_sign_answer = quadradic(a, b, c, 1);&lt;br /&gt;        Double minus_sign_answer = quadradic(a, b, c, -1);&lt;br /&gt;        if (plus_sign_answer != null) { //This isn't totally elegant because it only checks the &lt;br /&gt;         //first answer, even though both the first and second solutions check for null...&lt;br /&gt;        &lt;br /&gt;            println("The first solution is " + plus_sign_answer);&lt;br /&gt;            println("The second solution is " + minus_sign_answer);&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            println("There are no real number solutions.");&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;        &lt;br /&gt;    &lt;br /&gt;    private Double quadradic(int a, int b, int c, int sign) { //'sign' takes -1 or 1&lt;br /&gt;       &lt;br /&gt;     &lt;br /&gt;        double sqrt_part = Math.sqrt(b*b - 4*a*c);&lt;br /&gt;        int neg_b = b*-1;&lt;br /&gt;        int two_a = 2*a;        &lt;br /&gt;        double solution = (neg_b + (sign * sqrt_part))/two_a;&lt;br /&gt;        &lt;br /&gt;        if (sqrt_part &lt; 0) {&lt;br /&gt;            return null;&lt;br /&gt;            &lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        else {&lt;br /&gt;            return solution;&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-6694634711138709848?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/6694634711138709848/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-and-method-to-evaluate.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6694634711138709848'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6694634711138709848'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2011/01/program-and-method-to-evaluate.html' title='Program (and method!) to evaluate Quadratic Equation.'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_UagTlN5LQ1U/TR_oAhPooxI/AAAAAAAAAes/mkvlB7HoowI/s72-c/Screen%2Bshot%2B2011-01-01%2Bat%2B6.49.53%2BPM.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-8106113164919669752</id><published>2010-12-28T00:12:00.001-05:00</published><updated>2010-12-28T00:16:33.546-05:00</updated><title type='text'>Even-cleaner Checkerboard code</title><content type='html'>After I finished the checkerboard code, it really pained me to reuse the code for the inner loop that checked whether a square is an "every other" square (ie if ((i+j) % 2 != 0) ). We have to use this both for when determining if a square should be filled, and if a checker belongs in the spot.&lt;br /&gt;&lt;br /&gt;I got a couple of suggestions to avoid repeating code. Bill simply suggested storing the boolean in a local variable, which makes a lot of sense; I'm still getting the hang of how booleans work in structure, so it was good to know that I could store the logic that way.&lt;br /&gt;&lt;br /&gt;Usman suggested initiating the "checker" variable outside the loop that used the boolean and set the value of "checker" to null at first. Then within the loop, I could change the value of "checker" to be a new object and set it to a proper position and color. At the end of the loop, if the value of "checker" has been changed form "null" to a new object, plop that sucker down. This lets me avoid the problem of plopping the square down after the checker (and covering the checker up.)&lt;br /&gt;&lt;br /&gt;I rewrote the code according to Usman's suggestion, and it worked! Yippee! I hadn't known what null was, but it seems to be handy. It sounds like "null" is just a placeholder that is totally neutral, so Java won't object if we set a GOval variable object equal to null at first, and we can check whether the variable is or isn't "null" after the loop.&lt;br /&gt;&lt;br /&gt;However, Usman mentioned that it's not good to use "null" a whole lot. I wonder why....&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: CheckerboardWithoutRepeatedCode.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: Usman. He rules&lt;br /&gt;* Description: This program draws a checkerboard graphic and checkers on it in the original starting position.&lt;br /&gt;* To draw the board, we use the GRect class, with the "x" and "y" coordinates set by the index of the loop&lt;br /&gt;* as well as the square size (determined by private constants).  The checkers use the GOval class, and since they &lt;br /&gt;* are right on top of the squares, they can use the same "x" and "y" coordinates. To determine if the squares&lt;br /&gt;* should be filled in, we use the boolean ((i+j) % 2 != 0), which resolves to true every other square by&lt;br /&gt;* row and column. We reuse that test for determining whether a checker should be set down.&lt;br /&gt;* &lt;br /&gt;* I used Usman's suggestion of initiating a checker outside the inner loop with the boolean and setting it &lt;br /&gt;* equal to null at first, and if the inner loop determines that there should be a checker, we'll change the&lt;br /&gt;* "checker" variable from "null" to a new GOval object. We can check at the end of the loop whether "checker"&lt;br /&gt;* is null or not, and if not, then plot it on the board in the right place.&lt;br /&gt;* ------------------&lt;br /&gt;*/&lt;br /&gt; &lt;br /&gt;package Ch4Practice;&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class CheckerboardWithoutRepeatedCode extends GraphicsProgram {&lt;br /&gt; &lt;br /&gt;    //private constants&lt;br /&gt;    private static final int N_ROWS = 8;&lt;br /&gt;    private static final int N_COLUMNS = 8; &lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;        double sqSize = (double) getHeight() / N_ROWS; &lt;br /&gt;        double boardWidth = N_COLUMNS * sqSize; &lt;br /&gt;&lt;br /&gt;        for (int i = 0; i &lt; N_ROWS; i++) { //for the outer rows&lt;br /&gt;            for (int j = 0; j &lt; N_COLUMNS; j++) { //for the inner rows&lt;br /&gt;                double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;                double y = i* sqSize;&lt;br /&gt;   &lt;br /&gt;                //Starting off the checkerboard:&lt;br /&gt;                GRect sq = new GRect(x, y, sqSize, sqSize); //Make up a new square with the proper coordinates&lt;br /&gt;   &lt;br /&gt;                //Initiate the variable checker at null.&lt;br /&gt;                GOval checker = null;    &lt;br /&gt;    &lt;br /&gt;                if ((i+j) % 2 != 0) { //If it's every other square, both by row and column&lt;br /&gt;                    sq.setFilled(true); //Make it filled&lt;br /&gt;          &lt;br /&gt;                    //Now for the checkers:&lt;br /&gt;                    checker = new GOval (x, y, sqSize, sqSize); //Making the checker NOT null, if it appropriate&lt;br /&gt;                    checker.setFilled(true);&lt;br /&gt;     &lt;br /&gt;                    if ((i &lt; 3)) {//If it's every other AND the first three rows, make red checkers &lt;br /&gt;                        checker.setFillColor(Color.RED);&lt;br /&gt;      &lt;br /&gt;                    }&lt;br /&gt;                    else if (N_ROWS-4 &lt; i) { //If it's every other AND the last 3 rows, make white checkers&lt;br /&gt;                        checker.setFillColor(Color.WHITE); &lt;br /&gt;      &lt;br /&gt;                    } &lt;br /&gt;  &lt;br /&gt;                }&lt;br /&gt;    &lt;br /&gt;                add(sq); //Plop that square down&lt;br /&gt;                if (checker != null) {&lt;br /&gt;                    add(checker); //Plop that checker down-- BAM!&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt; &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-8106113164919669752?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/8106113164919669752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/12/even-cleaner-checkerboard-code.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8106113164919669752'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8106113164919669752'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/12/even-cleaner-checkerboard-code.html' title='Even-cleaner Checkerboard code'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2423453542292123216</id><published>2010-12-27T16:23:00.002-05:00</published><updated>2010-12-27T16:33:01.358-05:00</updated><title type='text'>Program to find Divisble by 6 or 7's</title><content type='html'>Problem: Write a program that displays the integers between 1 and 100 that are divisible by 6 or 7 but not both. (Roberts ch 4, exercise 4).&lt;br /&gt;&lt;br /&gt;This was one of the first problems in the set, and a relatively easy one that makes a good refresher on how booleans work. The sentence "boolean 'isDivisible' which resolves to true if the number is divisible by 6 and not 7 OR divisible by 7 and not 6" translates in code to "boolean isDivisable = ((integer % 6 == 0) &amp;&amp; (integer % 7 != 0)) || ((integer % 7 == 0) &amp;&amp; (integer % 6 != 0));"&lt;br /&gt;&lt;br /&gt;How long it took: 20 min&lt;br /&gt;&lt;br /&gt;What made it tough: Nada, not by now :)&lt;br /&gt;&lt;br /&gt;Lingering questions: Is there a better way to do this? For example, what if you first found through all the numbers divisible by 6, then all the numbers divisible by 7, then de-duped them. Would that be any more efficient or flexible?&lt;br /&gt;&lt;br /&gt;Also, my friend from work John Britton says that automated tests are a good practice. For a problem like this, you can imagine that if the range of numbers were much bigger (say 1-500,000), it would be harder to confirm that the code is accurate. How would you go about building in tests? &lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;* File: DivisibleBy6or7.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: &lt;br /&gt;* Description: This program finds the integers from 1 to 100 that are divisible by 6 or 7 (but not both) by running a boolean 'isDivisible' which resolves to true if the number is divisible by 6 and not 7 OR divisible by 7 and not 6. If the 'isDivisible' resolves to true, the we display the integer.&lt;br /&gt;* ------------------&lt;br /&gt;*/&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;package Ch4Practice;&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;public class DivisibleBy6or7 extends ConsoleProgram {&lt;br /&gt;     public void run() {&lt;br /&gt;     println("These are the integers from 1 to 100 that are divisible by 6 or 7, but not both:");&lt;br /&gt;        for (int i = 1; i &lt; 101; i++) {&lt;br /&gt;            int integer = i;&lt;br /&gt;            boolean isDivisable = ((integer % 6 == 0) &amp;&amp; (integer % 7 != 0)) || ((integer % 7 == 0) &amp;&amp; (integer % 6 != 0));&lt;br /&gt;            if(isDivisable) {&lt;br /&gt;             println(integer);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2423453542292123216?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2423453542292123216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/12/problem-write-program-that-displays.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2423453542292123216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2423453542292123216'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/12/problem-write-program-that-displays.html' title='Program to find Divisble by 6 or 7&apos;s'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-1958528810815133472</id><published>2010-12-25T23:46:00.000-05:00</published><updated>2010-12-25T23:46:42.392-05:00</updated><title type='text'>Cleaner Implementation of CheckerboardWithCheckers</title><content type='html'>There was an old problem in the textbook that drew a black-and-white checkerboard. For this problem, we had to modify it to also add checkers.&lt;br /&gt;&lt;br /&gt;I gave it a shot at first by simply breaking up the checkerboard into three sections. For the first three rows, make a square, add a red checker. For next three rows, just make the squares. For the last three rows, make a square, add a white checker (later change to black). Needless to say, this is a functional but un-elegant bit of code that re-creates all my loops three times, and if I ever needed to edit the conditions for laying down squares or checkers, I’d easily introduce bugs.&lt;br /&gt;&lt;br /&gt;This new version is cleaned up a bit.&lt;br /&gt;&lt;br /&gt;First, we put down the checkerboard. For each integration of the loop, we make a square in the proper spot. If it’s an “every other” square, both horizontally and vertically, make it filled. Then add the square to the board.&lt;br /&gt;&lt;br /&gt;Next we do the checkers. Make a new GOval for every iteration of the loop; we won’t necessarily plop it down on the board, only if that spot is a proper place for a checker. (Programming experts, is this a bad practice?) If we’re on an iteration of the loop that’s for an “every other” square (we can reuse the code for “sq.setFilled” here) AND if it’s in one of the first three rows, make it red and add it to the board. (Repeat for the last three rows, except make the checker black instead of red.)&lt;br /&gt;&lt;br /&gt;Here’s what the code looks like:&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;* File: CheckerboardWithCheckers.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: Geoff clearly, because without him there would be no acm.jar &lt;br /&gt;* in this Eclipse workspace, and then where would we be?&lt;br /&gt;* ------------------&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class CheckerboardWithCheckers extends GraphicsProgram {&lt;br /&gt;&lt;br /&gt;    //private constants&lt;br /&gt;    private static final int N_ROWS = 8;&lt;br /&gt;    private static final int N_COLUMNS = 8; &lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;        double sqSize = (double) getHeight() / N_ROWS; &lt;br /&gt;        double boardWidth = N_COLUMNS * sqSize; &lt;br /&gt;&lt;br /&gt;        for (int i = 0; i &lt; N_ROWS; i++) { //for the outer rows&lt;br /&gt;            for (int j = 0; j &lt; N_COLUMNS; j++) { //For columns within each row&lt;br /&gt;    &lt;br /&gt;                //First make the checkerboard:&lt;br /&gt;                double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;                double y = i* sqSize;&lt;br /&gt;                GRect sq = new GRect(x, y, sqSize, sqSize); //Make up a new square with the proper coordinates &lt;br /&gt;                if ((i+j) % 2 != 0) { //If it's every other square, both by row and column&lt;br /&gt;                    sq.setColor(Color.BLACK);&lt;br /&gt;                    sq.setFilled(true); //Make it filled&lt;br /&gt;                    sq.setFillColor(Color.GRAY);   &lt;br /&gt;                }&lt;br /&gt;                add(sq); &lt;br /&gt;    &lt;br /&gt;                //Now for the checkers:&lt;br /&gt;                GOval checker = new GOval (x, y, sqSize, sqSize);&lt;br /&gt;                checker.setFilled(true);&lt;br /&gt;                if ((i+j) % 2 != 0) {//If it's every other square... (This is bad kids! Don't repeat code.)&lt;br /&gt;                    if ((i &lt; 3)) {//AND the first three rows, make red checkers &lt;br /&gt;                        checker.setFillColor(Color.RED);&lt;br /&gt;                        add(checker);&lt;br /&gt;                    }&lt;br /&gt;                    else if ((N_ROWS-4 &lt; i) &amp;&amp; (i &lt; N_ROWS)) { //If it's every other AND the last 3 rows, make white checkers&lt;br /&gt;                        checker.setFillColor(Color.BLACK); &lt;br /&gt;                        add(checker);&lt;br /&gt;                    } &lt;br /&gt;  &lt;br /&gt;                }&lt;br /&gt;    &lt;br /&gt;             }&lt;br /&gt; &lt;br /&gt;         }&lt;br /&gt;    }&lt;br /&gt;  &lt;br /&gt;} &lt;br /&gt; &lt;br /&gt;&lt;/pre&gt;Here’s what you get when you run it:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_UagTlN5LQ1U/TRbDMxHOZqI/AAAAAAAAAeA/rtHJPMIQEQc/s1600/Screen%2Bshot%2B2010-12-25%2Bat%2B8.19.42%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="293" width="400" src="http://2.bp.blogspot.com/_UagTlN5LQ1U/TRbDMxHOZqI/AAAAAAAAAeA/rtHJPMIQEQc/s400/Screen%2Bshot%2B2010-12-25%2Bat%2B8.19.42%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;The above code is perfectly functional, except we reuse the code that checks for “every other” square, specifically “if ((i+j) % 2 != 0).” I’d wanted to use the same “if” statement for both “sq.setFilled” and “add(checker)”. However, I don’t think I can; the “add(sq)” method applies whether it’s a filled square or not, so I have to put it outside the “if every-other” loop. The add(checker) method does happen only on the “every-other” squares, so it’s inside the loop. This means adding a square comes AFTER adding a checker, so the squares go on top of the checker and we can’t see the checkers.Example code for CheckerboardWithoutRepeatingCode:&lt;pre class = "brush:java;"&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class CheckerboardWithoutRepeatedCode extends GraphicsProgram {&lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;        double sqSize = (double) getHeight() / N_ROWS; &lt;br /&gt;        double boardWidth = N_COLUMNS * sqSize; &lt;br /&gt;&lt;br /&gt;        for (int i = 0; i &lt; N_ROWS; i++) { //for the outer rows&lt;br /&gt;            for (int j = 0; j &lt; N_COLUMNS; j++) { //for the inner rows&lt;br /&gt;                double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;                double y = i* sqSize;&lt;br /&gt;                GRect sq = new GRect(x, y, sqSize, sqSize); //Make up a new square with the proper coordinates&lt;br /&gt;   &lt;br /&gt;                if ((i+j) % 2 != 0) { //If it's every other square, both by row and column&lt;br /&gt;                    sq.setFilled(true); //Make it filled&lt;br /&gt;          &lt;br /&gt;                    //Now for the checkers:&lt;br /&gt;                    GOval checker = new GOval (x, y, sqSize, sqSize);&lt;br /&gt;                    checker.setFilled(true);&lt;br /&gt;     &lt;br /&gt;                    if ((i &lt; 3)) {//If it's every other AND the first three rows, make red checkers &lt;br /&gt;                        checker.setFillColor(Color.RED);&lt;br /&gt;                        add(checker);&lt;br /&gt;                        }&lt;br /&gt;                    else if ((N_ROWS-4 &lt; i) &amp;&amp; (i &lt; N_ROWS)) { //If it's every other AND the last 3 rows, make white checkers&lt;br /&gt;                        checker.setFillColor(Color.WHITE); &lt;br /&gt;                        add(checker);&lt;br /&gt;                    } &lt;br /&gt;  &lt;br /&gt;                    }&lt;br /&gt;    &lt;br /&gt;                add(sq); //Plop that square down-- DAMN! So perfect except the squares go on top of the checkers...&lt;br /&gt;                         //We have to make the add(sq) come before the add(checker)  &lt;br /&gt;                }&lt;br /&gt; &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    //private constants&lt;br /&gt;    private static final int N_ROWS = 8;&lt;br /&gt;    private static final int N_COLUMNS = 8; &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;What it looks like:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_UagTlN5LQ1U/TRbEJ-HcwNI/AAAAAAAAAeI/7_QajI9ZKnw/s1600/Screen%2Bshot%2B2010-12-25%2Bat%2B8.25.44%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="291" width="400" src="http://4.bp.blogspot.com/_UagTlN5LQ1U/TRbEJ-HcwNI/AAAAAAAAAeI/7_QajI9ZKnw/s400/Screen%2Bshot%2B2010-12-25%2Bat%2B8.25.44%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Does anyone have ideas on how to condense this code and possibly not repeat the check for “if every-other” square?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-1958528810815133472?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/1958528810815133472/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/12/cleaner-implementation-of.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1958528810815133472'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/1958528810815133472'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/12/cleaner-implementation-of.html' title='Cleaner Implementation of CheckerboardWithCheckers'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_UagTlN5LQ1U/TRbDMxHOZqI/AAAAAAAAAeA/rtHJPMIQEQc/s72-c/Screen%2Bshot%2B2010-12-25%2Bat%2B8.19.42%2BPM.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-7167019173533709479</id><published>2010-12-25T01:10:00.002-05:00</published><updated>2010-12-25T01:39:09.235-05:00</updated><title type='text'>Checkerboard with Checkers</title><content type='html'>Problem: Enhance the Checkerboard problem so that it centers the checkerboard horizontally and draws the set of red and black checkers corresponding to the initial state of the game. (Roberts ch 4, Exercise 14)&lt;br /&gt;&lt;br /&gt;2 instances of repeated code here. We want red checkers apply to the first three rows, no checkers to apply to the middle rows, and white checkers apply to the last three rows. We can take care of this by changing the outer while loop; instead of i going from 0 to N_ROWS, we break it up into a) 0 to 3, b) 3 to N_ROWS-3, and c) N_ROWS-3 to N_ROWS. I need to think about how to break up my iterations this way without repeating code too much.&lt;br /&gt;&lt;br /&gt;In the rows where checkers do apply, we want to add checkers only to the squares that are filled in. We have a boolean that says "If the sum of the column number and the row number is odd, then it should be filled in." This is how we make sure the "Every Other" applies both horizontally and vertically. We want to same boolean to apply to whether we put checkers down or not; however, if we make the line "add(sq)" come after the "add(redChecker)", we won't see the checker. But we can't make it come after while being in the same loop because we want to add the square whether or not it's a filled-in square.&lt;br /&gt;&lt;br /&gt;I'm going to sleep now...this will be easier to clean up in the morning. &lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;import acm.graphics.*;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: CheckerboardWithCheckers.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: Geoff clearly, because without him there would be no acm.jar in this Eclipse workspace, and then where would we be?&lt;br /&gt;* ------------------&lt;br /&gt;*&lt;br /&gt;*/&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class CheckerboardWithCheckers extends GraphicsProgram {&lt;br /&gt;&lt;br /&gt; public void run() {&lt;br /&gt;  double sqSize = (double) getHeight() / N_ROWS;&lt;br /&gt;  double boardWidth = N_COLUMNS * sqSize;&lt;br /&gt;  for (int i = 0; i &lt; 3; i++) {&lt;br /&gt;   for (int j = 0; j &lt; N_COLUMNS; j++) {&lt;br /&gt;    double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;    double y = i* sqSize;&lt;br /&gt;    GRect sq = new GRect(x, y, sqSize, sqSize);&lt;br /&gt;    if ((i+j) % 2 != 0) {&lt;br /&gt;     sq.setFilled(true);&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;    }&lt;br /&gt;    add(sq);&lt;br /&gt;    &lt;br /&gt;    if ((i+j) % 2 != 0) {//This is bad, kids! Don't repeat code&lt;br /&gt;     GOval redChecker = new GOval (x, y, sqSize, sqSize);&lt;br /&gt;     redChecker.setFilled(true);&lt;br /&gt;     redChecker.setFillColor(Color.RED); &lt;br /&gt;     add(redChecker);&lt;br /&gt;    }&lt;br /&gt;   } &lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  for (int i = 3; i &lt; N_ROWS-3; i++) { //This is bad, kids! Don't repeat code&lt;br /&gt;   for (int j = 0; j &lt; N_COLUMNS; j++) {&lt;br /&gt;    double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;    double y = i* sqSize;&lt;br /&gt;    GRect sq = new GRect(x, y, sqSize, sqSize);&lt;br /&gt;    if ((i+j) % 2 != 0) {&lt;br /&gt;     sq.setFilled(true);&lt;br /&gt;     &lt;br /&gt;    }&lt;br /&gt;    add(sq);&lt;br /&gt;   &lt;br /&gt;   } &lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  for (int i = N_ROWS-3; i &lt; N_ROWS; i++) {&lt;br /&gt;   for (int j = 0; j &lt; N_COLUMNS; j++) {&lt;br /&gt;    double x = (j * sqSize) + (getWidth()/2 - boardWidth/2);&lt;br /&gt;    double y = i* sqSize;&lt;br /&gt;    GRect sq = new GRect(x, y, sqSize, sqSize);&lt;br /&gt;    if ((i+j) % 2 != 0) {&lt;br /&gt;     sq.setFilled(true);&lt;br /&gt;     &lt;br /&gt;    }&lt;br /&gt;    add(sq);&lt;br /&gt;    &lt;br /&gt;    if ((i+j) % 2 != 0) {//This is bad, kids! Don't repeat code&lt;br /&gt;     GOval whiteChecker = new GOval (x, y, sqSize, sqSize);&lt;br /&gt;     whiteChecker.setFilled(true);&lt;br /&gt;     whiteChecker.setFillColor(Color.WHITE); &lt;br /&gt;     add(whiteChecker);&lt;br /&gt;    }&lt;br /&gt;   } &lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//private constants&lt;br /&gt; private static final int N_ROWS = 8;&lt;br /&gt; private static final int N_COLUMNS = 8;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;What it looks like:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_UagTlN5LQ1U/TRWK4AY964I/AAAAAAAAAd4/cSAJeV-Lqww/s1600/Screen%2Bshot%2B2010-12-24%2Bat%2B10.10.07%2BPM.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="288" width="400" src="http://1.bp.blogspot.com/_UagTlN5LQ1U/TRWK4AY964I/AAAAAAAAAd4/cSAJeV-Lqww/s400/Screen%2Bshot%2B2010-12-24%2Bat%2B10.10.07%2BPM.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-7167019173533709479?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/7167019173533709479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/12/checkerboard-with-checkers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7167019173533709479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7167019173533709479'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/12/checkerboard-with-checkers.html' title='Checkerboard with Checkers'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_UagTlN5LQ1U/TRWK4AY964I/AAAAAAAAAd4/cSAJeV-Lqww/s72-c/Screen%2Bshot%2B2010-12-24%2Bat%2B10.10.07%2BPM.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-8133325116710818841</id><published>2010-12-19T23:52:00.000-05:00</published><updated>2010-12-19T23:52:16.734-05:00</updated><title type='text'></title><content type='html'>Test: Does this syntax highlighter work?&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:ruby;"&gt;&lt;br /&gt;#Old, not-so-useful version:&lt;br /&gt;&lt;br /&gt;print "Hello. Please enter a Celsius value: "&lt;br /&gt;celsius = gets&lt;br /&gt;fahrenheit = (celsius.to_i * 9.0 / 5) + 32&lt;br /&gt;print "The farenheit equivalent is "&lt;br /&gt;print fahrenheit&lt;br /&gt;puts "."&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:ror;"&gt;&lt;br /&gt;#New and improved version that accomplishes temperature conversion as a method&lt;br /&gt;&lt;br /&gt;class Numeric&lt;br /&gt;  def c2f  # this is a method &lt;br /&gt;    self* 9.0 / 5 + 32&lt;br /&gt;  end  &lt;br /&gt;end  &lt;br /&gt;&lt;br /&gt;#Example use cases of this method&lt;br /&gt;current_farenheit = 12.c2f &lt;br /&gt;puts current_farenheit&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-8133325116710818841?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/8133325116710818841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/12/test-does-this-syntax-highlighter-work.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8133325116710818841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8133325116710818841'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/12/test-does-this-syntax-highlighter-work.html' title=''/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-6142971762511697466</id><published>2010-09-13T01:58:00.004-04:00</published><updated>2010-09-13T02:19:45.852-04:00</updated><title type='text'>Animated Bouncing Ball- The Bill Version</title><content type='html'>After I wrote up my Animated Bouncing Ball solution, my friend Bill left a comment. His suggestion had to do with how I tracked the location of my ball so I'd know when it had hit a wall and should reverse directions. My solution had been to declare the variables x and y, set them to the initial x and y coordinates of my ball, and change their value after each move the ball made to make them match the new coordinates. As Bill said, this is quite poor because you're tracking x and y in two places, leaving it inefficient and susceptible to bugs.&lt;br /&gt;&lt;br /&gt;The truth is, I had wanted to use a built-in getX() kind of method, but I didn't think such a method existed, since it wasn't listed in the textbook. Then I talked to Andrew, a friend from work, and realized that not only were there hundreds of other methods for GOval than were in the book, but that Java has dozens of ways to conceptualize an oval, only one of which is the GOval class. It's like I've been walking around with blinders on and suddenly the blinders have come off and I realize there's much more than I even knew to look for. As my friend Jane says, half of programming is just knowing what to Google.&lt;br /&gt;&lt;br /&gt;So, I looked up to see whether I can use getX() with the GOval class to make my program more efficient, and lo and behold, I can! New and improved code below. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class = "brush:java;"&gt;/*&lt;br /&gt;* File: BounceBall.java&lt;br /&gt;* Name: &lt;br /&gt;* Section Leader:&lt;br /&gt;* -----------------&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class BounceBall extends GraphicsProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;        &lt;br /&gt;//Create ball in initial centered position&lt;br /&gt;     int centeredx = (getWidth()-DIAMETER)/2;&lt;br /&gt;     int centeredy = (getHeight()-DIAMETER)/2;&lt;br /&gt;        &lt;br /&gt;     GOval ball = new GOval (centeredx, centeredy, DIAMETER, DIAMETER); &lt;br /&gt;        ball.setFilled(true);&lt;br /&gt;        add(ball);&lt;br /&gt;&lt;br /&gt;        int dx = 1; //Setting the size of the ball's steps&lt;br /&gt;        int dy = 1;&lt;br /&gt;        &lt;br /&gt;        while(true) { //Getting the ball to move&lt;br /&gt;            ball.move(dx, dy);&lt;br /&gt;            pause(PAUSE_TIME);&lt;br /&gt;&lt;br /&gt;//  Declare x and y variables to locate the ball&lt;br /&gt;          &lt;br /&gt;            double x = ball.getX(); &lt;br /&gt;            double y = ball.getY();&lt;br /&gt;            &lt;br /&gt;//"If" statements to test whether the ball is at a wall, and if so, reassign dx or dy to switch direction.&lt;br /&gt;            &lt;br /&gt;            if (x &gt;= getWidth()-DIAMETER) { //If it hits the right-hand wall`&lt;br /&gt;             dx=-dx;     &lt;br /&gt;            }&lt;br /&gt;            if (y &gt;= getHeight()-DIAMETER) { //If it hits the bottom&lt;br /&gt;             dy=-dy;&lt;br /&gt;            }&lt;br /&gt;            if (x == 0) { //If it hits the left-hand wall&lt;br /&gt;             dx=-dx;&lt;br /&gt;            }&lt;br /&gt;            if (y == 0) {//If it hits the top&lt;br /&gt;             dy=-dy;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;//Private Constants&lt;br /&gt;        &lt;br /&gt;        private static final int DIAMETER = 40;&lt;br /&gt;        private static final int PAUSE_TIME = 20;     &lt;br /&gt;      &lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-6142971762511697466?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/6142971762511697466/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/09/animated-bouncing-ball-bill-version.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6142971762511697466'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6142971762511697466'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/09/animated-bouncing-ball-bill-version.html' title='Animated Bouncing Ball- The Bill Version'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2921754464098790777</id><published>2010-08-09T01:10:00.018-04:00</published><updated>2010-08-09T03:40:50.549-04:00</updated><title type='text'>Program to Draw an Animated Bouncing Ball</title><content type='html'>&lt;span id="internal-source-marker_0.38598974980413914" style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;As I learn to code, programmer friends emphasize that I should break a problem down into chunks that I solve sequentially. It helps me think clearly to focus on one goal at a time.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;For example, a recent question asked me to create a ball that bounces within the graphics window, like &lt;/span&gt;&lt;a href="http://www.screenjelly.com/watch/Jj8lIPGeiMc"&gt;&lt;span style="background-color: transparent; color: #000099; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;this&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;The 3 main parts of this problem, once you draw the ball, are:&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;1) Get the ball to MOVE.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;2) Get the ball to STOP at the edge of the graphics window.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;3) Get the ball to BOUNCE to the next wall, over and over.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Here is how I tackled them:&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Step 1. Get the ball to MOVE:&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;When I started the problem, I just wanted to make a ball on the screen and get it to move in the direction I told it to. It would start moving at an x=y diagonal.  I wrote a ‘while’ loop that says, “As long as we’re in this loop, move the ball one pixel to the left (dx) and one pixel down (dy).” &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;At this point, I didn’t know whether I’d have to tell the loop to make the ball stop moving when it got to the edge of the window or whether it would automatically do so. (Silly me). As it turns out, it really goes on forever! I ran the app to see what would happen, and the ball, once it reached the end of the window, kept going, apparently forever. (&lt;/span&gt;&lt;a href="http://www.screenjelly.com/watch/pDmW76wiVUY"&gt;&lt;span style="background-color: transparent; color: #000099; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;"&gt;See for yourself.&lt;/span&gt;&lt;/a&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;) That meant I next had to figure out how to detect the edge of the window and make the ball stop.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Also, the textbook problem required that the ball start off in the middle of the screen, though in my screencast you’ll notice that I had it starting at the top-left corner. This is because, for the Java graphics library, (0,0) is the top-left corner, so it’s easy to start the ball there, whereas placing it in the center requires a trivial bit of algebra that I knew I’d be able to do later. I just wanted to make the ball quickly and confirm that my ‘while’ loop could get  the ball to move.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;pre class="brush: java;"&gt;public class BounceBall extends GraphicsProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;   &lt;br /&gt;//First make the ball...    &lt;br /&gt;   GOval ball = new GOval (0, 0, DIAMETER, DIAMETER);&lt;br /&gt;        ball.setFilled(true);&lt;br /&gt;        add(ball);&lt;br /&gt;        &lt;br /&gt;        double dx = 1;&lt;br /&gt;        double dy = 1;&lt;br /&gt; &lt;br /&gt;        while(true) {&lt;br /&gt;            ball.move(dx, dy);&lt;br /&gt;            pause(PAUSE_TIME);&lt;br /&gt;            &lt;br /&gt;        }&lt;br /&gt;//Private Constants   &lt;br /&gt;    private static final int DIAMETER = 40;&lt;br /&gt;    private static final int PAUSE_TIME = 20;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br /&gt;2) Get the ball to STOP at the edge of the graphics window.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Now I needed to figure out how to make my program “know” when it’s the end of the window, so it could make the ball stop. I wanted the program to say, “When X coordinate of the ball equals the width of the window, or when Y equals the height of the window, stop!” (To be more precise, the X coordinate should actually be the width of the window MINUS the diameter of the ball, so that the ball doesn’t sink past the edge of the window before it bounces each time).&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;At first I was stumped, because I couldn’t think of a way to keep track of the x and y coordinates of the ball. After all, in Java, to move the ball, you just declare variables “dx” and “dy”, which tell you &lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;how much t&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;o move the ball by; you don’t specify what the new coordinates &lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;are&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;. After chatting with my friend Emmett, I realized that you could just declare variables “x” and “y” to equal the x and y coordinates of the ball, which you alter, by adding dx and dy to them, respectively, in every iteration of the  loop. Indexing  x and y lets you check the coordinates, even if it’s not necessary for the “move” command itself. Seems obvious now, so thanks Emmett :)&lt;/span&gt; &lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Also in this stage, I centered the ball’s starting position.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;pre class="brush: java;"&gt;public void run() {&lt;br /&gt;&lt;br /&gt;//First you make the ball-- centered now&lt;br /&gt; int x = (getWidth()-DIAMETER)/2; &lt;br /&gt; int y = (getHeight()-DIAMETER)/2;    &lt;br /&gt;   &lt;br /&gt; GOval ball = new GOval (x, y, DIAMETER, DIAMETER); &lt;br /&gt; ball.setFilled(true);&lt;br /&gt; add(ball); &lt;br /&gt; &lt;br /&gt;//Then you make it move&lt;br /&gt; int dx = 1; &lt;br /&gt; int dy = 1; &lt;br /&gt;&lt;br /&gt; while(true) {  &lt;br /&gt;  ball.move(dx, dy);  &lt;br /&gt;  pause(PAUSE_TIME);  &lt;br /&gt;  x += dx; //changing x and y to check coordinates &lt;br /&gt;  y += dy; //against graphics window    &lt;br /&gt;  &lt;br /&gt;  if (x &amp;gt;= getWidth()-DIAMETER) {    &lt;br /&gt;   break;&lt;br /&gt;      }  &lt;br /&gt;  if (y &amp;gt;= getHeight()+DIAMETER) { &lt;br /&gt;   break;&lt;br /&gt;      } &lt;br /&gt;  if (x == 0) {&lt;br /&gt;   break;&lt;br /&gt;     }  &lt;br /&gt;   if (y == 0) {&lt;br /&gt;   break;&lt;br /&gt;     }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;3) Getting the ball to BOUNCE&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;Getting the ball to bounce is just a matter of switching the dx or dy variable to its own negative once we detect that the ball hit a wall. (I drew the ball’s path on a notepad before I attempted coding to think this through.)  For example, if the ball is moving down and to the right and hits the bottom edge, it will start going upwards, but its horizontal direction will still be to the right. In that case, dy will switch to -dy, but dx won’t change.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Georgia; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"&gt;What this looks like in terms of coding is I made “if” statements that say, “If we hit the top or bottom of the wall (aka if the y coordinate of the ball is zero or the height of the window), then change dy to -dy. If we hit the right or left side of the window (aka if the x coordinate is zero or the width of the window) then change dx to -dx. This can go on indefinitely.&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush: java;"&gt;while(true) {&lt;br /&gt; ball.move(dx, dy);&lt;br /&gt;    pause(PAUSE_TIME);            &lt;br /&gt;/*Reset the variables x and y at each iteration&lt;br /&gt; so can check them against window boundaries*/     &lt;br /&gt;    x += dx;&lt;br /&gt;    y += dy;&lt;br /&gt;            &lt;br /&gt;    if (x &gt;= getWidth()-DIAMETER) {&lt;br /&gt;        dx=-dx;&lt;br /&gt;        dy=dy;&lt;br /&gt;            }&lt;br /&gt;    if (y &gt;= getHeight()-DIAMETER) { &lt;br /&gt;        dx=dx;&lt;br /&gt;        dy=-dy;&lt;br /&gt;            }&lt;br /&gt;    if (x == 0) { &lt;br /&gt;        dx=-dx;&lt;br /&gt;        dy=dy;&lt;br /&gt;            }&lt;br /&gt;    if (y == 0) {&lt;br /&gt;        dx=dx;&lt;br /&gt;        dy=-dy;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Georgia; font-size: 13px; white-space: pre-wrap;"&gt;Something to think about is: What will happen if the ball hits a corner? If it hits the top-left corner, will it bounce to straight right or straight down? Answer: it depends on the order you write your “if” statements in. My particular code checks for hitting the left before it checks for the top, so when the ball hits top-left it will bounce horizontally.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Georgia; font-size: 13px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2921754464098790777?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2921754464098790777/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/08/as-i-learn-to-code-programmer-friends.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2921754464098790777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2921754464098790777'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/08/as-i-learn-to-code-programmer-friends.html' title='Program to Draw an Animated Bouncing Ball'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-7647856164487376556</id><published>2010-02-28T15:14:00.009-05:00</published><updated>2010-02-28T16:14:37.216-05:00</updated><title type='text'>Find2Largest Problem</title><content type='html'>Problem: Expand the problem from the preceding exercise "Find Largest" so that it finds both the largest and the second largest values in the list." (Robers ch 4, problem 13).&lt;br /&gt;&lt;br /&gt;Basically, you start with two placeholder variables, "top" and "second." You set them initially to 0, but their values will change. As the user enters in new integers, you want a program that looks at each new input and says "If the input value is greater than the top, then replace the current top with the new input. If the input value is less than the current top AND greater than the current second, then reset second to equal the new input."&lt;br /&gt;&lt;br /&gt;Renee's Code:&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * File: Find2ndLargest.java&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * Name: Renee&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * Section Leader: &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * ------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;This program uses three variables to find the two-largest values in an indefinite&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;list of integers entered by the user: "value", which is the variable given to the user &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;inputs, and "top" and "second," which are storage variables that start off at a value of 0. As the user&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;enters inputs, we compare the inputs to the current "top" and "second." Whenever the value&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;the user enters is greater or equal to the current "top," then we reassign the value&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;of "top" to the new input (but first we have to move the old "top" to "second" place).&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;This keeps going until the user enters the sentinel, when we end the inner loop and print the&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;reigning "top" and "second."&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import acm.program.*;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class Find2ndLargest extends ConsoleProgram{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; public void run () {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     println ("This program finds the 2 largest of a list of integers.");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     println("Enter integers, one per line, entering 0 to signify the end of the list.");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     int top = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     int second = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     while (true){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     int value = readInt("Enter integer: ");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     if (value == SENTINEL) break;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     if (value &gt;= top) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         second = top;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         top = value;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     }else if (value &gt;= second){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         second = value;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     println("The highest is " + top + ".");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     println("The second highest is " + second + ".");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; //private constants&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; private static final int SENTINEL = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100228-p7haixqmu92etpq4m7r4qijh9x.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 513px; height: 199px;" src="http://img.skitch.com/20100228-p7haixqmu92etpq4m7r4qijh9x.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;Time it took: 35 min&lt;br /&gt;&lt;br /&gt;What made it difficult: Not super-difficult this time. I did have a funny blooper. The first time I ran the code, I entered "7, 9, 6, 0" and got "The highest is 9. The second-highest is 6." What! The problem was, I had forgotten to state, "if you get a new 'top,' make the old 'top' equal to the 'second' and then make the new 'value' equal to the new 'top.'"  Leaving out that line, then if your second-highest gets  entered before your highest, you won't capture the second-highest.&lt;br /&gt;&lt;br /&gt;This program also shows you the importance of "greater than" versus "greater than or equal to." At first I thought it was a wash whether I used "&gt;=" or "&gt;" as my operator, but running the program by hand with a couple test inputs clarified the difference.&lt;br /&gt;&lt;br /&gt;Let's say the user enters "2, 2, 1, 0" as the inputs. What's the right answer? "The highest is 2. The second-highest is 1", right? However, if the operator is "&gt;", which is wrong, here's how the program runs (going through it by hand):&lt;br /&gt;&lt;br /&gt;Initial: top = 0, second = 0&lt;br /&gt;1st loop: value = 2. The program thinks, "Is 2 greater than my current top, 0? Yes. Then top = 2, second = 0."&lt;br /&gt;2nd loop: value = 2. The program thinks, "Is 2 greater than my current top, 2? NO! Well, is it greater than my current second, 0? Yes! Then reassign "second" to equal 2. top = 2, second = 2."&lt;br /&gt;3rd: value = 1. The program thinks "1 is not greater than my current top or my current second, so keep the status as top = 2, second = 2."&lt;br /&gt;4th: value = 0. Zero is the sentinel. Program stops, print "The highest is 2. The second-highest is 2."&lt;br /&gt;&lt;br /&gt;This bug is solved by making the operator &gt;=, because if the current top is 2 and the user enters 2, the program will replace the old 2 with the new 2, instead of moving it to the "second" variable.&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-7647856164487376556?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/7647856164487376556/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/02/find2largest-problem.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7647856164487376556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7647856164487376556'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/02/find2largest-problem.html' title='Find2Largest Problem'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-8739296074414045975</id><published>2010-02-24T14:42:00.005-05:00</published><updated>2010-02-24T14:53:06.036-05:00</updated><title type='text'>FindLargest Problem</title><content type='html'>Problem: "Write a ConsoleProgram that reads in a list of integers, one per line, until the users a sentinel value of 0. When the sentinel is read, your program should display the largest value in the list" (Robers ch4, problem 12).&lt;br /&gt;&lt;br /&gt;Renee's Code:&lt;br /&gt;&lt;pre style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;/*&lt;br /&gt;* File: FindLargest.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: Me! I did this one before breakfast, =D&lt;br /&gt;* ------------------&lt;br /&gt;We use two variables, "value" and "top", to solve this problem. "Top" is&lt;br /&gt;declared at the beginning of the run method and arbitrarily gets assigned&lt;br /&gt;-1. "Value" comes from the user's imput. Every time the user enters&lt;br /&gt;Value comes the user's input. For every input, we check the input value against&lt;br /&gt;the current "top," and if it's greater, we replace "top" with the "value."&lt;br /&gt;Then once the user enters the sentinel, we print the current "top."&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class FindLargest extends ConsoleProgram{&lt;br /&gt; public void run () {&lt;br /&gt;     println ("This program finds the largest of a list of integers.");&lt;br /&gt;     println("Enter integers, one per line, entering 0 to signify the end of the list.");&lt;br /&gt; &lt;br /&gt;     int top = -1;&lt;br /&gt; &lt;br /&gt;     while (true){&lt;br /&gt;     int value = readInt("Enter integer: ");&lt;br /&gt;     if (value == SENTINEL) break;&lt;br /&gt;&lt;br /&gt;     if (value &gt;= top) top = value;&lt;br /&gt;     &lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     println("The highest is " + top + ".");&lt;br /&gt; &lt;br /&gt; }&lt;br /&gt; //private constants&lt;br /&gt; private static final int SENTINEL = 0;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;What it looks like:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100224-tdiggcqeqjpp3pk692gt1cbm6w.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 534px; height: 326px;" src="http://img.skitch.com/20100224-tdiggcqeqjpp3pk692gt1cbm6w.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;How long it took: 25 min&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Lingering Questions: I solved this by using two variables ("Top" and "Value") whose value changed, as the comment for the code describes. The question is: What are alternative approaches to solving this?&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-8739296074414045975?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/8739296074414045975/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/02/findlargest-proglem.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8739296074414045975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/8739296074414045975'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/02/findlargest-proglem.html' title='FindLargest Problem'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-6330233279285441199</id><published>2010-02-12T21:36:00.003-05:00</published><updated>2010-02-12T21:45:23.275-05:00</updated><title type='text'>The Pyramid Saga Continues</title><content type='html'>Finally I draw my pyramid. Before I post the actual code, a second addition to the blooper reel. Have a look at the blooper code and tell me why we're getting a rhombus. And what the correction in the code should be:&lt;br /&gt;&lt;br /&gt;Rhombus pyramid: &lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100213-dne9runhaffxfrp9cmjfst498g.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 562px; height: 314px;" src="http://img.skitch.com/20100213-dne9runhaffxfrp9cmjfst498g.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;br /&gt;public class Pyramid extends GraphicsProgram {&lt;br /&gt;&lt;br /&gt;public void run() {&lt;br /&gt;&lt;br /&gt;    //X and Y coordinates for the starting (bottom-left) brick:&lt;br /&gt;    double xstart = getWidth()/2 -(1/2)*(BRICK_WIDTH)*(BRICKS_IN_BASE);&lt;br /&gt;    double ystart = getHeight()/2 + (1/2)*(BRICK_HEIGHT)*(BRICKS_IN_BASE);&lt;br /&gt;&lt;br /&gt;    //Outer loop to make a new row. The pyramid has as many rows as bricks in base.&lt;br /&gt;    for(int i = 0; i &amp;lt; BRICKS_IN_BASE; i++) {&lt;br /&gt;   &lt;br /&gt;        double BricksInRow = BRICKS_IN_BASE;&lt;br /&gt;   &lt;br /&gt;        double x = xstart;&lt;br /&gt;        double y = ystart;&lt;br /&gt;   &lt;br /&gt;        //Inner loop to place bricks in a row&lt;br /&gt;        for(int j = 0; j &amp;lt; BricksInRow; j++) {&lt;br /&gt;       &lt;br /&gt;            GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);&lt;br /&gt;            add(brick);&lt;br /&gt;       &lt;br /&gt;            x +=  BRICK_WIDTH; //place next brick forward&lt;br /&gt;       &lt;br /&gt;        }&lt;br /&gt;   &lt;br /&gt;        BricksInRow -= 1; //Make the next row 1 brick shorter&lt;br /&gt;        ystart = ystart - BRICK_HEIGHT; //and one brick higher&lt;br /&gt;        xstart += BRICK_WIDTH / 2; //and start 1/2 a brick to the right&lt;br /&gt;   &lt;br /&gt;   &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Width of each brick in pixels&lt;br /&gt;private static final int BRICK_WIDTH = 15;&lt;br /&gt;&lt;br /&gt;//Width of each brick in pixels&lt;br /&gt;private static final int BRICK_HEIGHT = 6;&lt;br /&gt;&lt;br /&gt;//Number of bricks in the base of the pyramid&lt;br /&gt;private static final int BRICKS_IN_BASE = 14;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;(I once asked my friend Ryan, who took CS in college, what CS exams look like, since clearly it's difficult to write many entire programs during an exam. He said that there is lots of tracing and troubleshooting of code, so I imagine this must be what taking a little test in CS class is like :] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the actual pyramid:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100213-bb9wymrm3xsdtncn8771uywma5.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 754px; height: 514px;" src="http://img.skitch.com/20100213-bb9wymrm3xsdtncn8771uywma5.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Here is the code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;table align="center" width="100%" border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td width="100%"&gt;&lt;form method="post" action="module.php?name=PrettyPrinter"&gt;&lt;div align="left"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/*&lt;br /&gt;* File: Pyramid.java&lt;br /&gt;* Name:&lt;br /&gt;* Section Leader:&lt;br /&gt;* ------------------&lt;br /&gt;* First, you have an outer loop that says, "Start the next row". It does this by,&lt;br /&gt;* once you've made a row, removing one of the bricks from Bricks_In_Base so that&lt;br /&gt;* the second row is one brick shorter, and by moving the starting brick up by&lt;br /&gt;* 1 brick-height up and 1/2 brick-width to the right.&lt;br /&gt;*&lt;br /&gt;* Second, you have an inner loop that says "Make a row." It does this by&lt;br /&gt;* placing down a brick in the starting location, then adding Brick_Width to&lt;br /&gt;* the x coordinate and laying another brick at the new x-coordinate, repeated&lt;br /&gt;* until you get to the end of the number of bricks in your row&lt;br /&gt;* (or, until the index j gets to Bricks_In_Base.)&lt;br /&gt;*&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import acm.graphics.*;&lt;br /&gt;import acm.program.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;public class Pyramid extends GraphicsProgram {&lt;br /&gt;&lt;br /&gt;public void run() {&lt;br /&gt;&lt;br /&gt;    //X and Y coordinates for the starting (bottom-left) brick:&lt;br /&gt;&lt;br /&gt;    double xstart = getWidth()/2 - (1/2)*(BRICK_WIDTH)*(BRICKS_IN_BASE);&lt;br /&gt;    double ystart = getHeight()/2 + (1/2)*(BRICK_HEIGHT)*(BRICKS_IN_BASE);&lt;br /&gt;&lt;br /&gt;    double BricksInRow = BRICKS_IN_BASE;&lt;br /&gt;&lt;br /&gt;    //Outer loop to make a new row. The pyramid has as many rows as bricks in base.&lt;br /&gt;    for(int i = 0; i &amp;lt; BRICKS_IN_BASE; i++) {&lt;br /&gt;   &lt;br /&gt;        double x = xstart;&lt;br /&gt;        double y = ystart;&lt;br /&gt;   &lt;br /&gt;        //Inner loop to place bricks in a row&lt;br /&gt;        for(int j = 0; j &amp;lt; BricksInRow; j++) {&lt;br /&gt;       &lt;br /&gt;            GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);&lt;br /&gt;            add(brick);&lt;br /&gt;       &lt;br /&gt;            x +=  BRICK_WIDTH; //place next brick forward&lt;br /&gt;       &lt;br /&gt;        }&lt;br /&gt;   &lt;br /&gt;        BricksInRow -= 1; //Make the next row 1 brick shorter&lt;br /&gt;        ystart = ystart - BRICK_HEIGHT; //and one brick higher&lt;br /&gt;        xstart += BRICK_WIDTH / 2; //and start 1/2 a brick to the right&lt;br /&gt;   &lt;br /&gt;   &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Width of each brick in pixels&lt;br /&gt;private static final int BRICK_WIDTH = 15;&lt;br /&gt;&lt;br /&gt;//Width of each brick in pixels&lt;br /&gt;private static final int BRICK_HEIGHT = 6;&lt;br /&gt;&lt;br /&gt;//Number of bricks in the base of the pyramid&lt;br /&gt;private static final int BRICKS_IN_BASE = 14;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt; &lt;/code&gt;&lt;/div&gt;                   &lt;!-- END index_on --&gt;&lt;/form&gt;&lt;/td&gt;                 &lt;td style="background-image: url(kleider/blue-sand/images/main_right.gif); background-repeat: repeat-y;" width="10"&gt;&lt;br /&gt;&lt;/td&gt;               &lt;/tr&gt;               &lt;tr&gt;                 &lt;td style="background-image: url(kleider/blue-sand/images/main_left.gif); background-repeat: repeat-y;" width="10"&gt;&lt;br /&gt;&lt;/td&gt;&lt;td style="background-image: url(kleider/blue-sand/images/main_middle.gif);" width="100%"&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/pre&gt;What made it difficult: Algebra. The triangle has to be centered, and it should be flexible so that you can change the size of the bricks and the number of bricks in the pyramid. That means that the coorddinates of the starting brick have to vary depending on what the size constants are. If you have a look at the original coordinates for xstart and ystart (the x and y coordinates for the first brick of the bottom row), you'll see how much it depended on the constants.&lt;br /&gt;&lt;br /&gt;The other thing that was tough for me is that I didn't figure out until much later that I should have two pairs of variables for the X and Y coordinates of the bricks. Arram tackled this problem in another language and did it with two pairs, and once I saw this I felt stupid for not having thought of that! You have xstart and ystart, which are the coordinates for the first brick of each row, and then x and y, the coordinates for every brick. (When you're making the first brick of the row in your inner loop, you set x =xstart, and then when you're ready to go up a row, you change xstart.) The whole time, I was in a tizzy trying to figure out how to go back to the left side of the pyyramid from the right once I've finished a row. So obvious now!&lt;br /&gt;&lt;br /&gt;Lingering questions:&lt;br /&gt;&lt;br /&gt;Well, the pyramid is supposed to be centered.&lt;br /&gt;&lt;br /&gt;To make it centered, this is how I found the coordinates for the starting X: I found the center (using the getWidth method divided by 2) and then subtracted half the length of the pyramid. Same goes for the starting X, except for that I used the getHeight/d and added half the height of the pyramid.&lt;br /&gt;&lt;br /&gt;However, if the pyramid is very big and needs the viewer to maximize the window, the pyramid is not centered and goes off the screen on one side. For example, this is what the screen looks like if I set the pyramid to be 200 bricks wide and tall:&lt;br /&gt;&lt;br /&gt;Big pyramid:&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100213-dq9h6qnarde4nd6jb9rmw55555.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 754px; height: 516px;" src="http://img.skitch.com/20100213-dq9h6qnarde4nd6jb9rmw55555.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I think that the bottom-left brick is actually in the CENTER of the view window! I thought I had written xstart and ystart so that the bottom brick would be appropriately off-center...What's going on?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-6330233279285441199?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/6330233279285441199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/02/finally-i-draw-my-pyramid.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6330233279285441199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/6330233279285441199'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/02/finally-i-draw-my-pyramid.html' title='The Pyramid Saga Continues'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-5364204230790640607</id><published>2010-02-10T20:19:00.003-05:00</published><updated>2010-02-10T20:44:52.008-05:00</updated><title type='text'>Stuck Stuck Stuck like a Duck</title><content type='html'>&lt;code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* File: Pyramid.java&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* Name:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* Section Leader:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* ------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* First, you have an outer loop that says, "Start the next row". It does this by,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* once you've made a row, removing one of the bricks from Bricks_In_Base so that&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* the second row is one brick shorter, and by moving the starting brick up by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* 1 brick-height up and 1/2 brick-width to the right.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* Second, you have an inner loop that says "Make a row." It does this by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* placing down a brick in the starting location, then adding Brick_Width to&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* the x coordinate and laying another brick at the new x-coordinate, repeated&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* until you get to the end of the number of bricks in your row&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* (or, until the index j gets to Bricks_In_Bas&lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;/code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e.)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import acm.graphics.*;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import acm.program.*;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import java.awt.*;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class Pyramid extends GraphicsProgram {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   public void run() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       //X and Y coordinates for the starting (bottom-left) brick:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       double xstart = getWidth()/2 -(1/2)*(BRICK_WIDTH)*(BRICKS_IN_BASE);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       double ystart = getHeight()/2 + (1/2)*(BRICK_HEIGHT)*(BRICKS_IN_BASE);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       double x = xstart;&lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       double y = ystart;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       double BricksInRow = BRICKS_IN_BASE;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       //Outer loop to make a new row. The pyramid has as many rows as bricks in base.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       for(int i = 0; i &amp;lt; BRICKS_IN_BASE; i++) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           //Inner loop to place bricks in a row&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           for(int j = 0; j &amp;lt; BricksInRow; j++) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;               GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;               add(brick);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;               x +=  BRICK_WIDTH; //place next brick forward&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           BricksInRow -= 1; //Make the next row 1 brick shorter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       y = y - BRICK_HEIGHT; //and one brick higher&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           xstart += BRICK_WIDTH / 2; //and start 1/2 a brick to the right&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   //Width of each brick in pixels&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   private static final int BRICK_WIDTH = 30;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   //Width of each brick in pixels&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   privat&lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e static final int BRICK_HEIGHT = 12;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   //Number of bricks in the base of the pyramid&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   private static final int BRICKS_IN_BASE = 14;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;code style="color: rgb(0, 0, 0);"&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-family: arial;"&gt;If I built pyramids, all my mummies would have disintegrated long ago.&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/code&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20100211-ddmfdcg4i1auabaudbd5qnt7uu.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 1161px; height: 384px;" src="http://img.skitch.com/20100211-ddmfdcg4i1auabaudbd5qnt7uu.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-5364204230790640607?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/5364204230790640607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2010/02/stuck-stuck-stuck-like-duck.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5364204230790640607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/5364204230790640607'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2010/02/stuck-stuck-stuck-like-duck.html' title='Stuck Stuck Stuck like a Duck'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-679898820212603376</id><published>2009-11-15T21:58:00.007-05:00</published><updated>2009-11-15T23:10:14.317-05:00</updated><title type='text'>Work in Progress: Program To Draw A Pyramid</title><content type='html'>Problem: Write a "GraphicsProgram" subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid.&lt;br /&gt;&lt;br /&gt;The pyramid should be centered in the window and should use named constants for the following parameters:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;BRICK_WIDTH: The width of each brick&lt;br /&gt;BRICK_HEIGHT: The height of each brick&lt;br /&gt;BRICKS_IN_BASE: The nuber of bricks in the base&lt;br /&gt;&lt;br /&gt;(Roberts Ch 4, exercise 11).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;What's making this difficult:&lt;br /&gt;&lt;br /&gt;1) Centering the pyramid. I can use getWidth/2 and getHeight/2 methods to find the center of the screen, and add or subtract to place the brick at a location in reference to the center. However, the bricks are sometimes above the center, sometimes below, sometimes to the right of the center, sometimes to the left. Either I'm missing something (quite likely), or it requires a clunky solution (ie, if four sepeate nested loops for each quadrant of the pyramid).&lt;br /&gt;&lt;br /&gt;2) Variables inside the nested "for" statement: The outer "i" loop draws all of the rows, and the inner loop "j" draws all of the bricks inside the rows. The first row has 1 brick, the second row has two bricks, and so on, so I make the inner loop condition sound like this: "From j equals 1 to j equals i, draw a brick", making "i" the endpoint since "i" is the nth row that we're on. However, Eclipse seems to get mad when I reference "i" inside the "j" loop; I don't know why, but it has an angry red line under my i's within the inner loop.&lt;br /&gt;&lt;br /&gt;(Click to enlarge)&lt;br /&gt;&lt;br /&gt;&lt;div class="thumbnail"&gt;&lt;a href="http://skitch.com/reneighbor/n8w4h/stanford-editor-pyramid.java-eclipse-sdk-applications-eclipse-eclipse.app-contents-macos-cs106a-workspace"&gt;&lt;img src="http://img.skitch.com/20091116-8uxrexs13eanpmanrque74txdh.preview.jpg" alt="Stanford Editor - Pyramid.java - Eclipse SDK - _Applications_eclipse_Eclipse.app_Contents_MacOS_cs106a_workspace" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128);font-family:Lucida Grande,Trebuchet,sans-serif,Helvetica,Arial;font-size:85%;"  &gt;Uploaded with &lt;a href="http://plasq.com/"&gt;plasq&lt;/a&gt;'s &lt;a href="http://skitch.com/"&gt;Skitch&lt;/a&gt;!&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;I ignored these problems for the time being and tried to see whether I could *just draw* the first brick of the first row. Below is the code for that part; I think it's in the right place. How can I modify it to get to the rest of the pyramid?&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * File: Pyramid.java&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * Name: &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * Section Leader: &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * ------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; */&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import acm.graphics.*;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import acm.program.*;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import java.awt.*;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class Pyramid extends GraphicsProgram {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void run() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        //Starting from the bottom, build rows&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        for (int i = BRICKS_IN_BASE; i &gt; 1; i --); {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            //For each row; starting from the left, lay bricks&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            for (int j = 1; j &lt;= 14; j++); {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                double x = getWidth()/2 - BRICK_WIDTH * 14/2;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                double y = getHeight()/2 + BRICK_HEIGHT * 14/2;            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                add(brick);    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/** Width of each brick in pixels */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private static final int BRICK_WIDTH = 30;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/** Width of each brick in pixels */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private static final int BRICK_HEIGHT = 12;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/** Number of bricks in the base of the pyramid */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private static final int BRICKS_IN_BASE = 14;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;What it looks like? One sad brick :(&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20091116-re5kwpu1iaxsjskbjrqba965mm.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 750px; height: 520px;" src="http://img.skitch.com/20091116-re5kwpu1iaxsjskbjrqba965mm.png" alt="" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-679898820212603376?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/679898820212603376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/11/problem-write-graphicsprogram-subclass.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/679898820212603376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/679898820212603376'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/11/problem-write-graphicsprogram-subclass.html' title='Work in Progress: Program To Draw A Pyramid'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-405200907887766862</id><published>2009-11-01T20:29:00.005-05:00</published><updated>2009-11-01T23:36:55.326-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while loop'/><category scheme='http://www.blogger.com/atom/ns#' term='temp variables'/><title type='text'>Program to Reverse the Digits in an Integer</title><content type='html'>&lt;span style="color: rgb(0, 0, 0);"&gt;The problem: "Rewrite the "Digit Sum" program given in Figure 4-6 so that instead of adding the digits in the number, it generates the number that has the same digits in the reverse order, as illustrated by this sample run:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;'This program reverses the digits in an integer.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Enter a positive integer: 1729&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;The reversed number is 9271'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;The idea in this exercise is not to take the integer apart character by character, which you will not learn how to do until Chapter 8. Instead, you need to use arithmetic to compute the reversed integer as you go. In this example, the new integer will be 9 after the first cycle of the loop, 92 after the second, 927 after the third, and 9271 after the fourth." (Robers ch 4, exercise 6).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Here's my code:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;* File: ReverseIntegers.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: Chubacca&lt;br /&gt;* -----------------&lt;br /&gt;*This program follows the insight from the Digital Root problem that the last digit in an integer is the&lt;br /&gt;*same thing as the remainder of that number when divided by 10. We use a while loop to divide the imput&lt;br /&gt;*by 10 repeatedly. Each time, we take the last digit of the integer, house it in the variable "temp",&lt;br /&gt;*and add it to the cumulating  variable "reverse." Then when n can't be divided by 10 any more, we print&lt;br /&gt;*the number housed in "reverse."&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class ReverseIntegers extends ConsoleProgram {&lt;br /&gt;   public void run() {&lt;br /&gt;     &lt;br /&gt;       println("This program reverses the integers in an integer.");&lt;br /&gt;     &lt;br /&gt;       int n = readInt("Enter a positive integer: ");&lt;br /&gt;     &lt;br /&gt;       //If there's only one digit, print; we're done&lt;br /&gt;       if (n &lt;= 9) {             println("The reversal is " + n);         }                 else {                         int temp = 0;             int reverse = 0;                         while (n &gt; 0) {&lt;br /&gt;               temp = n % 10;&lt;br /&gt;               reverse = 10*reverse + temp;&lt;br /&gt;               n/=10;&lt;br /&gt;           }&lt;br /&gt;           println ("The reverse is " + reverse);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;table style="color: rgb(0, 0, 0);" align="center" border="0" cellpadding="0" cellspacing="0" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td width="100%"&gt;&lt;form method="post" action="module.php?name=PrettyPrinter"&gt;&lt;!-- END index_on --&gt;&lt;/form&gt;&lt;br /&gt;&lt;/td&gt;                 &lt;td style="background-image: url(kleider/blue-sand/images/main_right.gif); background-repeat: repeat-y;" width="10"&gt;&lt;br /&gt;&lt;/td&gt;               &lt;/tr&gt;               &lt;tr&gt;                 &lt;td style="background-image: url(kleider/blue-sand/images/main_left.gif); background-repeat: repeat-y;" width="10"&gt;&lt;br /&gt;&lt;/td&gt;                 &lt;td style="background-image: url(kleider/blue-sand/images/main_middle.gif);" width="100%"&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;What the output looks like:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 0, 0);" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://img.skitch.com/20091102-nhteyjd4chwhffjqm3wti49upx.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 365px; height: 87px;" src="http://img.skitch.com/20091102-nhteyjd4chwhffjqm3wti49upx.png" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;What made it tough: This was actually quite easy to figure out, having just worked on the sister problems for digital root and fib sequence. It took about 10 minutes to sketch out. Jason and Annie's examples for fib sequence, which used temp variables, made it easy to think of using temp variables for this problem.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Time to complete: 20 minutes.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-405200907887766862?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/405200907887766862/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/11/program-to-reverse-digits-in-integer.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/405200907887766862'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/405200907887766862'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/11/program-to-reverse-digits-in-integer.html' title='Program to Reverse the Digits in an Integer'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-4191793376953302396</id><published>2009-10-12T17:35:00.007-04:00</published><updated>2009-10-12T23:29:31.489-04:00</updated><title type='text'>Program to Find the Digital Root of an Integer</title><content type='html'>&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;Problem: "The digital root of an integer n is defined as the result of summing the digits repeatedly until only a single digit remains. For example, the digital root of 1729 can be calculated using the following steps:&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Step 1: 1+7+2+9 = 19&lt;/div&gt;&lt;div&gt;Step 2: 1+9 = 10&lt;/div&gt;&lt;div&gt;Step 3: 1+0 = 1&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Because the total at the end of step 3 is the single digit 1, that value is the digital root.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Rewrite the DigitSum program shown in Figure 4-6 so that it calculates the digital root of the imput value." (Roberts ch 4, exercise 7).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here's the code:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td width="100%" style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px; "&gt;&lt;form method="post" action="http://prettyprinter.de/module.php?name=PrettyPrinter" style="font-size: 11px; font-family: Georgia, 'Times New Roman', Times, serif; color: rgb(0, 0, 0); margin-bottom: 0px; "&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;/*&lt;br /&gt;* File: DigitalRoot.java&lt;br /&gt;* Name: Chu&lt;br /&gt;* Section Leader: Ambiguous. Probably Usman Akeju.&lt;br /&gt;* -----------------------------&lt;br /&gt;&lt;br /&gt;This program uses a while loop inside while loop to run integer n thru the DigitSum&lt;br /&gt;program multiple times until you get to the digital root. The DigitSum program took advantage&lt;br /&gt;of the fact that the remainder of n after getting divided by 10 equals the last digit&lt;br /&gt;in the integer. This program uses that as the inner loop. Then after running through the inner&lt;br /&gt;loop, the output of the inner loop is made to be equal to the integer temp, which is&lt;br /&gt;evaluated as a condition of the outer while loop. This is what lets us feed the integer through&lt;br /&gt;the DigitSum program over and over.&lt;br /&gt;**/&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;public class DigitalRoot extends ConsoleProgram {&lt;br /&gt;&lt;br /&gt;public void run () {&lt;br /&gt; println("This program finds the digital root of an integer.");&lt;br /&gt; int n = readInt("Enter a positive integer: ");&lt;br /&gt;&lt;br /&gt; //If the imput is one digit to begin with, print it; we're done.&lt;br /&gt;&lt;br /&gt; if (n &amp;lt;=9) {&lt;br /&gt;     println("The digital root is " + n);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; else {&lt;br /&gt;&lt;br /&gt;     int dsum = 0;&lt;br /&gt;     int temp = n;&lt;br /&gt;&lt;br /&gt;     //Outer loop; keeps running until we get a single-digit output.&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;     while (temp &amp;gt; 9) {&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;         n = temp;&lt;br /&gt;         dsum = 0;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;         /*Inner loop: Keeps finding the remainder after you divide n by 10 to&lt;br /&gt;         * find the sum of digits&lt;br /&gt;         */&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;         while (n &amp;gt; 0) {&lt;br /&gt;             dsum += n % 10;&lt;br /&gt;             n /= 10;&lt;br /&gt;         }&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;div align="left"   style="  ;font-family:Georgia, 'Times New Roman', Times, serif;font-size:11px;"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;         //Set temp to dsum so that dsum can get fed through the outer loop again.&lt;br /&gt;         temp = dsum;&lt;br /&gt; &lt;br /&gt;     }&lt;br /&gt;     println("The digital root is " + temp);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/form&gt;&lt;/td&gt;&lt;td width="10" style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px; background-image: url(http://prettyprinter.de/kleider/blue-sand/images/main_right.gif); background-repeat: repeat-y; "&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td width="10" style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px; background-image: url(http://prettyprinter.de/kleider/blue-sand/images/main_left.gif); background-repeat: repeat-y; "&gt;&lt;/td&gt;&lt;td width="100%" style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px; background-image: url(http://prettyprinter.de/kleider/blue-sand/images/main_middle.gif); "&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What the output looks like:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;img src="http://1.bp.blogspot.com/_UagTlN5LQ1U/StOt3v-ZikI/AAAAAAAAAWY/5ar-kbUHbm8/s400/Digital+Root+Output.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5391844352175213122" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 362px; height: 164px; " /&gt;&lt;/div&gt;&lt;div&gt;What made it tough: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1) A loop of loops: &lt;/div&gt;&lt;div&gt;As was mentioned in the question, this problem builds off an example program given in the text to sum the digits of any given integer, which is any one of the steps 1 through 3 in the explanation. The sample problem gave us what would be the inner while loop of this program. The trick was to create a loop of loops that would take the solution from step 1 and feed it back through the inner loop to find the output for step 2, and so on.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I had a go at it at first by writing it as three individual loops:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:monospace;font-size:11px;"&gt;public class DigitalRoot extends ConsoleProgram {&lt;br /&gt;&lt;br /&gt;public void run () {&lt;br /&gt; println("This program finds the digital root of an integer.");&lt;br /&gt; int n = readInt("Enter a positive integer: ");&lt;br /&gt; int dsum = 0;&lt;br /&gt; while (n &amp;gt; 0) {&lt;br /&gt;     dsum += n % 10;&lt;br /&gt;     n /= 10;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; /*NEED: If dsum is &amp;gt; 10, send it back thru the while loop. (How?...)&lt;br /&gt; If dsum &amp;lt; 10, print "The digital root is" + dsum.*/&lt;br /&gt;&lt;br /&gt; int o = dsum;&lt;br /&gt; dsum = 0;&lt;br /&gt; while (o &amp;gt; 0) {&lt;br /&gt;     dsum += o % 10;&lt;br /&gt;     o /= 10;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; int p = dsum;&lt;br /&gt; dsum = 0;&lt;br /&gt; while (p &amp;gt; 0) {&lt;br /&gt;     dsum += p % 10;&lt;br /&gt;     p /= 10;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; println("The digital root is " + dsum);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The program worked only for inputs of 4 digits, since there are three loops. For a long time, I couldn't figure out how to get the output of one of the loops to cycle back up and go through the loop again.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2) Debugging:&lt;/div&gt;&lt;div&gt;Once I made up a loop of loops and used the temp variable (thanks to inspiration from the comments to Fib Seq), this problem gave me the damndest time when it came to debugging. For a long time after I sketched out the strategy and wrote my code, I'd run the program, enter the output, and got nothing; my program would just blink stupidly at me.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Usman showed me the clever tip to debug by printing lines of text at different stages of the program:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:monospace;font-size:11px;"&gt;&lt;br /&gt;//DEBUG:&lt;br /&gt;println("Entering OUTER loop: n= "+ n +" dsum= "+dsum +" temp= "+temp);&lt;br /&gt;while (temp &amp;gt; 9) {&lt;br /&gt;n = temp;&lt;br /&gt;dsum = 0;&lt;br /&gt;&lt;br /&gt;//DEBUG:&lt;br /&gt;println("Entering INNER loop: n= "+ n +" dsum= "+dsum+" temp= "+temp);&lt;br /&gt;while (n &amp;gt;= 0) {&lt;br /&gt;  dsum += n % 10;&lt;br /&gt;  n /= 10;&lt;br /&gt;  //DEBUG:&lt;br /&gt;  println(" End INNER loop: n= "+ n +" dsum= "+dsum+" temp= "+temp);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;temp = dsum;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span"  style="font-size:-webkit-xxx-large;"&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Georgia, serif;font-size:16px;"&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:130%;"&gt;&lt;span class="Apple-style-span"  style="font-size:16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-size:16px;"&gt;Inserting the debut text gave this output:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:130%;"&gt;&lt;span class="Apple-style-span"  style="font-size:16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:-webkit-xxx-large;"&gt;&lt;span class="Apple-style-span"  style=" ;font-size:16px;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_UagTlN5LQ1U/StO4ZUxbZ4I/AAAAAAAAAWo/w4zCpob6j6w/s1600-h/Digital+Root+Debug--+the+telltale.png" style="text-decoration: none;"&gt;&lt;img src="http://2.bp.blogspot.com/_UagTlN5LQ1U/StO4ZUxbZ4I/AAAAAAAAAWo/w4zCpob6j6w/s400/Digital+Root+Debug--+the+telltale.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5391855924104882050" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 363px; height: 158px; " /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;We had told the program to print text when a) it got to entering the outer loop, b) it entered the inner loop, and c) it exited the inner loop. Since the third line of text never got printed, that told me that the program was getting stuck somewhere in the inner loop. Specifically, it was getting stuck when it got to n = 0, since of the condition to keep the loop running was n&gt;=0.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(85, 26, 139); -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://www.geocities.com/Hollywood/Theater/4208/bigdoginfence.jpg" border="0" alt="" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 360px; height: 270px; " /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here's what the debug code looks like when fixed:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 238); -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://1.bp.blogspot.com/_UagTlN5LQ1U/StO42iU9W0I/AAAAAAAAAWw/jWnJgHlPr3A/s400/Digital+Root+Debug--+the+one+that+worked.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5391856425959775042" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 367px; height: 212px; " /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Time to completion: I read the problem and worked on it for an hour every night last week.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lingering questions:  How come this took so damn long!&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-4191793376953302396?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/4191793376953302396/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-find-digital-root-of-integer.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4191793376953302396'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/4191793376953302396'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-find-digital-root-of-integer.html' title='Program to Find the Digital Root of an Integer'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_UagTlN5LQ1U/StOt3v-ZikI/AAAAAAAAAWY/5ar-kbUHbm8/s72-c/Digital+Root+Output.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-7681274356895924433</id><published>2009-10-06T19:58:00.005-04:00</published><updated>2009-10-06T20:04:24.682-04:00</updated><title type='text'>Fib Sequence, the Usman Version</title><content type='html'>&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_UagTlN5LQ1U/SsvadWm-tfI/AAAAAAAAAWM/OT1Orbrbzdo/s1600-h/FibSequence2.png"&gt;&lt;/a&gt;OK. So in response to the Fib Sequence problem from 10/5, Usman ("manus") said that I could make my code more elegant by using two variables instead of three. Never one to back down from a challenge (ok, that's not true, but this time I don't), I'll giving it a whirl. I think I'm on the right track, but so far my answers aren't right. Any ideas, folks?&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;/*&lt;br /&gt;* File: FibSequence2.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: Me!&lt;br /&gt;* -----------------&lt;br /&gt;* THE USMAN VERSION&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class FibSequence2 extends ConsoleProgram {&lt;br /&gt;   public void run() {&lt;br /&gt;       //Getting the first two in the sequence started...this is a bit inelegant.&lt;br /&gt;       int n0 = 0;&lt;br /&gt;       int n1 = 1;&lt;br /&gt;       println(n0);&lt;br /&gt;       println(n1);&lt;br /&gt;&lt;br /&gt;       //Now kicking off the fun part.&lt;br /&gt;       for (int i = 2; i &amp;lt; END; i++) {&lt;br /&gt;           n1=n1+n0;&lt;br /&gt;           println(n1);&lt;br /&gt;           n0=n1;&lt;br /&gt;&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   //Private constant; "End" is the the nth number in the Fib sequence displayed.&lt;br /&gt;&lt;br /&gt;   private static final double END = 15;&lt;/span&gt;                        &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="font-family: Georgia, serif; color: rgb(0, 0, 238); font-size: 16px; -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://1.bp.blogspot.com/_UagTlN5LQ1U/SsvadWm-tfI/AAAAAAAAAWM/OT1Orbrbzdo/s400/FibSequence2.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5389641576899065330" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 160px; height: 400px; " /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-7681274356895924433?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/7681274356895924433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/10/fib-sequence-usman-version.html#comment-form' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7681274356895924433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7681274356895924433'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/10/fib-sequence-usman-version.html' title='Fib Sequence, the Usman Version'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_UagTlN5LQ1U/SsvadWm-tfI/AAAAAAAAAWM/OT1Orbrbzdo/s72-c/FibSequence2.png' height='72' width='72'/><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-2096866343084053274</id><published>2009-10-05T19:39:00.007-04:00</published><updated>2009-10-06T00:02:40.533-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fib sequence'/><title type='text'>Program to Display the First N Values of the Fibonacci Sequence</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_UagTlN5LQ1U/SsqhS0lsRsI/AAAAAAAAAVo/2wTq-hscXZg/s1600-h/Output+FibSequence.png"&gt;&lt;/a&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;Problem: "In mathematics, there is a famous sequence of numbers called the Fibonacci sequence, after the thirteenth-century Italian mathematician Leonardo Fibonacci. The first two terms in this sequence are 0 and 1, and every subsequent term is the sum of the preceding two. Thus the first several terms in the Fibonacci sequence are as follows:&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;fib0 = 0&lt;/div&gt;&lt;div&gt;fib1 = 1&lt;/div&gt;&lt;div&gt;fib2 = 1 (0+1)&lt;/div&gt;&lt;div&gt;fib3 = 2 (1+1)&lt;/div&gt;&lt;div&gt;fib4 = 3 (1+2)&lt;/div&gt;&lt;div&gt;fib5 = 5 (2+3)&lt;/div&gt;&lt;div&gt;fib6 = 8 (3+5)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Write a program to display the values in this sequence from fib0 through fib15" (Roberts Ch 4, Exercise 9).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Program from PrettyPrinter.de: (thanks for the tip, Arnab! no more screenshots of Eclipse)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', Times, serif; font-size: 11px; "&gt;&lt;br /&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 0); "&gt;/*&lt;br /&gt;* File: FibSequence.java&lt;br /&gt;* Name: Renee&lt;br /&gt;* Section Leader: Me!&lt;br /&gt;* -----------------&lt;br /&gt;* This program displays the first N numbers in the Fib sequence (the user can pick how&lt;br /&gt;* far N is by changing the private constant "END.") It first sets the sequence in motion&lt;br /&gt;* by explicitly defining and printing out fib0 and fib1, but henceforth builds off&lt;br /&gt;* the prior numbers of the sequence by re-setting the values of n2, n1, and n0 for every&lt;br /&gt;* loop in the program.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import acm.program.*;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class FibSequence extends ConsoleProgram {&lt;br /&gt;    public void run() {&lt;br /&gt;        //Getting the first two in the sequence started...this is a bit inelegant.&lt;br /&gt;        int n0 = 0;&lt;br /&gt;        int n1 = 1;&lt;br /&gt;        println(n0);&lt;br /&gt;        println(n1);&lt;br /&gt;       &lt;br /&gt;        //Now kicking off the fun part.&lt;br /&gt;        for (int i = 2; i &amp;lt; END; i++) {&lt;br /&gt;            int n2 = n1 + n0;&lt;br /&gt;            println(n2);&lt;br /&gt;            n0=n1;&lt;br /&gt;            n1=n2;&lt;br /&gt;           &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;   &lt;br /&gt;    //Private constant; "End" is the the nth number in the Fib sequence displayed.&lt;br /&gt;   &lt;br /&gt;    private static final double END = 15;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 238); -webkit-text-decorations-in-effect: underline; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span"  style="color:#0000EE;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;What the output looks like:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_UagTlN5LQ1U/SsqhS0lsRsI/AAAAAAAAAVo/2wTq-hscXZg/s1600-h/Output+FibSequence.png"&gt;&lt;img src="http://4.bp.blogspot.com/_UagTlN5LQ1U/SsqhS0lsRsI/AAAAAAAAAVo/2wTq-hscXZg/s400/Output+FibSequence.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5389297248828671682" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 202px; height: 400px; " /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What made it tough: There are some things you learn in Java that completely fly in the face of what you learned in math class. For example, if you have the decimal number (or, "double") 2.9, but you want it to be converted into an integer, it would be converted to 2. Not 3! Everything after the integer gets truncated, even if it's not the closest integer! Similarly, if your program is working in integers, and you divide 15 by 4, the number is displayed as 3! No decimal points allowed.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The other practice that I'm struggling with is that of re-declaring variables. In one program, n can equal 1, and later on in the program, n equals 2. N just changes as your program moves along if you want it to!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If that weren't anti-instinctive enough, this Fib program gets solved declaring a variable to equal another variable, but then changing &lt;i&gt;those &lt;/i&gt;assignments around. You find the number in a Fib sequence by summing the two outputs of the sequence that came before it (ie, Fib(n)=Fib(n-1)+Fib(n-2)), but since I can't do functions in Java yet, I store the values of the past functions by reassigning the variables down the chain.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you look at the "fun part," once we print n2 (the latest number in the sequence to be calculated), we move the value of n2 down to the variable n1, and move the value of n1 down to the variable n0, and then we use the new n1 and n0 to calculate the new n2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Time to completion: 3 hours.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lingering questions: I don't like that I started off explicitly writing fib(0) and fib(1) before I could start the elegant part of the code. Is there a way to do this without?&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-2096866343084053274?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/2096866343084053274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-display-first-n-values-of.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2096866343084053274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/2096866343084053274'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-display-first-n-values-of.html' title='Program to Display the First N Values of the Fibonacci Sequence'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_UagTlN5LQ1U/SsqhS0lsRsI/AAAAAAAAAVo/2wTq-hscXZg/s72-c/Output+FibSequence.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7850692473012665527.post-7415740475851668176</id><published>2009-10-05T18:54:00.006-04:00</published><updated>2009-10-05T19:27:12.926-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='while loop'/><category scheme='http://www.blogger.com/atom/ns#' term='sentinel'/><category scheme='http://www.blogger.com/atom/ns#' term='average'/><title type='text'>Program to Average an Indefinite List of Integers</title><content type='html'>&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span"  style="color:#0000EE;"&gt;&lt;span class="Apple-style-span" style="text-decoration: underline;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;Problem: "Write a program called AverageList that reads in a list of integers representing exam scores, and then prints out the average. Because some unprepared student might actually get a score of 0, your program should use -1 as the sentinel to mark the end of the input." (Roberts Ch 4, Exercise 5.)&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Program in Eclipse Editor (click to enlarge):&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_UagTlN5LQ1U/Ssp83jZ8b0I/AAAAAAAAAVQ/QWbO1GULIYI/s1600-h/AverageIntegerList.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 319px;" src="http://3.bp.blogspot.com/_UagTlN5LQ1U/Ssp83jZ8b0I/AAAAAAAAAVQ/QWbO1GULIYI/s400/AverageIntegerList.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5389257197940928322" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What the output looks like:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 238); -webkit-text-decorations-in-effect: underline; "&gt;&lt;img src="http://1.bp.blogspot.com/_UagTlN5LQ1U/Ssp-GqC6yuI/AAAAAAAAAVY/_Dna-Y2pztk/s400/Output+AverageIntegerList.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5389258556933065442" style="display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; text-align: center; cursor: pointer; width: 400px; height: 215px; " /&gt;&lt;/span&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What made it tough: One of the earlier problems in this set asked "Write a program that finds the average of a pre-defined list of integers". That was easier, because if the list took in N inputs from the user, you'd know to divide the sum of your integers by N. Another problem asked, "Write a program that finds the sum of an indefinite list of integers." That was also easier, because you could keep adding to your sum until the user entered the sentinel. This one took me a while to figure out how you'd know what to divide your sum by, but it eventually cemented in my head the ways you can use increment operators like i++.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Time to completion: 3 hrs&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lingering Questions: Although my program says that it can average non-negative integers, you really can average any integer except the one used as the sentinel. Ben wants to know why you can't just use a letter as your sentinel, so that you could average any possible integer. I tried setting the sentinel constant as "A." Eclipse didn't like that. Is there a way to design this program so that you could use a letter as your sentinel? &lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_UagTlN5LQ1U/Ssp5XyX_FgI/AAAAAAAAAVI/wic95l-fMDA/s1600-h/AverageIntegerList.png"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7850692473012665527-7415740475851668176?l=reneecoding.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://reneecoding.blogspot.com/feeds/7415740475851668176/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-average-indefinite-list-of.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7415740475851668176'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7850692473012665527/posts/default/7415740475851668176'/><link rel='alternate' type='text/html' href='http://reneecoding.blogspot.com/2009/10/program-to-average-indefinite-list-of.html' title='Program to Average an Indefinite List of Integers'/><author><name>Hello, I'm Renee.</name><uri>http://www.blogger.com/profile/02081872116763616695</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_UagTlN5LQ1U/Ssp83jZ8b0I/AAAAAAAAAVQ/QWbO1GULIYI/s72-c/AverageIntegerList.png' height='72' width='72'/><thr:total>1</thr:total></entry></feed>
