File IO¶
Assignment¶
Read the integers in input.txt and write their sum to output.txt.
The student submits a Summer.java class with a public static void run()
method. The teacher provides input.txt by uploading it as a Manage-file,
so Tin stages it next to the submission automatically - this is the Java
equivalent of the Python grader’s --read argument.
Sample Solution¶
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class Summer {
public static void run() throws Exception {
long sum = 0;
try (Scanner in = new Scanner(new File("input.txt"))) {
while (in.hasNextInt()) {
sum += in.nextInt();
}
}
try (PrintWriter out = new PrintWriter("output.txt")) {
out.println(sum);
}
}
}
Sample Grader¶
import static org.junit.Assert.assertEquals;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Test;
// `input.txt` is uploaded as a Manage-file, so Tin stages it next to the
// submission automatically - the student's code can open it by its plain name.
// The student's `Summer` class reads it and writes the sum to `output.txt`,
// which this grader then checks.
public class Grader {
@Test
public void writesCorrectSum() throws Exception {
Summer.run();
String result = new String(
Files.readAllBytes(Paths.get("output.txt"))
).trim();
// the integers in input.txt add up to 42
assertEquals("42", result);
}
}
Note
Upload input.txt as a Manage-file on the assignment’s “Edit” page. Any data or
helper files listed there are staged next to the submission before it is
compiled and run.