Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

D Model.java 3 1 package code; 3 import java . util,HashMap;0 Homework 3, part 2

ID: 3599336 • Letter: D

Question

D Model.java 3 1 package code; 3 import java . util,HashMap;0 Homework 3, part 2 10 This homework is the second step to a geography-based application and will 11 give you practice with the following concepts: 12 13 14 15 16 17 In this homework you will work with a real-world data set that contains 18 approximately 48,080 city names, locations, and populations from around 19 the world. The data is in the WorldCitiesPop.csv file in the Data folder. 28 21 Each line of this file has the following structure: 22Country, City, AccentCity,Region, Population, Latitude, Longitude 23 24 THE City CLASS 25 26 You will need to keep all the data for a City together in one place. To do 27 this you define a City class that has an instance variable for the following 28 fields: 29 30 31 32 33Population store this as an int 34 35 36 7 Notice that AccentCity is missing you may safely ignore this field 38 39 The City class must have a constructor that accepts these values as Strings reading data from a file structuring data Filtering data Country store this as a String City store this as a String Region store this as a String Latitude - store this as a double Longitude store this as a double Country, City, Region, Population, Latitude, Longitude 1 *and initializes the above instance variables as expected 43 * Define also a simple ecessor. method for each of the instance variables 45 THE CityPair CLASS 7 Sometimes you need to work with pairs of cities rather than just individual 48*cities. The CityPair class has a constructor that takes two City objects and 49 stores each in its own instance variable

Explanation / Answer

City.java

package code;

public class City {

private String country;
        private String city;
        private String region;
        private int population;
        private double lattitude;
        private double longitude;
        public City(String country, String city, String region, int population, double lattitude, double longitude) {
                super();
                this.country = country;
                this.city = city;
                this.region = region;
                this.population = population;
                this.lattitude = lattitude;
                this.longitude = longitude;
        }
        public String getCountry() {
                return country;
        }
        public void setCountry(String country) {
                this.country = country;
        }
        public String getCity() {
                return city;
        }
        public void setCity(String city) {
                this.city = city;
        }
        public String getRegion() {
                return region;
        }
        public void setRegion(String region) {
                this.region = region;
        }
        public int getPopulation() {
                return population;
        }
        public void setPopulation(int population) {
                this.population = population;
        }
        public double getLattitude() {
                return lattitude;
        }
        public void setLattitude(double lattitude) {
                this.lattitude = lattitude;
        }
        public double getLongitude() {
                return longitude;
        }
        public void setLongitude(double longitude) {
                this.longitude = longitude;
        }
}
CityPair.java

package code;

public class CityPair {

        private City cityOne;
        private City cityTwo;
        public CityPair(City cityOne, City cityTwo) {
                super();
                this.cityOne = cityOne;
                this.cityTwo = cityTwo;
        }
        public City cityOne() {
                return cityOne;
        }
      
        public City cityTwo() {
                return cityTwo;
        }
}
Model.java

package code;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Model {

public ArrayList<City> readDataFromCSV(String fileName){
                ArrayList<City> cities = new ArrayList<City>();
                try {
                    BufferedReader in = new BufferedReader(new FileReader(fileName));
                    String str;
                    while ((str = in.readLine()) != null){
                      
                        String[] cityInfo = str.split(",");
//If AccentCity is added in the file, then add the indices accordingly in the constructor,as
//indices of String[] array will differ in that case
                        City city = new City(cityInfo[0], cityInfo[1], cityInfo[2], Integer.parseInt(cityInfo[3]), Double.parseDouble(cityInfo[4]), Double.parseDouble(cityInfo[5]));
                        cities.add(city);
                    }
                    in.close();
                } catch (IOException e) {
                }
                return cities;
        }
      
        public ArrayList<City> filterByCountry(String country, ArrayList<City> cities){
                ArrayList<City> citiesByCountry = new ArrayList<City>();
                for (City city : cities) {
                        if(city.getCountry().equalsIgnoreCase(country)){
                                citiesByCountry.add(city);
                        }
                      
                }
                return citiesByCountry;
        }
      
        public ArrayList<City> filterByRegion(String region, ArrayList<City> cities){
                ArrayList<City> citiesByRegion = new ArrayList<City>();
                for (City city : cities) {
                        if(city.getRegion().equalsIgnoreCase(region)){
                                citiesByRegion.add(city);
                        }
                      
                }
                return citiesByRegion;
        }
      
        public ArrayList<City> filterByMinimumPopulation(int minimumPopultion, ArrayList<City> cities){
                ArrayList<City> citiesByMinPopulation = new ArrayList<City>();
                for (City city : cities) {
                        if(city.getPopulation() >= minimumPopultion){
                                citiesByMinPopulation.add(city);
                        }
                }
                return citiesByMinPopulation;
                }
      
        public ArrayList<City> filterByMaximumPopulation(int maximumPopultion, ArrayList<City> cities){
                ArrayList<City> citiesByMaxPopulation = new ArrayList<City>();
                for (City city : cities) {
                        if(city.getPopulation() <= maximumPopultion){
                                citiesByMaxPopulation.add(city);
                        }
                }
                return citiesByMaxPopulation;
                }
      
        public int totalPopulation(ArrayList<City> cities){
                int totalPopulation = 0;
                for (City city : cities) {
                        totalPopulation = totalPopulation + city.getPopulation();
                }
                return totalPopulation;
              
        }
      
        public CityPair furthestApart(ArrayList<City> cities){
                double distance=0.0;
                CityPair cityPair = null;
                for (City city1 : cities) {
                        for (City city2 : cities) {
                                Point2D loc1 = new Point2D(city1.getLattitude(), city1.getLongitude());
                                Point2D loc2 = new Point2D(city2.getLattitude(), city2.getLongitude());
                                if(loc1.distance(loc2) > distance){
                                        distance = loc1.distance(loc2);
                                        cityPair = new CityPair(city1, city2);
                                      
                                }
                              
                        }
                      
                }
                return cityPair;
        }
      
        public CityPair closestTogether(ArrayList<City> cities){
                double distance=25000.0;        //Figure larger than Earth's Circumference, distance can not be more than this
                CityPair cityPair = null;
                for (City city1 : cities) {
                        for (City city2 : cities) {
                                //To avoid comparing a city with itself
                                if(city1.getLattitude() != city2.getLattitude() && city1.getLongitude() != city2.getLongitude()){
                                        Point2D loc1 = new Point2D(city1.getLattitude(), city1.getLongitude());
                                        Point2D loc2 = new Point2D(city2.getLattitude(), city2.getLongitude());
                                        if(loc1.distance(loc2) < distance){
                                                distance = loc1.distance(loc2);
                                                cityPair = new CityPair(city1, city2);
                                              
                                        }
                                }
                        }
                      
                }
                return cityPair;
        }
}
I have added only those methods which are not defined and which are required. You can add remaining methods which are already there and test them below in Testing.java

Testing.java

package code;

import java.util.ArrayList;

public class Testing {

public static void main(String[] args) {
                Model model = new Model();
                //Enter your file with absolute path
                ArrayList<City> cities = model.readDataFromCSV("C:/Users/LENOVO/Desktop/WorldCitiesPop.csv");
                ArrayList<City> result = new ArrayList<City>();
              
                System.out.println("Filtering Cities By Country..");
                result = model.filterByCountry("US", cities);
                for (City city : result) {
                        System.out.println("Country : " + city.getCountry() + " City: " + city.getCity());
                }
                System.out.println();
              
                System.out.println("Filtering Cities By Region..");
                result = model.filterByRegion("Northwest", cities);
                for (City city : result) {
                        System.out.println("Region : " + city.getRegion() + " City: " + city.getCity());
                }
                System.out.println();
              
                System.out.println("Filtering Cities By Minimum Population..");
                result = model.filterByMinimumPopulation(1000, cities);
                for (City city : result) {
                        System.out.println("Popultion : " + city.getPopulation() + " City: " + city.getCity());
                }
                System.out.println();
              
                System.out.println("Filtering Cities By Maximum Population..");
                result = model.filterByMaximumPopulation(1000, cities);
                for (City city : result) {
                        System.out.println("Popultion : " + city.getPopulation() + " City: " + city.getCity());
                }
                System.out.println();
              
                CityPair furthestCities = model.furthestApart(cities);
                System.out.println("Furthest cities in the List are " + furthestCities.cityOne().getCity() + " And " + furthestCities.cityTwo().getCity());
                System.out.println();
              
                CityPair closestCities = model.closestTogether(cities);
                System.out.println("Closest cities in the List are " + closestCities.cityOne().getCity() + " And " + closestCities.cityTwo().getCity());
                System.out.println();
              
                int totalPopulation = model.totalPopulation(cities);
                System.out.println("Total Popultion of the cities in the List is: " + totalPopulation);
        }
}