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

You’ve been given the task to display certain population data for the United Sta

ID: 3754047 • Letter: Y

Question

You’ve been given the task to display certain population data for the United States, Germany, and Argentina. You will prompt the user for US data. For Germany and Argentina, you will send the specified data to the class via overloaded constructor. Create the following in your program: A class named Population that has the following data fields with appropriate modifiers and data types: String variable for name of country. Integer variables for number of males and for number of females. Double variable for number of square miles Integer variable for number of states. The Population class should also contain: Default constructor (no default values). Overloaded constructor that accepts name, males, females, square miles, and states as arguments setters & getters for all data fields. value-returning instance method that calculates total population (males + females) value-returning instance method that calculates population per square mile (population / square miles) value-returning instance method that calculates population per state (population / states). overridden toString method that displays country name, total population, population per square mile, and population per state o display total population, population per square mile, and population per state by accessing the appropriate instance method. The main method should: create an object of the Population class for USA and initialize only the name either by using an additional overloaded constructor or by using the setter. Get user input for USA data (see sample output): number of males, number of females, square miles, number of states and then initialize the object using setters (do not use constructors to populate). Create and initialize objects of the Population class using the overloaded constructor as follows o Germany: males = 40,340,771; females = 41,961,693; square miles = 137,846.52; states = 16 o Argentina: males = 19,768,407; females = 20,643,969; square miles = 1,068,301.76; states = 23. Call the class method (toString) to display the output for each object as noted in sample output. Formatting: total population should be a whole number (no decimal) and comma separated. Population per square mile and population per state should be formatted as comma separated and to two decimal places.

Explanation / Answer

import java.util.*;
import java.math.*;
class population{
String country;
int male,female,state;
double sqmiles;
population(){
country="";
male=0;
female=0;
state=0;
sqmiles=0.0;
}
population(String cou,int mal,int fem,double sqm,int sta){
this.country=cou;
this.male=mal;
this.female=fem;
this.state=sta;
this.sqmiles=sqm;
}
int totpopulation(){ //Instance method to calculate total population
return male+female;
}
double poppersqmile(){ //Instance method to calculate population per sq. miles
double d=Math.round(((male+female)/sqmiles)*100.0)/100.0; //round off to two decimal places
return (d);
}
double popperstate(){ //Instance method to calculate populaton per state
double di=(double)state;
double res=Math.round(((male+female)*1.0/state)*100.0)/100.0; //round off to two decimal places
return res;
}
public String toString(){ //Overriding the toString method
int totpop=this.totpopulation(); //method calling
double sqmilpop=this.poppersqmile();
double statepop=this.popperstate();
return String.format("Country name="+country+" Total Population="+totpop+" Population per square miles="+sqmilpop+" Population per state="+statepop);
}
  
}
public class HelloWorld{

public static void main(String []args){
Scanner sc=new Scanner(System.in);
String country=sc.next();
int male=sc.nextInt();
int female=sc.nextInt();
int state=sc.nextInt();
double sqmiles=sc.nextDouble();
population p=new population(country,male,female,sqmiles,state);
population p1=new population("Germany",40340771,41961693,137846.52,16);
population p2=new population("Argentina",19768407,20643969,1068301.76,23);
System.out.println(p); //The compiler internally calls the toString method
System.out.println(p1);
System.out.println(p2);
  
}
}