Published using Google Docs
IA 7 - Master of the Haiku (a sorting assignment)
Updated automatically every 5 minutes

IA 7 - Master of the Haiku

Sorting Text Files (Iterative Algorithms)

It will come as no surprise to you that Mr. Couprie is world renowned as the Master of the Haiku. It is now your turn, Grasshopper, to learn the art of the ku.

Yes you shall sort thus

Your data for the masses

Bring thee thine arrays.

By Master of the Haiku, Scott Couprie, Copyright 2019

Step 1: Write a Vastly Inferior Haiku

Using Notepad (or another program), create a text file called HaikuPoem1.txt and save it into the main  folder of your Iterative Algorithms project.  In the file, you should type a haiku written by you.  Each line of the haiku should be written on a separate line of the file.  Do not worry.  The quality of your haiku will not be judged as yours will pale in comparison to those of the master.

Step 2: Create a Method for Loading your Vastly Inferior Haiku into an Array or ArrayList (70%)

Create new java file called IA7_Haiku.  

In this file you will need to do:

  1. Add code to load and save a text file (OR use your FILES file if you already have it).  Scroll down to the end of this document for the complete load and save code.  Your 3 line text file should end up creating a 3 item String Array.
  2. In your main method, load the file into an array.
  3. Print the following title: ORIGINAL HAIKU and then print the contents of the array so it looks like a standard 3 line array.
  4. Sort the array using SELECTION or INSERTION sort that you found for the last assignment, not bubble sort.  The sort will automatically use the first character of each line.  

Before Sort

The wonderment of

Life.  Be around you all now.

Silicon is life

After sort

Life.  Be around you all now.

Silicon is life

The wonderment of

As you are not cultured in the Art of the Ku, you may not realize that the sorted poem is no longer a proper Haiku.  Fear not.  The sorted Haiku is an ancient modification and is only allowed by the masters. Therefore, this is only between us as teacher and student.  Do not publish this or you will be spoken of with derision and I may be sanctioned by the World Haiku Dojo.

  1. Print the title: SORTED HAIKU
  2. Print the newly sorted array.
  3. Save the array back out to the file.

Run the code and then re-open the original HaikuPoem1.txt  file using Notepad to see that it has been changed.  Put it back to the original and resave it so that you can test the program again.  You will need to do this every time you run the code.

This part of the assignment will be marked as complete/incomplete and will be worth 70%.  Demonstrate it to your teacher when finished.

Next 10%

As you are only beginning to learn the Haikuiian Arts, Mr. Couprie will fix your errors.   Come up with a way for Mr. Couprie to change one word or one line in your haiku after it has been sorted but BEFORE it saves.

Next 10%

Right now, the code should save the file automatically.  Instead, give the user the OPTION to save the file or not before exiting.

Final 10%

  1. Create two additional text files called haikupoem2.txt and haikupoem3.txt
  2. Ask the user the name of the file he/she would like to open.  You can assume the user will type the name of the file exactly correct.  If you are up for the challenge add the GUI JFileChooser option.

Advanced Challenge Just for Fun

Create a text file that has a haiku consisting of only 1 syllable words.  Try to figure out how to test to see if it is a legitimate 5-7-5 haiku.

My Computer Breathes

Life as its Science Strengthens

My Mind like Barbell

By Master of the Haiku, Scott Couprie, Copyright 2019


Text File Help

Write your Haiku file on 3 separate lines.  The following method adds those lines into one String with each line separated by commas.  Then, we Split the String into an array.

public static String[] loadStringArr (String filename){

String addLines=””;

try {

BufferedReader file = new BufferedReader(new FileReader(filename));

while (file.ready()) {

                addLines += file.readLine();

                addLines+=”,”;

}//end while

} catch (IOException e) {

            System.out.println(e);

}

String[] tempStringArray = addLines.split(",");

return tempStringArray;

}//end loadStringArr from a text file

//saves a string array into a line by line text file.

public static void saveStringArray(String filename,String[] temp ) {

try {

PrintWriter file = new PrintWriter(new FileWriter(filename));

for (int i = 0; i < temp.length; i++) {

file.println(temp[i]);

}

file.close();

} catch (IOException ex) {

System.out.println(ex.toString());

}

}//end saveStringArray to a text file