Java
public static void main(String[] args) { Butterfly b01 = new Butterfly("Malachite", "green", "North America"); Butterfly b02 = new Butterfly("Red Rim", "red and black", "Central America"); Butterfly b03 = new Butterfly("Zebra Longwing", "black and yellow", "Central America"); Butterfly b04 = new Butterfly("Western Blue Beauty", "blue", "Africa"); Butterfly b05 = new Butterfly("Fairy Hairstreak", "white", "Africa"); Butterfly b06 = new Butterfly("Gaudy Commodore", "orange", "Africa"); System.out.println(b01); System.out.println(b02); ButterflyCollection bCol = new ButterflyCollection("Australia", "South America", "Central America"); bCol.add(b01); bCol.add(b02); bCol.add(b03); bCol.add(b04); bCol.add(b05); bCol.add(b06); System.out.println(" --- North America ---"); bCol.listByRegion("North America"); System.out.println(" --- Africa ---"); bCol.listByRegion("Africa"); System.out.println(" --- Central America ---"); bCol.listByRegion("Central America"); System.out.println(" --- Australia ---"); bCol.listByRegion("Australia"); System.out.println(" --- Full Collection (sorted) ---"); bCol.listFullCollection(); System.out.println(" --- Remove b01 = \"Malachite\" ---"); bCol.remove(b01); bCol.listByRegion("North America"); System.out.println(" --- Remove \"Fairy...\" and \"Gaudy...\""); bCol.remove("Fairy Hairstreak"); bCol.remove("Gaudy Commodore"); bCol.listByRegion("Africa"); System.out.println(" --- Full Collection (sorted) ---"); bCol.listFullCollection(); }
represents an interface implementation and the - - - indicates the nesting of an Note that the inner class. Comparable
> Butterfly butterflyName : String butterfly Color: String -origin: String + Butterfly (butterflyName : String, bufferfly Color : String, origin: String) + getButterflyName(): String + getButterfly Color(): String + getorigin (): String + compare To (butterfly: Butterfly): int + toString 0 + compare To (b: Butterfly): int Butterfly Collection - regionCollections: List OriginCollection + Butterfly Collection (region : String...) + addButterfly (butterfly: Butterfly): vold + removeButterfly (butterfly : Butterfly):void + removeButterfly (butterflyName : String):void + listByRegion (region : String):void + listFull Collection ():void + merge All Regions(): List): List> OriginCollection -collection : List The regionCollections variable is a list of lists. It maintains the list of all the regional collections that were created (as per just above). A regional collection can be created in two ways: 1. Through a Butterfly Collection constructor call. This constructor must use a varargs String parameter and those String parameters are meant to represent the regions of origin for the butterflies. In other word, for each of the varags input, this constructor should construct a different instance of the inner) OriginCollection class (and add it to the regionCollections list). 2. Upon adding a new butterfly. If the region of origin for a new butterfly is already one that has its own collection (i.e., its own OriginCollection instance), then add to it. On the other hand, if the new butterfly is from a region of origin that is not already represented, a new OriginCollection instance must be created (with the proper region variable value, and this new instance must be added to the regionCollections list. The addButterfly() and removeButterfly() methods face the challenge of having to first identify (and possibly create the proper OriginCollection instance the butterfly should be added to or removed from! The mergeAllRegions() method creates an overall list of all the butterflies in the collection, irrespective of the their region of origin. It should be constructed upon demand from all the different OriginCollection instances. This overall collection MUST BE SORTED using the butterfly names. The listByRegion() and listFullCollection() methods are simply printing the regional or overall collections on the console. The (private) mergeLists() method, is a helper method for the mergeAllRegions() method. You are not required to create such a method if you do not wish to do so. The OriginCollection class is straightforward. Each instance of this inner class will contain the list of butterflies for a specific region (specified in the region field). This class should be declared PRIVATE (and so are all of its attributes and methods (see the UML), except iterator()). To test your code, use the main method code provided. Sample Run: Output: Butterfly [butterflyName-Malachite, butterflycolor-green, origin-North America) Butterfly (butterflyName=Red Rim, butterflyColor=red and black, origin=Central America) North America MALACHITE --- Africa --- WESTERN BLUE BEAUTY FAIRY HAIRSTREAK GAUDY COMMODORE --- Central America RED RIM ZEBRA LONGWING --- Australia --- --- Full Collection (sorted) .. FAIRY HAIRSTREAK GAUDY COMMODORE MALACHITE RED RIM WESTERN BLUE BEAUTY ZEBRA LONGHING ... Remove bei = "Malachite" ... --- Remove "Fairy..." and "Gaudy..." WESTERN BLUE BEAUTY --- Full Collection (sorted) --- RED RIM WESTERN BLUE BEAUTY ZEBRA LONGWING represents an interface implementation and the - - - indicates the nesting of an Note that the inner class. Comparable> Butterfly butterflyName : String butterfly Color: String -origin: String + Butterfly (butterflyName : String, bufferfly Color : String, origin: String) + getButterflyName(): String + getButterfly Color(): String + getorigin (): String + compare To (butterfly: Butterfly): int + toString 0 + compare To (b: Butterfly): int Butterfly Collection - regionCollections: List OriginCollection + Butterfly Collection (region : String...) + addButterfly (butterfly: Butterfly): vold + removeButterfly (butterfly : Butterfly):void + removeButterfly (butterflyName : String):void + listByRegion (region : String):void + listFull Collection ():void + merge All Regions(): List): List> OriginCollection -collection : List The regionCollections variable is a list of lists. It maintains the list of all the regional collections that were created (as per just above). A regional collection can be created in two ways: 1. Through a Butterfly Collection constructor call. This constructor must use a varargs String parameter and those String parameters are meant to represent the regions of origin for the butterflies. In other word, for each of the varags input, this constructor should construct a different instance of the inner) OriginCollection class (and add it to the regionCollections list). 2. Upon adding a new butterfly. If the region of origin for a new butterfly is already one that has its own collection (i.e., its own OriginCollection instance), then add to it. On the other hand, if the new butterfly is from a region of origin that is not already represented, a new OriginCollection instance must be created (with the proper region variable value, and this new instance must be added to the regionCollections list. The addButterfly() and removeButterfly() methods face the challenge of having to first identify (and possibly create the proper OriginCollection instance the butterfly should be added to or removed from! The mergeAllRegions() method creates an overall list of all the butterflies in the collection, irrespective of the their region of origin. It should be constructed upon demand from all the different OriginCollection instances. This overall collection MUST BE SORTED using the butterfly names. The listByRegion() and listFullCollection() methods are simply printing the regional or overall collections on the console. The (private) mergeLists() method, is a helper method for the mergeAllRegions() method. You are not required to create such a method if you do not wish to do so. The OriginCollection class is straightforward. Each instance of this inner class will contain the list of butterflies for a specific region (specified in the region field). This class should be declared PRIVATE (and so are all of its attributes and methods (see the UML), except iterator()). To test your code, use the main method code provided. Sample Run: Output: Butterfly [butterflyName-Malachite, butterflycolor-green, origin-North America) Butterfly (butterflyName=Red Rim, butterflyColor=red and black, origin=Central America) North America MALACHITE --- Africa --- WESTERN BLUE BEAUTY FAIRY HAIRSTREAK GAUDY COMMODORE --- Central America RED RIM ZEBRA LONGWING --- Australia --- --- Full Collection (sorted) .. FAIRY HAIRSTREAK GAUDY COMMODORE MALACHITE RED RIM WESTERN BLUE BEAUTY ZEBRA LONGHING ... Remove bei = "Malachite" ... --- Remove "Fairy..." and "Gaudy..." WESTERN BLUE BEAUTY --- Full Collection (sorted) --- RED RIM WESTERN BLUE BEAUTY ZEBRA LONGWING