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: 3576052 • 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
Note: If you don't use a for -each loop you will not be rated.


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

public class ProvinceTerritory {

   private String name;
   private int population;

   public ProvinceTerritory() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param name
   * @param population
   */
   public ProvinceTerritory(String name, int population) {
       this.name = name;
       this.population = population;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @return the population
   */
   public int getPopulation() {
       return population;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @param population
   * the population to set
   */
   public void setPopulation(int population) {
       this.population = population;
   }

}

import java.util.ArrayList;
import java.util.List;

public class Canada {

   List<ProvinceTerritory> provinces = new ArrayList<ProvinceTerritory>();

   public Canada() {
       // TODO Auto-generated constructor stub
   }

   /**
   * calculate Canada’s total population
   *
   * @return
   */
   public int getTotalPopulation() {

       int totalPopulation = 0;
       for (ProvinceTerritory territory : provinces) {
           totalPopulation += territory.getPopulation();
       }

       return totalPopulation;
   }

   /**
   * return the name of the province/territory that has the lowest population
   *
   * @return
   */
   public String getLowestPopulation() {

       int minPopulation = Integer.MIN_VALUE;
       String minName = "";
       for (ProvinceTerritory territory : provinces) {
           if (minPopulation > territory.getPopulation()) {
               minPopulation = territory.getPopulation();
               minName = territory.getName();

           }

       }

       return minName;
   }

   /**
   * determine and return the name of the province/territory that has the
   * highest population.
   *
   * @return
   */
   public ProvinceTerritory getHighestPopulation() {

       int maxPopulation = Integer.MAX_VALUE;
       String maxName = "";
       for (ProvinceTerritory territory : provinces) {
           if (maxPopulation < territory.getPopulation()) {
               maxPopulation = territory.getPopulation();
               maxName = territory.getName();

           }

       }

       return new ProvinceTerritory(maxName, maxPopulation);
   }

   /**
   * 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.
   *
   * @param province
   * @return
   */
   public int getPopulation(String province) {
       final int NO_SUCH_PROVINCE = -1;
       for (ProvinceTerritory territory : provinces) {
           if (territory.getName().equals(province)) {
               return territory.getPopulation();

           }

       }

       return NO_SUCH_PROVINCE;

   }

   /**
   * returns true if there is a province/territory in Canada with the given
   * name (the parameter); otherwise returns false.
   *
   * @param name
   * @return
   */
   public boolean isProvinceInCanada(String name) {

       for (ProvinceTerritory territory : provinces) {
           if (territory.getName().equals(name)) {
               return true;

           }

       }
       return false;

   }

   /**
   * returns an array of the names of all provinces/territories whose name
   * contains substring (the parameter).
   *
   * @param substring
   * @return
   */
   public ProvinceTerritory[] getProvincesWhoseNameContains(String substring) {

       ProvinceTerritory[] territories = new ProvinceTerritory[provinces
               .size()];
       int count = 0;
       for (ProvinceTerritory territory : provinces) {
           if (territory.getName().contains(substring)) {
               territories[count++] = territory;

           }

       }
       return territories;
   }

   /**
   * returns an ArrayList of the names of all provinces/territories whose name
   * contains substring (the parameter).
   *
   * @param substring
   * @return
   */
   public ArrayList<ProvinceTerritory> getMoreProvincesWhoseNameContains(
           String substring) {
       ArrayList<ProvinceTerritory> territories = new ArrayList<ProvinceTerritory>();
       for (ProvinceTerritory territory : provinces) {
           if (territory.getName().contains(substring)) {
               territories.add(territory);

           }

       }
       return territories;

   }

   /**
   * returns an array of the names of all provinces/territories whose name
   * starts with letter (the parameter)
   *
   * @param letter
   * @return
   */
   public ArrayList<String> getProvincesWhoseNameStartsWith(char letter) {

       ArrayList<String> territories = new ArrayList<String>();
       for (ProvinceTerritory territory : provinces) {
           if (territory.getName().charAt(0) == letter) {
               territories.add(territory.getName());

           }

       }
       return territories;
   }
}

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