How to make a text File: https://www.youtube.com/watch?v=BKvb0_hKELA This video will help with using the text file(For time
Question:
How to make a text File: https://www.youtube.com/watch?v=BKvb0_hKELA
This video will help with using the text file(For time start at 10:00):
https://www.youtube.com/watch?v=GIjq9Y7qZeg&list=PLxmkTORwPF14R8E1w2R1g4el8ATG3LMT8&index=7&t=2118s
My Haiku is:
An afternoon breeze
expels cold air, along with
the fallen brown leaves
Step 1: Write a Vastly Inferior Haiku Using Notepad (or another program), make a text file called HaikuPoem1.txt and save it into the into a "Data" folder, in your assignment project folder. (You can find the location of your assignment project folder by either hovering your cursor over the project, or right-clicking -> Properties -> Sources -> Project Folder). In the file, you should make a haiku written by you. Each line of the haiku should be written on a separate line of the file.
Step 2: Make a Method for Loading your Vastly Inferior Haiku into an Array or ArrayList Make new java file called IA5_Haiku_LastName.java. In this file you will need to do:
1. Add code to load and save a text file (OR use your Files.java 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 haiku.
4. Sort the array using SELECTION or INSERTION sort from CodeHS, not bubble sort. You may have to change the algorithm slightly to sort strings alphabetically, like you did with bubble sort in assignment 3. The sort will automatically use the first character of each line.
Selection Sort Code:
For time: (5:23 - 6:00)
https://www.youtube.com/watch?v=WTRUBQoAbY8
Insertion Sort Code:
For time: (2:27 - 3:00)
https://youtu.be/_mFBTs-Pf3E
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
Note, the sorted poem is no longer a proper haiku. (Make sure that it isn't.) It's cool. Ms. Wiebe won't tell the haiku police, and you won't be sent to haiku jail for five months, seven weeks, and five days.
5. Print the title: SORTED HAIKU 6. Print the newly sorted array. 7. 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%. You may want to demonstrate it to your teacher when finished.
Next 10% If you are really worried about the haiku police, you can program a way for Ms. Wiebe to fix the haiku for you. Come up with a way for Ms. Wiebe to change one word or one line in your haiku after it has been sorted but BEFORE it saves. You can do this how you want but you may want to use String.replace(oldwords, newwords)
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 they would like to open. You can assume the user will type the name of the file exactly correct.
This is the code for the Files Class:
// This class has methods for saving and loading arrays from files. import java.io.*; // imports all of the input/output classes, including PrintWriter, // FileWriter, BufferedReader, FileReader, etc. They will help us work with the files.
public class Files {
//************** String Array Methods *********************// public static void saveFile(String filename, String[] array) { // This method takes the name of the file you will save to and the array // you will be saving as parameters. try { // try allows java to try to execute some code, and if an error occurs, // instead of halting the entire program, you run whatever is in catch. // The technical term for these errors is Java throwing an exception, // so the 'catch' code catches the exception and does something with it. PrintWriter file = new PrintWriter(new FileWriter(filename)); // This line creates a FileWriter object which, as the name suggests, // allows you to write to a file. However, we use the PrintWriter // class and the FileWriter object we just created to make a PrintWriter // object called "file". Basically, this allows us to use regular print // commands to format what we want to print to put in the file. for(int i=0; i
public static String[] loadStringArr(String filename) { //Note: This is not a void method. We will be returning a String array. //Also, the filename of the file you will load into the array you are // going to return is required. String addLines=""; try { BufferedReader file = new BufferedReader(new FileReader(filename)); // As the name suggests, FileReader is a class that allows you to read // files. BufferedReader just speeds up the reading process. while (file.ready()) { // .ready() just checks to see if the file // has data ready to be read addLines += file.readLine()+","; // The lines from the file are //added to the addLines string, separated by commas. } } catch (IOException e) { System.out.println(e); } return addLines.split(","); // .split(",") takes a string and splits it into // a string array whereever a comma is present. We placed commas between // each line read from the file, so each line in the final is turned into // an element in the array this method returns. }//end loadStringArr
//********************** Number Array Methods ******************// public static void saveFile(String filename, int[] array) { try { PrintWriter file = new PrintWriter( new FileWriter(filename) ); for(int i=0; i
} } catch (IOException e) { System.out.println(e); }
String[] tempStringArray = addLines.split(","); // .split() gives us a string array, but we have to convert it to // an integer array. int[] tempIntArray = new int[tempStringArray.length]; // make a temporary // integer array the same length as the temp string array for (int i = 0; i < tempIntArray.length; i++) { tempIntArray[i] = Integer.parseInt(tempStringArray[i]); // Go through each element in the string array, convert it to an // integer, then add it to the integer array. } return tempIntArray; // return your newly made integer array.
}//end loadIntArr
public static double[] loadDoubleArr(String filename) { String addLines = ""; try { BufferedReader file = new BufferedReader(new FileReader(filename)); while (file.ready()) { addLines += file.readLine() + ",";
} } catch (IOException e) { System.out.println(e); }
String[] tempStringArray = addLines.split(","); double[] tempDoubleArray = new double[tempStringArray.length]; for (int i = 0; i < tempDoubleArray.length; i++) { tempDoubleArray[i] = Double.parseDouble(tempStringArray[i]); } return tempDoubleArray;
}//end loadIntArr
}//end Files Class
Numerical Methods With Chemical Engineering Applications
ISBN: 9781107135116
1st Edition
Authors: Kevin D. Dorfman, Prodromos Daoutidis