For Programming A to Z this week we were asked to use Java to create a text:
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. Your program should use at least one method of Java’s String class that we didn’t discuss in class.Be creative, insightful, or intentionally banal. Optional: Use the program that you created in tandem with another UNIX command line utility.
I wanted to create a completely silly text this week, so I based my assignment on Lewis Carroll’s poem, The Jabberwocky, replacing a’s with o’s. Here’s the code:
import com.decontextualize.a2z.TextFilter;
public class JabberMoshy extends TextFilter {
public static void main(String[] args) {
JabberMoshy j = new JabberMoshy();
j.run();
}
public void begin(){
}
public void eachLine(String line) {
String tmpString = line.replace(‘a’,'o’);
println(tmpString);
}
public void end(){
}
}
Here’s the command line input:
caroline-browns-computer:~/atoz caroline$ javac JabberMoshy.java
caroline-browns-computer:~/atoz caroline$ java JabberMoshy <jabberwocky.txt
And here’s the result:
‘Twos brillig, ond the slithy toves
Did gyre ond gimble in the wobe:
All mimsy were the borogoves,
And the mome roths outgrobe.“Bewore the Jobberwock, my son!
The jows thot bite, the clows thot cotch!
Bewore the Jubjub bird, ond shun
The frumious Bondersnotch!”He took his vorpol sword in hond:
Long time the monxome foe he sought–
So rested he by the Tumtum tree,
And stood owhile in thought.And, os in uffish thought he stood,
The Jobberwock, with eyes of flome,
Come whiffling through the tulgey wood,
And burbled os it come!One two! One two! And through ond through
The vorpol blode went snicker-snock!
He left it deod, ond with its heod
He went golumphing bock.“And host thou sloin the Jobberwock?
Come to my orms, my beomish boy!
O frobjous doy! Collooh! Colloy!”
He chortled in his joy.‘Twos brillig, ond the slithy toves
Did gyre ond gimble in the wobe:
All mimsy were the borogoves,
And the mome roths outgrobe.
Next steps: Make the replace function case-insensitive, replace multiple characters.