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

Create a class called ProvinceTerritory. It contains two instance variables: a S

ID: 3576142 • Letter: C

Question

Create a class called ProvinceTerritory. It contains two instance variables: a String name and an int population. Create accessors and mutators too. The constructor takes the name and population as parameters and sets them as long as the name isn’t null and the population isn’t negative.


Create a class called Canada. It contains an ArrayList of ProvinceTerritory references:

Name ---------------------------------- Population
Ontario -------------------------------- 12,851,821
Quebec -------------------------------- 7,903,001
British Columbia --------------------- 4,400,057
Alberta --------------------------------- 3,645,257
Manitoba ------------------------------ 1,208,268
Saskatchewan ------------------------ 1,033,381
Nova Scotia --------------------------- 921,727
New Brunswick ---------------------- 751,171
Newfoundland and Labrador ----- 514,536
Prince Edward Island --------------- 140,204
Northwest Territories --------------- 41,462
Yukon ----------------------------------- 33,897
Nunavut -------------------------------- 31,906
Total: Canada ------------------------- 33,476,688

Populate the ArrayList inside the Canada constructor. The following code will be useful:

provinces.add(new ProvinceTerritory("british columbia", 4400057));
provinces.add(new ProvinceTerritory("alberta", 3645257));
etc...

Please use a for -each loop in the thoes methods below


Create a method called public int getTotalPopulation() which uses a for-each loop to add up all of the individual populations to calculate Canada’s total population.


Create a method called public String getLowestPopulation() which uses a for-each loop to determine and return the name of the province/territory that has the lowest population.


Create a method called public ProvinceTerritory getHighestPopulation() which uses a for-each loop to determine and return the name of the province/territory that has the highest population.


Create a method called public int getPopulation(String province) which returns the population of the province (the parameter); if there is no such province, return a constant called NO_SUCH_PROVINCE, which is an int set to -1.


Create a method called public boolean isProvinceInCanada(String name) which returns true if there is a province/territory in Canada with the given name (the parameter); otherwise returns false.


Create a method called public ProvinceTerritory[] getProvincesWhoseNameContains(String substring)which returns an array of the names of all provinces/territories whose name contains substring (the parameter). Hint: use the String class’s contains() method.


public ArrayList<ProvinceTerritory>
   getMoreProvincesWhoseNameContains(String substring)

which returns an ArrayList of the names of all provinces/territories whose name contains substring (the parameter). Hint: use the String class’s contains() method.


Create a method called public ArrayList<String> getProvincesWhoseNameStartsWith(char letter) which returns an array of the names of all provinces/territories whose name starts with letter (the parameter). Hint: use the String class’s startsWith() method or use the String class’s charAt(0) method.

Explanation / Answer

// ProvinceTerritory.java
public class ProvinceTerritory
{
   int population;
   String name;

   public ProvinceTerritory(int p, String n)
   {
       population = p;
       name = n;
   }

   public int getPopulation()
   {
       return population;
   }
  
   public int getProvince()
   {
       return name;
   }

}


// Canada.java
public class Canada
{
   ArrayList<ProvinceTerritory> provinces = new ArrayList<ProvinceTerritory>();//creating new generic arraylist

   public Canada()
   {
       provinces.add(new ProvinceTerritory("Ontario", 12851821));
       provinces.add(new ProvinceTerritory("Quebec", 7903001));
       provinces.add(new ProvinceTerritory("British Columbia", 4400057));
       provinces.add(new ProvinceTerritory("Alberta", 3645257));
       provinces.add(new ProvinceTerritory("Manitoba", 1208268));
       provinces.add(new ProvinceTerritory("Saskatchewan", 1033381));
       provinces.add(new ProvinceTerritory("Nova Scotia", 921727));
       provinces.add(new ProvinceTerritory("New Brunswick", 751171));
       provinces.add(new ProvinceTerritory("Newfoundland and Labrador", 514536));
       provinces.add(new ProvinceTerritory("Prince Edward Island", 140204));
       provinces.add(new ProvinceTerritory("Northwest Territories", 41462));
       provinces.add(new ProvinceTerritory("Yukon", 31906));
       provinces.add(new ProvinceTerritory("Nunavut", 33476688));
   }

   public int getTotalPopulation()
   {
       int total;
       for (int i = 0; i < provinces.length; i++ )
       {
           total = total + provinces[i].getPopulation();
       }

       return total;
   }

   public String getLowestPopulation()
   {
       int index = 0;
       int lowest = provinces[0].getPopulation();
       for (int i = 1; i < provinces.length; i++ )
       {
           if(provinces[i].getPopulation() < lowest)
           {
               index = i;
               lowest = provinces[i].getPopulation();
           }
       }

       return provinces[index].getProvince();
   }

   public ProvinceTerritory getHighestPopulation()
   {
      
       int index = 0;
       int highest = provinces[0].getPopulation();
       for (int i = 1; i < provinces.length; i++ )
       {
           if(provinces[i].getPopulation() > highest)
           {
               index = i;
               highest = provinces[i].getPopulation();
           }
       }

       ProvinceTerritory p = new ProvinceTerritory(provinces[index].getProvince(), provinces[i].getPopulation());
       return p;
   }

   public int getPopulation(String province)
   {
       for (int i = 0; i < provinces.length ;i++ )
       {
           if(provinces[i].getProvince().equals(province))
               return    provinces[i].getPopulation();
       }

       return -1;
   }

   public boolean isProvinceInCanada(String name)
   {
       for (int i = 0; i < provinces.length ;i++ )
       {
           if(provinces[i].getProvince().equals(province))
               return    true;
       }

       return false;
   }


   public ProvinceTerritory[] getProvincesWhoseNameContains(String substring)
   {
int i = 0;
int index = 0;
  
while (i < provinces.length)
{
if (provinces[i].getProvince().contains(substring))
{
index++;
}
i++;
}
  
ProvinceTerritory[] criteria = new ProvinceTerritory[index];
int j = 0;
  
while (i < provinces.length)
{
if(provinces[i].getProvince().contains(substring))
{
criteria[j] = new ProvinceTerritory(provinces[i].getProvince(), provinces[i].getPopulation());
j++;
}
i++;
}
return criteria;
}

public ArrayList<ProvinceTerritory> getMoreProvincesWhoseNameContains(String substring)
   {
       int i = 0;
ArrayList<ProvinceTerritory> criteria = new ArrayList<ProvinceTerritory>();
int j = 0;
  
while (i < provinces.length)
{
if(provinces[i].getProvince().contains(substring))
{
criteria[j].add(ProvinceTerritory(provinces[i].getProvince(), provinces[i].getPopulation()));
j++;
}
i++;
}
return criteria;
   }

   public ArrayList<String> getProvincesWhoseNameStartsWith(char letter)
   {
       int i = 0;
ArrayList<ProvinceTerritory> criteria = new ArrayList<ProvinceTerritory>();
int j = 0;
  
while (i < provinces.length)
{
   String charToString = Character.toString(letter);
if(provinces[i].getProvince().startsWith(charToString))
{
criteria[j].add(ProvinceTerritory(provinces[i].getProvince(), provinces[i].getPopulation()));
j++;
}
i++;
}
return criteria;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote