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: 3754353 • 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

Below is the Population.java:

package chegg;

public class Population {
   //declare a variable country name,males,females,square miles,states to display
   String country_name;
    int number_of_males;
    int number_of_females;
    double number_of_square_miles;
    int states;
    //constructor
   public Population(String country_name, int number_of_males, int number_of_females, double number_of_square_miles, int states) {
       super();
       this.country_name = country_name;
       this.number_of_males = number_of_males;
       this.number_of_females = number_of_females;
       this.number_of_square_miles = number_of_square_miles;
       this.states = states;
   }
   //setter and getter mthod
   public String getCountry_name() {
       return country_name;
   }
   public void setCountry_name(String country_name) {
       this.country_name = country_name;
   }
   public int getNumber_of_males() {
       return number_of_males;
   }
   public void setNumber_of_males(int number_of_males) {
       this.number_of_males = number_of_males;
   }
   public int getNumber_of_females() {
       return number_of_females;
   }
   public void setNumber_of_females(int number_of_females) {
       this.number_of_females = number_of_females;
   }
   public double getNumber_of_square_miles() {
       return number_of_square_miles;
   }
   public void setNumber_of_square_miles(double number_of_square_miles) {
       this.number_of_square_miles = number_of_square_miles;
   }
   public int getStates() {
       return states;
   }
   public void setStates(int states) {
       this.states = states;
   }
   //tostring method
   @Override
   public String toString() {
       return country_name + ", males = " + number_of_males + ", females = " + number_of_females + ", square miles = " + number_of_square_miles + " , States"+states;
   }
  
   //main mthod
   public static void main(String[] args)
    {
       //create a population object and pass the number to the constructor
        Population b = new Population("Germany", 40340771, 41961693,137846.52,16);
        System.out.println(b); //prints the object
        Population b1 = new Population("Argentina", 19768407, 20643969, 1068301.76,23);
        System.out.println(b1); //prints the object
    }
}

sample output:

Germany, males = 40340771, females = 41961693, square miles = 137846.52 , States16
Argentina, males = 19768407, females = 20643969, square miles = 1068301.76 , States23