Intro to Java Programming 1. Implement the method getSequence that takes a Scann
ID: 3703709 • Letter: I
Question
Intro to Java Programming
1. Implement the method getSequence that takes a Scanner as a parameter. It should read Strings one line at a time from the Scanner and store them in an ArrayList<String> until it reaches a line with a single '.' character on it. It should then return the array list to the calling program.
2. Now implement the method summarizeSequences that takes a String as a parameter. This method should open the file named by this String, and read a sequence from that file into an array list using the getSequence method you wrote above. It should then display on a single line the first, last, and middle String in the sequence (if the list is even, the position of the "middle" String is the size of the list divided by 2). Make sure you catch and handle the possible IOException exceptions.
3. Now implement the method writeSequenceReverse that takes a PrintWriter and an ArrayList<String> as parameters. This method shouldwrite the Strings in the ArrayList to the PrintWriter in reverse order and then write a line with a single '.' on it.
4. Now implement a method reverseSequences that takes two Strings as parameters. The first String is the name of an input file containing String values and the second is the name of an output file to write the reverse sequences to. The method should read sequences from the input file one sequence at a time using getSequence and then write them out to the output file. Do not forget to close the output file when the method is finished. Make sure you catch and handle the possible IOExceptionexceptions.
Explanation / Answer
package tutor;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Sequence {
public static ArrayList<String> getSequence(Scanner in) {
ArrayList<String> sequence = new ArrayList<String>();
while (in.hasNext()) {
String input = in.nextLine();
if (input == ".")
break;
sequence.add(input);
}
return sequence;
}
public static void summarizeSequences(String fileName) {
ArrayList<String> sequence = null;
try {
File file = new File(fileName);
Scanner sc = new Scanner(file);
// reading from file using scanner
sequence = getSequence(sc);
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
if (sequence != null && sequence.size() > 0) {
int mid = sequence.size() / 2;
System.out.print("First: " + sequence.get(0));
System.out.print(" Last: " + sequence.get(sequence.size() - 1));
System.out.println(" Mid: " + sequence.get(mid));
}
}
public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list) {
if (list == null)
return;
for (int i = list.size() - 1; i >= 0; i--) {
out.write(list.get(i));
}
out.write(".");
}
public static void reverseSequences(String inputFile, String outputFile) {
// reading from file
ArrayList<String> sequence = null;
try {
File file = new File(inputFile);
Scanner sc = new Scanner(file);
sequence = getSequence(sc);
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
//writing to output file
try {
File outFile = new File(outputFile);
PrintWriter writer = new PrintWriter(outFile);
writeSequenceReverse(writer, sequence);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.