{"id":455,"date":"2009-02-03T07:38:18","date_gmt":"2009-02-03T12:38:18","guid":{"rendered":"http:\/\/itp.indiamos.com\/blog\/?p=455"},"modified":"2009-04-14T01:24:53","modified_gmt":"2009-04-14T06:24:53","slug":"reinventing-the-wheel","status":"publish","type":"post","link":"https:\/\/itp.indiamos.com\/blog\/2009\/02\/03\/reinventing-the-wheel\/","title":{"rendered":"Reinventing the wheel"},"content":{"rendered":"<p><a href=\"http:\/\/www.flickr.com\/photos\/powerhouse_museum\/2821106432\/\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/02\/wheel.jpg?resize=460%2C311\" alt=\"wheel\" title=\"wheel\" width=\"460\" height=\"311\" class=\"alignnone size-full wp-image-463\" srcset=\"https:\/\/i0.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/wheel.jpg?w=460&amp;ssl=1 460w, https:\/\/i0.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/wheel.jpg?w=400&amp;ssl=1 400w\" sizes=\"(max-width: 460px) 100vw, 460px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>This week&#8217;s A&#8211;Z assignment:<\/p>\n<blockquote><p>Create a program (using, e.g., the tools presented in class) that behaves like a UNIX text processing program (such as cat, grep, tr, etc.). Your program should take text as input (any text, or a particular text of your choosing) and output a version of the text that has been filtered and\/or munged. <!--more-->Your program should use at least one method of Java\u2019s String class that we didn\u2019t discuss in class.<\/p>\n<p>Be creative, insightful, or intentionally banal. Optional: Use the program that you created in tandem with another UNIX command line utility<\/p><\/blockquote>\n<p>Sounds easy-peasy, except that despite our handy <a href=\"http:\/\/www.decontextualize.com\/teaching\/a2z\/strung-out-on-java\/\">review session<\/a>, I&#8217;ve completely forgotten anything I ever knew about Java or Processing. Not good.<\/p>\n<p>Finally, after hours of beating my head against the couch and various O&#8217;Reilly books, I came up with two Java versions of the vowel shift&#8211;inducing UNIX command <code>tr 'AEIOUaeiou' 'EIOUAeioua'<\/code>. The first loops through the input letter by letter, checking each to see if it belongs to an array of vowels:<\/p>\n<p>[sourcecode language=&#8217;java&#8217;]import com.decontextualize.a2z.TextFilter;<\/p>\n<p>public class Tr extends TextFilter<br \/>\n{<br \/>\n    public static void main(String[] args)<br \/>\n    {<br \/>\n        Tr t = new Tr();<br \/>\n        t.run();<br \/>\n    } \/\/ end main(String[] args)<br \/>\n    public void eachLine(String line)<br \/>\n    {<br \/>\n        char [] vowels_in = new char [] { &#8216;A&#8217;, &#8216;a&#8217;, &#8216;E&#8217;, &#8216;e&#8217;, &#8216;I&#8217;, &#8216;i&#8217;, &#8216;O&#8217;, &#8216;o&#8217;, &#8216;U&#8217;, &#8216;u&#8217; };<br \/>\n        char [] vowels_out = new char [] { &#8216;E&#8217;, &#8216;e&#8217;, &#8216;I&#8217;, &#8216;i&#8217;, &#8216;O&#8217;, &#8216;o&#8217;, &#8216;U&#8217;, &#8216;u&#8217;, &#8216;A&#8217;, &#8216;a&#8217; };<br \/>\n        for (int i = 0; i < line.length(); i++)\n        {\n            char currentletter;\n            char currentvowel;\n            char printletter;\n            currentletter = line.charAt(i);\n            printletter = currentletter;\n            for (int v = 0; v < 10; v++)\n            {\n                currentvowel = vowels_in[v];\n                if (currentletter == currentvowel)\n                {\n                    printletter = vowels_out[v];\n                } \/\/ end if\n            } \/\/ end for v loop                \n            print(printletter);\n        } \/\/ end for i loop\n    } \/\/ end eachLine(String line)\n} \/\/ end Tr extends TextFilter[\/sourcecode]\n\nThe second rather tediously searches for and replaces each vowel in turn:\n\n[sourcecode language='java']import com.decontextualize.a2z.TextFilter;\n\npublic class Tr2 extends TextFilter \n{\n    public static void main(String[] args)\n    {\n        Tr2 t = new Tr2();\n        t.run();\n    } \/\/ end main(String[] args)\n    public void eachLine(String line)\n    {\n        String line_new;\n        line_new = line.replace( \"a\", \"[1]\" );\n        line_new = line_new.replace( \"e\", \"[2]\" );\n        line_new = line_new.replace( \"i\", \"[3]\" );\n        line_new = line_new.replace( \"o\", \"[4]\" );\n        line_new = line_new.replace( \"u\", \"[5]\" );\n        line_new = line_new.replace( \"A\", \"[6]\" );\n        line_new = line_new.replace( \"E\", \"[7]\" );\n        line_new = line_new.replace( \"I\", \"[8]\" );\n        line_new = line_new.replace( \"O\", \"[9]\" );\n        line_new = line_new.replace( \"U\", \"[10]\" );\n        line_new = line_new.replace( \"[1]\", \"e\" );\n        line_new = line_new.replace( \"[2]\", \"i\" );\n        line_new = line_new.replace( \"[3]\", \"o\" );\n        line_new = line_new.replace( \"[4]\", \"u\" );\n        line_new = line_new.replace( \"[5]\", \"a\" );\n        line_new = line_new.replace( \"[6]\", \"E\" );\n        line_new = line_new.replace( \"[7]\", \"I\" );\n        line_new = line_new.replace( \"[8]\", \"O\" );\n        line_new = line_new.replace( \"[9]\", \"U\" );\n        line_new = line_new.replace( \"[10]\", \"A\" );\n        print(line_new + \"\\n\");\n    } \/\/ end eachLine(String line)\n} \/\/ end Tr2 extends TextFilter[\/sourcecode]\n\nI tried to rewrite the latter using arrays and a loop, as well---\n\n\n\n[sourcecode language='java']import com.decontextualize.a2z.TextFilter;\n\npublic class TrB extends TextFilter \n{\n    public static void main(String[] args)\n    {\n        TrB t = new TrB();\n        t.run();\n    } \/\/ end main(String[] args)\n    public void eachLine(String line)\n    {\n        String line_new;\n        String find_string;\n        String replace_string;\n        \n        char [] vowels_in = new char [] { 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u' };\n        char [] vowels_out = new char [] { 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', 'A', 'a' };\n\n        line_new = line;\n        \n        for (int i = 0; i < vowels_in.length; i++)\n        {\n            find_string = \"[\" + i + \"]\";\n            line_new = line_new.replace( vowels_in[i], find_string );\n        }\n        for (int j = 0; j < vowels_in.length; j++)\n        {\n            replace_string = \"[\" + j + \"]\";\n            line_new = line_new.replace( replace_string, vowels_out[j] );\n        }\n        print(line_new + \"\\n\");\n    } \/\/ end eachLine(String line)\n} \/\/ end Tr2 extends TextFilter[\/sourcecode]\n\nbut I just couldn't get past this error message:\n\n\n\n<pre>rB.java:28: cannot find symbol\r\nsymbol  : method replace(char,java.lang.String)\r\nlocation: class java.lang.String\r\n            line_new = line_new.replace( vowels_in[i], find_string );\r\n                               ^\r\nTrB.java:33: cannot find symbol\r\nsymbol  : method replace(java.lang.String,char)\r\nlocation: class java.lang.String\r\n            line_new = line_new.replace( replace_string, vowels_out[j] );\r\n                               ^\r\n2 errors<\/pre>\n<p>What stupid, obvious thing am I doing wrong?<\/p>\n<h3>Sample Input<\/h3>\n<blockquote><p>Nelson was enjoying a little quiet at his house in the pretty village of Merton, in Surrey, when he learnt that the French and Spanish fleet, joined by the Ferrol squadron, had succeeded in entering Cadiz. His resolution was quickly taken. He went to the Admiralty and offered his services, which were joyfully accepted. Nelson was full of hope. \u201cDepend on it,\u201d he said to captain Blackwood, \u201cI shall yet give M. Villeneuve a drubbing.\u201d He formed his plans of attack during the short time of preparation, when the Victory had to be refitted, and other ships were to be got ready to accompany him. Lord Sidmouth told Mr. Eush, the American ambassador, that in the course of a visit he had received from Nelson, three weeks before the battle of Trafalgar, he described the plan of it, with bits of paper on a table, as it was afterwards fought. Yet he had a presentiment of his own fate. The coffin which was made out of the mast of l\u2019Orient was deposited at an upholsterer\u2019s. He desired its history to be engraved on its lid, saying that he should probably want it on his return. When he arrived at Portsmouth on the 14th of September, the enthusiasm of the people reached that height which sometimes gives a character of sublimity to the movements of multitudes acting with one heart. They wept; they blessed him; they even knelt as he passed along. The cheer which went up from thousands of voices as his barge pushed off to his flag-ship, -was the Godspeed of hia country. He waved his hat\u2014a last farewell to England.<br \/>\n\u2014<a href=\"http:\/\/books.google.com\/books?id=kWINAAAAIAAJ&#038;pg=PA445\"><cite>The Popular History of England: An Illustrated History of Society and Government from the Earliest Period to Our Own Times<\/cite><\/a> by Charles Knight (Boston: Estes and Lauriat, 1874)<\/p><\/blockquote>\n<h3>Sample Output<\/h3>\n<blockquote><p>Nilsun wes injuyong e lottli qaoit et hos huasi on thi pritty vollegi uf Mirtun, on Sarriy, whin hi liernt thet thi Frinch end Spenosh fliit, juonid by thi Firrul sqaedrun, hed sacciidid on intirong Cedoz. Hos risulatoun wes qaockly tekin. Hi wint tu thi Edmorelty end uffirid hos sirvocis, whoch wiri juyfally ecciptid. Nilsun wes fall uf hupi. \u201cDipind un ot,\u201d hi seod tu cepteon Bleckwuud, \u201cO shell yit govi M. Volliniavi e drabbong.\u201d Hi furmid hos plens uf etteck darong thi shurt tomi uf priperetoun, whin thi Voctury hed tu bi rifottid, end uthir shops wiri tu bi gut riedy tu eccumpeny hom. Lurd Sodmuath tuld Mr. Iash, thi Emirocen embessedur, thet on thi cuarsi uf e vosot hi hed riciovid frum Nilsun, thrii wiiks bifuri thi bettli uf Trefelger, hi discrobid thi plen uf ot, woth bots uf pepir un e tebli, es ot wes eftirwerds fuaght. Yit hi hed e prisintomint uf hos uwn feti. Thi cuffon whoch wes medi uat uf thi mest uf l\u2019Uroint wes dipusotid et en aphulstirir\u2019s. Hi disorid ots hostury tu bi ingrevid un ots lod, seyong thet hi shuald prubebly went ot un hos ritarn. Whin hi errovid et Purtsmuath un thi 14th uf Siptimbir, thi inthasoesm uf thi piupli riechid thet hioght whoch sumitomis govis e cherectir uf sablomoty tu thi muvimints uf maltotadis ectong woth uni hiert. Thiy wipt; thiy blissid hom; thiy ivin knilt es hi pessid elung. Thi chiir whoch wint ap frum thuasends uf vuocis es hos bergi pashid uff tu hos fleg-shop, -wes thi Gudspiid uf hoe cuantry. Hi wevid hos het\u2014e lest feriwill tu Inglend.<br \/>\n\u2014<i>Thi Pupaler Hostury uf Inglend: En Ollastretid Hostury uf Sucoity end Guvirnmint frum thi Ierloist Piroud tu Uar Uwn Tomis<\/i> by Cherlis Knoght (Bustun: Istis end Learoet, 1874)\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s A&#8211;Z assignment: Create a program (using, e.g., the tools presented in class) that behaves like a UNIX text processing program (such as cat, grep, tr, etc.). Your program should take text as input (any text, or a particular text of your choosing) and output a version of the text that has been filtered &hellip; <a href=\"https:\/\/itp.indiamos.com\/blog\/2009\/02\/03\/reinventing-the-wheel\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Reinventing the wheel<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[26,4,35],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3qY10-7l","_links":{"self":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/455"}],"collection":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":14,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/455\/revisions\/622"}],"wp:attachment":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}