/* File: sqls.java square list yanushka */ import java.util.*; import java.io.*; /** The class Test creates a square list test object. */ class Test { public static void main( String [] args ) { new SQTest(); } } /** The class SQTest manages a test of a square list data structure. */ class SQTest { private static final String NL = "\n", DELIMITERS = "\\p{Punct}+|\\p{Blank}+", DEFAULT_FILE = "sq.java"; private final static int BASE = 150, MAX = 200; /** Construct a square list test object. */ public SQTest() { testInts(); testWords( DEFAULT_FILE ); } /** Test a square list of integers with a random number generator. */ public void testInts() { SquareList< Integer > sl = new SquareList< Integer >(); Random rand = new Random(); int curr; for ( int i = 0; i < MAX; i++ ) { curr = rand.nextInt( BASE ); // sl.add( curr ); if ( ! sl.add( curr ) ) sl.remove( curr ); System.out.println( sl ); } } /** Test a square list of words with a text file. */ public void testWords( String fileName ) { try { SquareList< String > slW = new SquareList< String >(); Scanner sc = new Scanner( new File( fileName ) ), lSc; String nextLine, token; // Read a line. while ( sc.hasNextLine() ) { nextLine = sc.nextLine(); // Parse nextLine. lSc = new Scanner( nextLine ); lSc.useDelimiter( DELIMITERS ); while ( lSc.hasNext() ) { token = lSc.next().toLowerCase(); if ( token.length() > 0 ) // slW.add( token ); if ( ! slW.add( token ) ) slW.remove( token ); System.out.println( slW ); } } sc.close(); } catch( FileNotFoundException ie ) { ie.printStackTrace(); } } }