/* Tutorial 7/14/05 Conditionals, Lists, and Recursion Sample Problem Statement: You want to keep track of the theme parks that you have visited. You are especially interested in "aggregate" information (about all of them) such as how many "large" theme parks you have visited or how many parks owned by Six Flags or Disney you have visited. Useful information: Any parks over the size of 5 acres is considered "large". Designing Recursive (Inductively Defined) Data Structures: A CollectionOfThemeParks is either: (1) An EmptyCollectionOfThemeParks, or (2) A LargerCollectionOfThemeParks with a single ThemePark and a remaining CollectionOfThemeParks Concepts: (1) Recursive (Inductively Defined) Data Structures (2) Java abstract and inheritance (3) Conditionals (4) Aggregate Operations on Recursive Data Structures */ abstract class CollectionOfThemeParks { // To report how many "large" parks are in this CollectionOfThemeParks abstract int numberOfLargeParks(); // To report how many parks with a given owner (ownerName) are in this // CollectionOfThemeParks abstract int numberWithOwner(String ownerName); } class EmptyCollectionOfParks extends CollectionOfThemeParks { EmptyCollectionOfParks() { } // new EmptyCollectionOfParks().numberOfLargeParks() == 0 // To report how many "large" parks are in this EmptyCollectionOfParks int numberOfLargeParks() { return 0; } // new EmptyCollectionOfParks().numberWithOwner("SeaWorld") == 0 // To report how many parks with a given owner (ownerName) are in this // EmptyCollectionOfParks int numberWithOwner(String ownerName) { // ... ownerName ... return 0; } } class LargerCollectionOfParks extends CollectionOfThemeParks { ThemePark park; CollectionOfThemeParks rest; LargerCollectionOfParks(ThemePark park, CollectionOfThemeParks rest) { this.park = park; this.rest = rest; } // new LargerCollectionOfParks(new ThemePark(10, "SeaWorld"), // new EmptyCollectionOfParks()).numberOfLargerParks() == 1 // new LargerCollectionOfParks(new ThemePark(10, "SeaWorld"), // new LargerCollectionOfParks(new ThemePark(5, "Disney"), // new EmptyCollectionOfParks())) // .numberOfLargerParks() == 1 // new LargerCollectionOfParks(new ThemePark(10, "Disney"), // new LargerCollectionOfParks(new ThemePark(5, "Six Flags"), // new LargerCollectionOfParks(new ThemePark(4, "Busch Gardens"), // new LargerCollectionOfParks(new ThemePark(16, "Disney"), // new EmptyCollectionOfParks())))).numberOfLargeParks() == 2 // new LargerCollectionOfParks(new ThemePark(4, "Six Flags"), // new LargerCollectionOfParks(new ThemePark(5, "California Adventure"), // new LargerCollectionOfParks(new ThemePark(3, "Marine World"), // new EmptyCollectionOfParks())))).numberOfLargeParks() == 0 // To report how many parks in this LargerCollectionOfParks are "large" int numberOfLargeParks() { // ... this.park.ThemeParkMethod() ... this.rest.CollectionOfThemeParksMethod() ... if (this.park.isLargePark()) return 1 + this.rest.numberOfLargeParks(); else return this.rest.numberOfLargeParks(); } /* new LargerCollectionOfParks(new ThemePark(10, "Disney"), new LargerCollectionOfParks(new ThemePark(5, "Six Flags"), new LargerCollectionOfParks(new ThemePark(4, "Busch Gardens"), new LargerCollectionOfParks(new ThemePark(16, "Disney"), new EmptyCollectionOfParks())))).numberWithOwner("Disney") == 2 new LargerCollectionOfParks(new ThemePark(10, "Disney"), new LargerCollectionOfParks(new ThemePark(5, "Six Flags"), new LargerCollectionOfParks(new ThemePark(4, "Busch Gardens"), new LargerCollectionOfParks(new ThemePark(16, "Disney"), new EmptyCollectionOfParks())))).numberWithOwner("Six Flags") == 1 */ // To report the number of ThemeParks in this LargerCollection with a given owner int numberWithOwner(String ownerName) { // ... this.park.ThemeParkMethod() ... this.rest.CollectionOfThemeParksMethod() ... ownerName ... if (this.park.isOwnedBy(ownerName)) return 1 + this.rest.numberWithOwner(ownerName); else return this.rest.numberWithOwner(ownerName); } } // Given class ThemePark { int size; String owner; ThemePark(int size, String owner) { this.size = size; this.owner = owner; } // new ThemePark(5, "Marine World").isLargePark() == false // new ThemePark(6, "Six Flags").isLargePark() == true // To determine whether this ThemePark is "large" or not boolean isLargePark() { // ... this.size ... this.owner.StringMethod() ... return this.size > 5; } // new ThemePark(10, "Disney").isOwnedBy("Disney") == true // new ThemePark(10, "Six Flags").isOwnedBy("Disney") == false // To determine whether this ThemePark is owned by a given owner boolean isOwnedBy(String ownerName) { // ... this.size ... this.owner.StringMethod() ... ownerName ... return ownerName.equals(this.owner); } }