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

Create a whole new class called Canada. It consists of an array of size 13. Each

ID: 3775881 • Letter: C

Question

Create a whole new class called Canada. It consists of an array of size 13. Each element in the array is also an array: an array of three Strings:

1. The name of a province or territory
2. The name of the capital city of the province or territory
3. The name of the largest city by population of the province or territory

Provice -------------- Capital -------------- Largest City (by population)
Ontario -------------- Toronto ------------- Toronto
Quebec -------------- Quebec City ------- Montreal
Nova Scotia --------- Halifax ------------- Halifax
New Brunswick ---- Fredericton -------- Saint John
Manitoba ----------- Winnipeg ---------- Winnipeg
British Columbia -- Victoria ------------ Vancouver
Prince Edward ----- Charlottetown ---- Charlottetown
Island
Saskatchewan ----- Regina -------------- Saskatoon
Alberta -------------- Edmonton ---------- Calgary
Newfoundland ----- St.John's ------------ St.Jon's
and Labrador

Populate the array inside the Country constructor. The following code will be useful:

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

public static final int NAME_OF_PROVINCE = 0;
public static final int NAME_OF_CAPITAL_CITY = 1;
public static final int NAME_OF_BIGGEST_CITY = 2;

provinces[BC] = {"british columbia", "victoria", "vancouver"};
provinces[AB] = {"alberta", "edmonton", "calgary"}
etc...


Create a method called public String getCapitalCityOf(String province) which returns the name of the capital city of the province (the parameter); if there is no such province, return null.


Create a method called public String getLargestCityOf(String province) which returns the name of the largest city of the province (the parameter); if there is no such province, return null.


Create a method called public String getProvinceWhoseCapitalIs(String city) which returns the name of the province whose capital city is the city (the parameter); if there is no such city, return null.


Create a method called public void displayAllProvinceInfo() which prints out all the data from the two-dimensional array in exactly the following format:

The capital city of BRITISH COLUMBIA is Victoria, but the largest city is Vancouver.
OR
The capital city of NEWFOUNDLAND AND LABRADOR is St. John’s, and it is also the largest city.

Explanation / Answer

SOURCE CODE:

public class Canada {

/*

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

public static final int NAME_OF_PROVINCE = 0;
public static final int NAME_OF_CAPITAL_CITY = 1;
public static final int NAME_OF_BIGGEST_CITY = 2;

provinces[BC] = {"british columbia", "victoria", "vancouver"};

//This type of array initialization is not supported in Java

//And it has been ommitted purposfully

provinces[AB] = {"alberta", "edmonton", "calgary"}

//This type of array initialization is not supported in Java

//And it has been ommitted purposfully
etc...

*/

public static final int NAME_OF_PROVINCE = 0;
public static final int NAME_OF_CAPITAL_CITY = 1;
public static final int NAME_OF_BIGGEST_CITY = 2;
public String[][] province={
{"british columbia", "victoria", "vancouver"},
{"alberta", "edmonton", "calgary"},
{"Ontario","Toronto","Toronto"},
{"Quebec","Quebec City","Montrea"},
{"Nova Scotia","Halifax","Halifax"},
{"New Brunswick","Fredericton","Saint John"},
{"Manitoba","Winnipeg","Winnipeg"},
{"Prince Edward Island","Charlottetown","Charlottetownx"},
{"Saskatchewan","Regina","Saskatoon"},
{"Newfoundlandand Labrador","St.John's","St.John's"}};

public Canada()
{
  
}
  

public Canada(String[][] province) {
this.province = province;
}

public String getCapitalCityOf(String province)
{
String res=null;
for(int i=0;i<13;i++)
{
if(this.province[i][0].equals(province))
{
res=this.province[i][1];
}
}
return res;
}

public String getLargestCityOf(String province)
{
String res=null;
for(int i=0;i<13;i++)
{
if(this.province[i][0].equals(province))
{
res=this.province[i][2];
}
}
return res;
  
}

public String getProvinceWhoseCapitalIs(String city)
{
String res=null;
for(int i=0;i<13;i++)
{
if((this.province[i][1]).equals(city))
{
res=this.province[i][0];
}
}
return res;
}

public void displayAllProvinceInfo()
{
for(int i=0;i<this.province.length;i++)
{
if((this.province[i][1]).equals(this.province[i][2]))
{
System.out.println("The capital city of "+this.province[i][0]+" is "+this.province[i][1]+", and it is also the largest city.");
}
else
{
System.out.println("The capital city of "+this.province[i][0]+" is "+this.province[i][1]+", but the largest city is "+this.province[i][2]+".");
}
}
}
}

-----------------------------------------------------------------------------------------------------------------


public class Driver {
public static void main(String[] args){
Canada c = new Canada();
c.displayAllProvinceInfo();
}
}

-------------------------------------------------------------------------------------------------

OUTPUT:

The capital city of british columbia is victoria, but the largest city is vancouver.
The capital city of alberta is edmonton, but the largest city is calgary.
The capital city of Ontario is Toronto, and it is also the largest city.
The capital city of Quebec is Quebec City, but the largest city is Montrea.
The capital city of Nova Scotia is Halifax, and it is also the largest city.
The capital city of New Brunswick is Fredericton, but the largest city is Saint John.
The capital city of Manitoba is Winnipeg, and it is also the largest city.
The capital city of Prince Edward Island is Charlottetown, but the largest city is Charlottetownx.
The capital city of Saskatchewan is Regina, but the largest city is Saskatoon.
The capital city of Newfoundlandand Labrador is St.John's, and it is also the largest city.

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