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

Write the complete implementation of the class Region. This class repre- sents a

ID: 3730773 • Letter: W

Question

Write the complete implementation of the class Region. This class repre- sents a collection of towns (you can use an ArrayList) and is a subclass of PopulationUnit, defined above. The population of a region is the combined population of its towns, and the area of a region is the combined area of its towns. The constructor to your class should take a single argument, name, which is used to initialize the superclass The class should contain a method add that takes a town instance and add it to the region if it does not already contain it. If the region already contains the town, do nothing. This method should not return a value. The ordering of the towns is not important The class should implement toString and equals. The string format of a region should be the name of the region followed by a comma-separated list of the towns. For example: MilwaukeeCounty: Milwaukee (559164/98.6), Greendale (14340/5.6) Two regions are equal if their names are equal and they contain the same towns, but not necessarily in the same order (Hint: Two lists have the same content if they are the same size and all the elements of the first list are in the second list.) class Region extends PopulationUnit f // add fields Region (String name) C void add (Town town) f public String toString) ( public boolean equals (Object that) (

Explanation / Answer

Hi, You have not posted PopulationUnit and Town class, so I can not test.

import java.util.ArrayList;

public class Region extends PopulationUnit{

  

   private ArrayList<Town> list;

  

   public Region() {

       list = new ArrayList<>();

   }

  

   void add(Town town) {

      

       if(!list.contains(town))

           list.add(town);

   }

  

   public String toString() {

      

       String resullt = "";

       for(Town t : list) {

           resullt = resullt + t.toString()+" ";

       }

      

       return result;

   }

  

   public boolean equals(Object obj) {

       if(obj instanceof Region) {

          

           Region oth = (Region)obj;

          

           if(list.size() != oth.list.size())

               return false;

          

           for(Town t1 : oth.list) {

               if(!list.contains(t1))

                   return false;

           }

       }

      

      

       return false;

   }

  

  

}