// MP1.java // Author: Douglas W. Jones // the official solution import java.io.FileInputStream; import java.io.FileNotFoundException; /** Error reporting package * Provide a standard prefix and behavior for error reporting * @author Douglas W. Jones * @version 2019-02-07 */ class Errors { /** Prefix string for error messages */ private static String prefix = "??: "; /** Set prefix on error reports, should be done before any error reports * @arg p the prefix on any error messages */ public static void setPrefix( String p ) { prefix = p; } /** Report nonfatal errors, output a message and return * @arg m the message to output */ public static void warn( String m ) { System.err.println( prefix + ": " + m ); } /** Report fatal errors, output a message and terminate the program * @arg m the message to output */ public static void fatal( String m ) { warn( m ); // could duplicate code from warn instead System.exit( 1 ); } } /** Main program for MP1 * All this version does is take a command line-parameter and report * if it can open it. * @author Douglas W. Jones * @version 2019-02-07 */ public class MP1 { /** Main program */ public static void main( String[] args ) { Errors.setPrefix( "MP1" ); if (args.length < 1) { Errors.fatal( "Missing argument" ); } if (args.length > 1) { Errors.warn( "Too many arguments " + args[1] ); // it's unnecessary to iterate through all the extra args } try { FileInputStream f = new FileInputStream( args[0] ); System.out.println( "Opened " + args[0] ); } catch (FileNotFoundException e) { Errors.fatal( "Can't open " + args[0] ); } } }