package lab11; import java.util.Collections; import java.util.List; import java.util.ArrayList; public class Lab11 { /** * This method uses Bubble-Sort to sort the specified List of CatalogItems. * @param the ID type for each CatalogItem in catalogs over which CatalogItems are * ultimately sorted * @param the Media type which each CatalogItem in catalogs contains * @param catalog the List of CatalogItems to sort */ public static, S extends Media> void sort( List> catalog ) { int j; for( int i = 1; i < catalog.size(); i++ ) { j = i; while( j > 0 && catalog.get( j - 1 ).compareTo( catalog.get( j ) ) > 0 ) { Collections.swap( catalog, j, j - 1 ); j--; } } } public static void main( String[] args ) { //Create an ArrayList of CatalogItems named iPod //This ArrayList must take a type parameter of CatalogItem //CatalogItem in turn should take type parameters of Integer and CD //---here---- //adding CatalogItems iPod.add( new CatalogItem( 0, new CD( "Lady Gaga", "The Fame", 2008 ) ) ); iPod.add( new CatalogItem( 7, new CD( "Lady Gaga", "The Fame Monster", 2009 ) ) ); iPod.add( new CatalogItem( 5, new CD( "Jay-Z", "The Blueprint 3", 2009 ) ) ); iPod.add( new CatalogItem( 2, new CD( "Santana", "Supernatural", 1999 ) ) ); //printing the iPod catalog before and after sorting System.out.println( "iPod Catalog:" ); System.out.println( "Before Sorting:" ); System.out.println( iPod ); sort( iPod ); System.out.println( "\nAfter Sorting by ID:" ); System.out.println( iPod ); //Create an ArrayList of CatalogItems named kindle //This ArrayList must take a type parameter of CatalogItem //CatalogItem in turn should take type parameters of String and Book //---here----- //adding CatalogItems kindle.add( new CatalogItem( "B5", new Book( " Atlas Shrugged ", " Ayn Rand ", 1957 ) ) ); kindle.add( new CatalogItem( "A0", new Book( " Lord of the Rings: Fellowship of the Ring ", " J.R.R. Tolkien ", 1954 ) ) ); kindle.add( new CatalogItem( "C2", new Book( " Even Cowgirls Get the Blues ", " Tom Robbins ", 1976 ) ) ); kindle.add( new CatalogItem( "A1", new Book( " The Subtle Knife ", " Philip Pullman ", 1997 ) ) ); //printing the kindle catalog before and after sorting System.out.println( "\n\nKindle Catalog:" ); System.out.println( "Before Sorting:" ); System.out.println( kindle ); sort( kindle ); System.out.println( "\nAfter Sorting by ID:" ); System.out.println( kindle ); } }