This content can't be printed from the browser. Use Export PDF with Pro for an offline copy.
OrinWorks, a repair workshop, records component names in sequential text files when maintenance tasks are completed. The following program processes a demonstration file. Assume that all file operations succeed.
1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.util.Scanner;
5 public class MaintenanceLog {
6 public static void main(String[] args) throws IOException {
7 FileWriter writer = new FileWriter("maintenance.txt");
8 writer.write("Valve
Pump
");
9 writer.close();
10 FileWriter appender = new FileWriter("maintenance.txt", true);
11 appender.write("Filter
");
12 appender.close();
13 Scanner reader = new Scanner(new File("maintenance.txt"));
14 reader.nextLine();
15 while (reader.hasNextLine()) {
16 System.out.println(reader.nextLine());
17 }
18 reader.close();
19 }
20 }
Explain why append mode is suitable when adding newly completed maintenance tasks to the workshop's existing historical log.
A separate text file contains at least one component name, with one name on each line. Describe the steps of a method that returns the last component name in this sequential file without changing its contents. Do not write code.
State what the condition on line 15 tests.
State the output of the program.
Construct the static method appendMatches, with return type int and parameters String source, String destination and String component. It receives the names of two different text files and a component name. The source file contains one component name per line. The method must append every source line that exactly matches component to the destination file, placing each appended name on a separate line. It must preserve all existing destination records and return the number of records appended.
Assume that both files exist, the destination is empty or ends with a newline, and all file operations succeed. The method may declare that it throws IOException.
MerePath operates self-guided nature walks and stores stop labels in sequential text files. The following demonstration program creates and reads a file; the label END separates the current route from a reserve stop. Assume that all file operations succeed.
1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.util.Scanner;
5 public class RouteFile {
6 public static void main(String[] args) throws IOException {
7 FileWriter writer = new FileWriter("route.txt");
8 writer.write("Pond
Bridge
END
Gate
");
9 writer.close();
10 Scanner reader = new Scanner(new File("route.txt"));
11 String label = reader.nextLine();
12 System.out.println(label);
13 while (!label.equals("END")) {
14 label = reader.nextLine();
15 }
16 System.out.println(reader.nextLine());
17 reader.close();
18 }
19 }
Each morning, the complete route is saved to an existing file. Only the new route must remain in that file. Explain why write mode is suitable for this task.
A separate route file contains one stop label on each line and does not contain END. Describe, without writing code, the steps of a method that receives the filename and a target label, and returns the line number of the first occurrence of that label. Line numbering starts at 1. The method must return 0 if the target is absent and must leave the file unchanged.
State what the condition on line 13 tests.
State the output of the program.
Construct the static method numberStops, with return type int and parameters String source and String destination. It receives the names of two different text files. The source exists and contains one stop label on each line, with no END separator or blank lines.
The method must replace the destination file with a numbered copy of every source line, in the same order. Each output line must contain its line number starting at 1, followed by a colon, one space and the original label. It must return the number of lines copied, returning 0 for an empty source. Both files must be closed and the source must remain unchanged. You may declare that the method throws IOException.
VelaSeed, a community seed archive, stores packet descriptions in sequential text files. Blank lines separate groups of descriptions. The following program creates and examines a small file. Assume that all file operations succeed.
1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.util.Scanner;
5 public class SeedArchive {
6 public static void main(String[] args) throws IOException {
7 FileWriter writer = new FileWriter("packets.txt");
8 writer.write("Clover
Flax
");
9 writer.close();
10 Scanner reader = new Scanner(new File("packets.txt"));
11 int blank = 0;
12 int filled = 0;
13 while (reader.hasNextLine()) {
14 String line = reader.nextLine();
15 if (line.length() == 0) {
16 blank++;
17 } else {
18 filled++;
19 }
20 }
21 reader.close();
22 System.out.println(blank);
23 System.out.println(filled);
24 }
25 }
Explain why read mode is suitable when an archive worker needs to examine every saved description without changing the file.
Describe the steps of a method that combines two existing archive files into a third file. The third file must contain all lines of the first file followed by all lines of the second file, including blank lines. All three filenames are different. Each non-empty source file ends with a newline. Describe the steps without writing code.
State what the condition on line 15 tests.
State the output of the program.
Construct the static method copyPreview, with return type int and parameters String source, String destination and int limit. The method must replace the destination file with the first limit lines of the source file, or all its lines if it contains fewer than limit lines. It must retain blank lines and return the number of lines copied. The filenames are different, limit is non-negative, and each non-empty source file ends with a newline. The method may declare IOException.
The Luma Haven shelter records deliveries of food bags in a sequential text file. Each delivery record contains a delivery identifier followed by the number of bags.
Figure 1 in shows that calculates the total number of bags delivered. contains for this flowchart. The list DELIVERIES represents the fields read from the file in order. Assume that all file operations succeed.
State one reason why a text file should be closed after writing to it.
Trace in for the delivery records printed there. Copy and complete in . Each row records one evaluation of a test: write that test's result in its column, write a variable's value only when it changes, and record the output on the row where it is produced.
State one file-processing operation that would be required when implementing using the sequential text file.
The shelter needs a new text file named largeDeliveries.txt containing only delivery records with at least 5 bags. Each selected record must retain both fields and appear in its original order. Any previous contents of largeDeliveries.txt must be replaced.
Construct an algorithm, as numbered steps in structured English, to produce this file from the deliveries file. Your algorithm must also work when the deliveries file is empty. Do not write code.
A separate text file named evening.txt initially contains the single record D18, 5 on its own line. The following operations are performed in order:
Sketch the final contents of evening.txt as a labelled file box, showing each record on a separate line.
A delivery identifier and its number of bags have already been entered and validated. Describe the steps to add this delivery as a new record at the end of the existing deliveries file without changing any earlier records. Do not write code.
Build a Practice Exam — Computer Science HL
Test your knowledge with a custom paper containing questions from selected topics.

