import java.io.StreamTokenizer; import java.io.IOException; public class Add implements Transformation { /** * The integer value to add to each element of args for * this Transformation's operation. * * @see Add#transform */ private int addValue; /** * Whether addValue has been properly defined during * readArgs(). If false then transform() * will do nothing. * * @see Add#readArgs * @see Add#transfrom */ private boolean addDefined = false; /** * Creates a new Add transformation. * */ public Add() { } /** * Reads in the required arguments for this Transformation via the * StreamTokenizer, s. *

* Add expects only one argument, mainly the integer value by which to * increase each element of args during Add's transformation. * * @param s The StreamTokenizer from which this Transformation * will collect it's required arguments (if any). * * @see Add#transform */ public void readArgs(StreamTokenizer s) { boolean error = false; // yes/no error occurred while reading // input try { DONE: while ( true ) { switch( s.nextToken() ) { case StreamTokenizer.TT_NUMBER : if ( addDefined ) error = true; else { addValue = (int) s.nval; addDefined = true; } break DONE; case StreamTokenizer.TT_EOL : if ( addDefined == false ) error = true; break DONE; case StreamTokenizer.TT_EOF : if ( addDefined == false ) error = true; break DONE; default : //////////////////////////////////////// // Any data other than integer data is // considered faulty. error = true; break DONE; } } //////////////////////////////////////// // If an error occurred then print help // message. if ( error ) { addDefined = false; // insure transform does not modify data help( ); } } catch (IOException e) { System.err.println( "Error in input stream."); System.exit( -1 ); } } /** * Performs Add's transformation operation on args. Add adds * it's stored integer value to each element of args unless * an error occurred during readArgs in which case this * transformation does nothing. * * @param args The inputs to this Transformation's operation. The * elements of arg are modified by this * operation. */ public void transform( float[] args ) { if ( addDefined ) for (int i = 0; i < args.length; i++) args[i] += addValue; } /** * Returns information regarding the usage of Add. * */ public void help() { System.out.println("Usage: Add "); } }