Complete the Stadium class so that it has an inner class singleton Comparator (sorting strategy) called ByKidZoneCityName that sorts using the following rules: The primary sort field should be if the stadium has a "kid zone" playground. Stadiums that have a kid zone should be listed before stadiums that do not have a kid zone. If two stadiums have the same value for the kid zone, the city (in ascending alphabetical order) should be used to determine the order. If two stadiums have the same value for the kid zone and the city, the stadium name (in ascending alphabetical order) should be used to determine the order. When you are done, your program should produce the following output: Coors Field, located in Denver, has a kidzone. Angels Stadium, located in Los Angeles, has a kidzone. Dodgers Stadium, located in Los Angeles, has a kidzone. PNC Park, located in Pittsburgh, has a kidzone. AT&T Park, located in San Francisco, has a kidzone. Citi Field, located in New York, does not have a kidzone. Yankee Stadium, located in New York, does not have a kidzone. Citizens Bank Park, located in Philadelphia, does not have a kidzone. Notice that stadiums with a kid zone are shown before stadiums without a kid zone. This is because kid zone is the primary sort field. However, for stadiums with the same value for kid zone, the city name is used to break the tie, For example, Coors Field is listed before Angels Stadium because Denver comes before Los Angeles (alphabetically). Finally, for stadiums with the same value for kid zone and city name, the stadium name is used as the tie breaker. For example, City Field is listed before Yankee stadium because Citi Field comes before Yankees Stadium (alphabetically). import java.util.*; public class Main { public static void main(String[] args) { Main m = new Main(); m.go(); } private void go() { List parks = new ArrayList(); parks.add(new Stadium("PNC Park", "Pittsburgh", 38362, true)); parks.add(new Stadium("Dodgers Stadium", "Los Angeles", 56000, true)); parks.add(new Stadium("Citizens Bank Park", "Philadelphia", 43035, false)); parks.add(new Stadium("Coors Field", "Denver", 50398, true)); parks.add(new Stadium("Yankee Stadium", "New York", 54251, false)); parks.add(new Stadium("AT&T Park", "San Francisco", 41915, true)); parks.add(new Stadium("Citi Field", "New York", 41922, false)); parks.add(new Stadium("Angels Stadium", "Los Angeles", 45050, true)); Collections.sort(parks, Stadium.ByKidZoneCityName.getInstance()); for (Stadium s : parks) System.out.println(s); } }
Complete the Stadium class so that it has an inner class singleton Comparator (sorting strategy) called ByKidZoneCityName that sorts using the following rules:
- The primary sort field should be if the stadium has a "kid zone" playground. Stadiums that have a kid zone should be listed before stadiums that do not have a kid zone.
- If two stadiums have the same value for the kid zone, the city (in ascending alphabetical order) should be used to determine the order.
- If two stadiums have the same value for the kid zone and the city, the stadium name (in ascending alphabetical order) should be used to determine the order.
When you are done, your program should produce the following output:
Coors Field, located in Denver, has a kidzone.
Angels Stadium, located in Los Angeles, has a kidzone.
Dodgers Stadium, located in Los Angeles, has a kidzone.
PNC Park, located in Pittsburgh, has a kidzone.
AT&T Park, located in San Francisco, has a kidzone.
Citi Field, located in New York, does not have a kidzone.
Yankee Stadium, located in New York, does not have a kidzone.
Citizens Bank Park, located in Philadelphia, does not have a kidzone.
Notice that stadiums with a kid zone are shown before stadiums without a kid zone. This is because kid zone is the primary sort field. However, for stadiums with the same value for kid zone, the city name is used to break the tie, For example, Coors Field is listed before Angels Stadium because Denver comes before Los Angeles (alphabetically). Finally, for stadiums with the same value for kid zone and city name, the stadium name is used as the tie breaker. For example, City Field is listed before Yankee stadium because Citi Field comes before Yankees Stadium (alphabetically).
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Main m = new Main();
m.go();
}
private void go()
{
List<Stadium> parks = new ArrayList<Stadium>();
parks.add(new Stadium("PNC Park", "Pittsburgh", 38362, true));
parks.add(new Stadium("Dodgers Stadium", "Los Angeles", 56000, true));
parks.add(new Stadium("Citizens Bank Park", "Philadelphia", 43035, false));
parks.add(new Stadium("Coors Field", "Denver", 50398, true));
parks.add(new Stadium("Yankee Stadium", "New York", 54251, false));
parks.add(new Stadium("AT&T Park", "San Francisco", 41915, true));
parks.add(new Stadium("Citi Field", "New York", 41922, false));
parks.add(new Stadium("Angels Stadium", "Los Angeles", 45050, true));
Collections.sort(parks, Stadium.ByKidZoneCityName.getInstance());
for (Stadium s : parks)
System.out.println(s);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 3 images