Question: package prob1; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import prob2.*; public class MartianManagerIO { /** * DO NOT ALTER THIS METHOD. */ public
package prob1;
import java.io.File;
import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import prob2.*;
public class MartianManagerIO {
/** * DO NOT ALTER THIS METHOD. */ public static void writeMartians(String fileName, MartianManager mm) { File file = new File(fileName); try { writeMartiansFile(file, mm); } catch (FileNotFoundException e) { System.out.println("Error writing file"); e.printStackTrace(); } } /** * YOU WRITE THIS METHOD. * * Write the martians in the MartianManager to the file. The format is exactly the same * as specified in the homework document for reading valid data: G I V or R I V T. */ private static void writeMartiansFile(File file, MartianManager mm) throws FileNotFoundException { } }
/** * DO NOT ALTER THIS METHOD. */ public static ReadReport readMartians(String fileName) { File file = new File(fileName); ReadReport report = null; try { report = readMartiansFile(file); } catch (FileNotFoundException e) { System.out.println("Error reading file"); e.printStackTrace(); } return report; } /** * YOU WRITE THIS METHOD. * * Reads a text file that contains Martian data and returns a ReadReport object. Details * are in the homework document. * * @param file * @return * @throws RuntimeException * @throws FileNotFoundException */ private static ReadReport readMartiansFile(File file) throws RuntimeException, FileNotFoundException { return null; }
}
-------------------------------------------------
- Write the writeMartiansFile method in the MartainManagerIO class. This method accepts a MartianManager and should loop through the martians in the MartianManager and write the martians to the file object that is passed as an argument, using this format:
| Format, either of these is valid | Key | Example | ||||||||||||||||||
| G I V R I V T
|
| G 3 5 R 1 4 2 R 9 3 4 G 7 7 R 2 9 3 G 8 2 G 4 5
|
- Write the readMartiansFile method in the MartainManagerIO class.
- A valid line in the file is one of the two formats above. If a line does not meet either of these two formats then it is considered ill-formed.
- When you read a valid martian, then an attempt is made to add it to the MartianManager (the MartianManager does not allow duplicates, so it may not be added).
- As martians are being read, as well as attempting to add them to a MartianManager, it should also keep track of:
- numLinesRead the total number of lines read
- numSuccessfullyAdded the total number of martians successfully added to the MartianManager
- numAlreadyExist these are martians that were not added because they were duplicates.
- numIllFormed the total number of lines read that were ill-formed.
Note: numLinesRead = numSuccessfullyAdded + numAlreadyExist + numIllFormed
- To conclude this method, you will build a ReadReport object and return it. The constructor for that class is:
public ReadReport(
MartianManager mm,
String fileName,
int numLinesRead,
int numSuccessfullyAdded,
int numAlreadyExist,
int numIllFormed)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
