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;

---------------------------------------------

Problem 1 Requirements

  1. 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

Code

Type

Meaning

G

Character

Green Martian

R

Character

Red Martian

I

Integer

Id

V

Integer

Volume

T

Integer

Tenacity

G 3 5

R 1 4 2

R 9 3 4

G 7 7

R 2 9 3

G 8 2

G 4 5

  1. Write the readMartiansFile method in the MartainManagerIO class.

  1. 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.

  1. 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).

  1. As martians are being read, as well as attempting to add them to a MartianManager, it should also keep track of:

  1. numLinesRead the total number of lines read
  2. numSuccessfullyAdded the total number of martians successfully added to the MartianManager
  3. numAlreadyExist these are martians that were not added because they were duplicates.
  4. numIllFormed the total number of lines read that were ill-formed
  5. Note: numLinesRead = numSuccessfullyAdded + numAlreadyExist + numIllFormed

  1. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!