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
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);
}
}
import java.util.*;
public class Stadium
{
private String m_name;
private String m_city;
private int m_capacity;
private boolean m_kidzone;
public Stadium(String name, String city, int capacity, boolean kidzone)
{
m_name = name;
m_city = city;
m_capacity = capacity;
m_kidzone = kidzone;
}
public String getName()
{ return m_name; }
public String getCity()
{ return m_city; }
public int getCapacity()
{ return m_capacity; }
public boolean getKidzone()
{ return m_kidzone; }
@Override
public String toString()
{
String kz = getKidzone() ? "has a" : "does not have a";
return String.format("%s, located in %s, %s kidzone.", getName(), getCity(), kz);
}
static class ByKidZoneCityName implements Comparator<Stadium>
{
// your code goes here...
}
}
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.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images