Design classes with the given specification in each problem, along with the appropriate test suite. Each method in a class, excluding mutators, accessors, and constructors, must include proper Javadoc comments describing the (1) purpose of the method, (2) parameters, and (3) return value.
Do not round your solutions. For example, do not use
Math.round or float your output to, e.g.,
2 decimal places, unless specified.
You may not use anything beyond Chapter 5.2. This includes but is not limited to:
.class when using assertThrows).Objects.equalsSystem.arraycopy, Arrays.copyOf.Files.createTempDirectory or similar methods.Files, Paths, Path, Files.newBufferedReader, etc. Instead, you must use the "legacy file I/O" classes, e.g., File, Scanner, FileReader, BufferedReader, FileWriter, PrintWriter, etc.Any violation results in a score of 0 on the lab.
Please contact a staff member if you are unsure as to whether you're allowed to use something.
Important Warning 1: Like Problem Set 12, it's very easy to tell when students are using generative AI to solve these problems, particularly those involving exceptions and I/O. Just work with your classmates and ask the TAs for help if you need it.
Important Warning 2: You must catch every possible checked exception in your code. Thus, none of your methods should include a throws clause in the header. If it does, then your code will not compile in the autograder.
Note 1: You should follow the advice from Problem Set 12's writeup on how to write tests for methods involving exceptions and I/O.
(100 points) Design the EchoOdds class, which contains a method called static void echoOdds(String inFile) that reads a file of line-separated integers, and writes only the odd numbers out to a file of the same name, just with the .out extension. If the file contains any non-integer values, throw an InputMismatchException.
For example, if the user calls echoOdds("file1a.in"), and file1a.in contains the following:
5 100 25 17 2 4 0 -3848 13
Then file1a.out is generated containing the following:
5 25 17 13
Another example is, if the user calls echoOdds("file1b.in"), and file1b.in contains the following:
5 100 25 17 THIS_IS_NOT_AN_INTEGER! 4 0 -3848 13
Then the program does not output a file because it throws an exception.
Hint: Don't be so eager to immediately rush into opening an output stream of some kind. Remember that, if there is even a single invalid number in the input file, your code should not produce an output file. Instead, read the input file and, if there is an invalid number, immediately throw the aforementioned exception. Otherwise, only then open the output stream and echo the odd integers. Doing it in this specific order will ensure that an output file is not erroneously created in the event that an invalid number is parsed.
Warning: When writing your test cases, you will need at least one case that uses assertThrows to verify that your method throws the appropriate exception when given an invalid input. If you want, you can check to see that the file was not created using assertFalse(new File("filename").exists()), but this is not required.
Files to Submit:
EchoOdds.java, EchoOddsTest.java