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

Suppose that we need to develop a simple class called Country for use in a geogr

ID: 665707 • Letter: S

Question

Suppose that we need to develop a simple class called Country for use in a geographical database system. The class will need variables to hold (a) the name of a country, which contains mainly alpha- betic characters, (b) its capital city as alphabetic characters, (c) area in square km as a double, and (d) population as an integer. The only constructor for this class requires four arguments, the name, capital, population, and area. The methods needed are getName C) which returns the name of the country, get Capital C) which returns the name of the capital city, getArea C) which returns the area. in square km, getPopulation which returns the population size, and setPopulation popSize which changes the size of population to the value popSize. (a) Write out a skeleton of the Country class, clearly showing all variable declarations and method definitions. Their respective types, signatures, and arguments must be included. You are NOT required to include any code in the methods. (2 marks) (b) In an existing Geography class the following method is required: public static String largest Country (Country country List) It should return the name of the country that has the largest land area. Write out the method in detail. (5 marks) (c) Suppose that we are also interested in finding the country that is most densely populated (i.e., having the most number of people per square km). Write a method public static String mostDenselyPopulatedCountry(Country Cl country List) that returns the name of the most densely populated country.

Explanation / Answer

class Country {
String name;
long population;
double area;
Country(String name, long population, double area) {
this.name = name;
this.population = population;
this.area = area;
}
double density () {
if (area > 0)
return population/area;
else return 0;
}
public static void main(String[] args) {
int numberOfCountries = 10;
Country[] countries = new Country[numberOfCountries];

double lArea = -1; int lAreaIndex = -1;
long lPop = -1; int lPopIndex = -1;
double lDensity = -1; int lDensityIndex = -1;

for (int i=0; i < countries.length; i++) {
if (lArea < countries[i].area) {
lArea = countries[i].area;
lAreaIndex = i;
}
if (lPop < countries[i].population) {
lPop = countries[i].population;
lPopIndex = i;
}
if (lDensity < countries[i].density()) {
lDensity = countries[i].density();
lDensityIndex = i;
}
}
System.out.println(
"Largest area: " + country[lAreaIndex] +
"Largest population: " + country[lPopulationIndex] +
"Largest density: " + country[lDensityIndex]   
);
}
String toString() { // comes in handy in the println above
return " Country name: " + name +
" Population: " + population +
" Area: " + area +
" Density: " + density()
}
}

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