import java.util.*; import java.awt.Color; import uwcse.graphics.*; import uwcse.io.*; public class ExerciseSolution { private DataSource dataSource; /** * Constructor just constructs a DataSource to be used * for all exercises. The first argument (15) is the * number of rectangles to generate as data. The * second argument (9722321) is arbitrary. Change it * to get different random (but repeatable) data. */ public ExerciseSolution() { dataSource = new DataSource( 25, 9722321 ); } /** * This routine causes exercise 'exerciseNum' to be run. * The only implementation in here now is for exercise 0, * the null exercise, written just to show the control flow. * (We don't even care exercise is asked for, we just * execute exercise 0 in this skeleton code.) */ public void runExercise( int exerciseNum ) { HashSet dataSet = null; ArrayList dataList = null; int item; Iterator iterator = null; ArrayList resultList = null; Rectangle nextRect; Rectangle resultRect; // this call is required to alert the DataSource what exercise we're working on dataSource.startExercise( exerciseNum ); // not all of these calls are needed for each individual exercise, but they // do no harm - null is returned if the exercise doesn't use a particular kind of // collection class dataSource.startExercise( exerciseNum ); dataSet = dataSource.getDataSet(); dataList = dataSource.getDataList(); // exercise 11 doesn't need this, but the others do, so it's convenient to put it here resultList = new ArrayList(); // and they're off... switch ( exerciseNum ) { case 0: // the solution for exercise 0 is provided as a (trivial) example dataSource.registerResult( resultList ); break; //------------------------------------------------------------------- // DELETE FROM HERE //------------------------------------------------------------------- case 1: // copy list iterator = dataSet.iterator(); while ( iterator.hasNext() ) { resultList.add( iterator.next() ); } dataSource.registerResult( resultList ); break; case 2: // all blue iterator = dataSet.iterator(); while ( iterator.hasNext() ) { nextRect = (Rectangle)iterator.next(); if ( nextRect.getColor() == Color.blue ) { resultList.add( nextRect ); } } dataSource.registerResult( resultList ); break; case 3: // largest resultRect = null; iterator = dataSet.iterator(); while ( iterator.hasNext() ) { nextRect = (Rectangle)iterator.next(); if (resultRect == null) { resultRect = nextRect; } else { if ( resultRect.getWidth()*resultRect.getHeight() <= nextRect.getWidth()*nextRect.getHeight() ) { resultRect = nextRect; } } } if (resultRect != null) { resultList.add( resultRect ); } dataSource.registerResult( resultList ); break; case 4: // find first red iterator = dataSet.iterator(); while ( iterator.hasNext() ) { nextRect = (Rectangle)iterator.next(); if ( nextRect.getColor() == Color.red ) { resultList.add( nextRect ); break; } } dataSource.registerResult( resultList ); break; case 5: // copy list for ( item = 0; item= 0 && cmd < DataSource.NUMTESTS ) { mySolution.runExercise( cmd ); } else { cmd = -1; } } while ( cmd >= 0 ); System.exit( 0 ); } }