Create a class called Canada It contains an array of 13 Stri

Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of 13 ints - the populations of each of these:

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 two arrays inside the Country constructor. The following code will be useful:

public static final int BC = 0;
public static final int AB = 1;
etc...

provinces[BC] = \"british columbia\";
provinces[AB] = \"alberta\";
etc...

populations[BC] = 4400057;
populations[AB] = 3645257;
etc...

Create a method called public int getTotalPopulation() which uses a 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 loop to determine and return the name of the province/territory that has the lowest 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 String[] getProvincesWithPopulationBetween(int min, int max) which returns an array of the names of all provinces/territories that have a population between min and max (inclusive).

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 String[] getProvincesWhoseNameContains(String substring) which returns an array of the names of all provinces/territories whose name contains substring (the parameter).

Create a method called public String[] getProvincesWhoseNameStartsWith(char letter) which returns an array of the names of all provinces/territories whose name starts with letter (the parameter).

Solution

Here we are intiating the two arrays in the constructor. So when we crate an object for the class, the constructors will be instantiated.

Below is the code.

package com.chegg.canada;

import java.util.Scanner;

public class Canada {
   public static String[] provinces=new String[13];
   public static int[] populations=new int[13];
  
   public static final int OT = 0;
   public static final int QB = 1;
   public static final int BC = 2;
   public static final int AB = 3;
   public static final int MT = 4;
   public static final int SC = 5;
   public static final int NS = 6;
   public static final int NB = 7;
   public static final int NL = 8;
   public static final int PD = 9;
   public static final int NT = 10;
   public static final int YK = 11;
   public static final int NV = 12;
  
   public Canada(String[] provinces,int[] populations){
      
       provinces[OT] = \"Ontario\";
       provinces[QB] = \"Quebec\";
       provinces[BC] = \"British Columbia\";
       provinces[AB] = \"Alberta\";
       provinces[MT] = \"Manitoba\";
       provinces[SC] = \"Saskatchewan\";
       provinces[NS] = \"Nova Scotia\";
       provinces[NB] = \"New Brunswick\";
       provinces[NL] = \"Newfoundland and Labrador\";
       provinces[PD] = \"Prince Edward Island\";
       provinces[NT] = \"Northwest Territories\";
       provinces[YK] = \"Yukon\";
       provinces[NV] = \"Nunavut\";
      
       populations[OT] = 12851821;
       populations[QB] = 7903001;
       populations[BC] = 4400057;
       populations[AB] = 3645257;
       populations[MT] = 1208268;
       populations[SC] = 1033381;
       populations[NS] = 921727;
       populations[NB] = 751171;
       populations[NL] = 514536;
       populations[PD] = 140204;
       populations[NT] = 41462;
       populations[YK] = 33897;
       populations[NV] = 31906;
      
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
/*       String[] provinces={\"Ontario\",\"Quebec\",\"British Columbia\",\"Alberta\",\"Manitoba\",
               \"Saskatchewan\",\"Nova Scotia\",\"New Brunswick\",\"Newfoundland and Labrador\",
               \"Prince Edward Island\",\"Northwest Territories\",\"Yukon\",\"Nunavut\"};
       int[] populations={12851821,7903001,4400057,3645257,1208268,1033381,921727,751171,514536,140204,41462,33897,31906};
       */
      
              
       Canada canada=new Canada(provinces,populations);
      
       int min_value=0,max_value=0;
      
      
      
       int total_population=getTotalPopulation();
       System.out.println(\"Total population in canada is \"+total_population);
       /*----------------------------------------------------------------------------------------------------*/
      
       String place=getLowestPopulation();
       System.out.println(\"The province/territory that has lower population is \"+place);
      
       /*----------------------------------------------------------------------------------------------------*/
      
       Scanner scn=new Scanner(System.in);
       System.out.println(\"Enter the province/territory whose population you want to know \");
       String user_place=scn.nextLine();
       int place_population=getPopulation(user_place);
       System.out.println(\"The population of the province/territory you entered is \"+place_population);
      
       /*----------------------------------------------------------------------------------------------------*/
      
       System.out.println(\"Enter the manimum and maximum values to get all the province/territory names \"
               + \"whose populations are in between these two values\");
       System.out.println(\"Min Value\");
       max_value=scn.nextInt();
       System.out.println(\"Max value\");
       min_value=scn.nextInt();
       String[] user_places=getProvincesWithPopulationBetween(min_value,max_value);
       System.out.println(\"All the places whose populations drop between min and maxvalue(inclusive) are \");
       for(int j=0;j<user_places.length;j++){
           System.out.println(user_places[j]);
       }
      
       /*----------------------------------------------------------------------------------------------------*/
       System.out.println(\"Enter the name of province/territory to check whether it is belongs to Canada or not\");
       String place_check=scn.nextLine();
       boolean flag=isProvinceInCanada(place_check);
      
       /*----------------------------------------------------------------------------------------------------*/
      
       System.out.println(\"Enter the substring to get all province/territory names which contains that substring\");
       String sub=scn.nextLine();
       String[] sub_places=getProvincesWhoseNameContains(sub);
       System.out.println(\"All the province/territory names which contains the given substring are \");
       for(int k=0;k<sub_places.length;k++){
           System.out.println(sub_places[k]);
       }
      
       /*----------------------------------------------------------------------------------------------------*/
      
       System.out.println(\"Enter any character to get all province/territory names which start with that character\");
       char ch=scn.next().charAt(0);
       String[] char_places=getProvincesWhoseNameStartsWith(ch);
       System.out.println(\"All the province/territory names which starts with the given character are \");
       for(int l=0;l<char_places.length;l++){
           System.out.println(char_places[l]);
       }
      
   }
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
   public static int getTotalPopulation(){
       int total_population=0;
       for(int i=0;i<populations.length;i++){
           total_population=total_population+populations[i];
       }      
       return total_population;
   }
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
   public static String getLowestPopulation(){
       String place=null;
       int min=populations[0];
       int index=0;
      
       for(int i=0;i<populations.length;i++){
           if(populations[i]<min){
               min=populations[i];
               index=i;
              
           }
       }
      
       place=provinces[index];
      
       return place;
   }

  
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
   public static int getPopulation(String place){
       int place_population=0;
       int index=0;
       int NO_SUCH_PROVINCE=-1;
       for(int i=0;i<provinces.length;i++){
           if(place.equalsIgnoreCase(provinces[i])){
               index=i;
               break;
           }
           else
               index=NO_SUCH_PROVINCE;
      
       }
       place_population=populations[index];
       return place_population;
   }
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
   public static String[] getProvincesWithPopulationBetween(int min,int max){
       String[] user_places=new String[13];
       int index=0;
       for(int i=0;i<populations.length;i++){
           if((min<populations[i]) && (populations[i]<=max)){
               user_places[index]=provinces[i];
               index++;              
           }
       }
      
       return user_places;
      
   }
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
   public static boolean isProvinceInCanada(String user_place){
       boolean flag=true;
       for(int i=0;i<provinces.length;i++){
           if(provinces[i].equalsIgnoreCase(user_place)){
               flag=true;
               break;
           }
           else
               flag=false;
       }
       return flag;
   }
  
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
  
   public static String[] getProvincesWhoseNameContains(String sub){
      
       String[] sub_places=new String[13];
       int index=0;
       for(int i=0;i<provinces.length;i++){
           if(provinces[i].toLowerCase().contains(sub.toLowerCase())){
               sub_places[index]=provinces[i];
               index++;              
           }
       }
      
       return sub_places;
      
   }
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
   public static String[] getProvincesWhoseNameStartsWith(char ch){
       String[] char_places=new String[13];
       int index=0;
       for(int i=0;i<provinces.length;i++){
           if(ch==provinces[i].charAt(0)){
               char_places[index]=provinces[i];
               index++;              
           }
       }
      
       return char_places;
      
      
   }
   /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
}

Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of
Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of
Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of
Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of
Create a class called Canada. It contains an array of 13 Strings: the names of Canada’s ten provinces and three territories. It also contains another array - of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site