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

Create a class named State that holds the following fields: a String for the nam

ID: 643173 • Letter: C

Question

Create a class named State that holds the following fields: a String for the
name of the state, an integer for the population, and two City objects that hold
data about the capital city and the most populous city. The State constructor
requires six parameters that represent the names and populations of the state,
its capital, and its most populous city. Provide get methods for each field.
Create the City class to be a nonstatic, private inner class within the State
class; the City class contains a city

Explanation / Answer


/**
* The State java class contains the name and population of state
* and contains an inner class City that accepts a city and city populations
* and each field has getter methods to access name and population.
* */
//State.java
public class State
{
  
   //instance variables of class State
   private String name;
   private int statePopulation;
   //City class variables
   private City capitalCity;
   private City popularCity;
  
  
       //State constructor that accepts six variables
   //state name and population , city and city population
   //and popular city and popular city population
   public State(String name,int statePopulation,
           String capitalCity, int capitalPopulation,
           String popularCity, int popularCityPopulation)
   {
       this.name=name;
       this.statePopulation=statePopulation;
       //constructs a capital city by calling inner class City
       this.capitalCity=new City(capitalCity, capitalPopulation);
       //constructs a popular city city by calling inner class City
       this.popularCity=new City(popularCity,popularCityPopulation);
      
   }
  
   //getter methods
   public String getName()
   {
       return name;          
   }
  
   public int getPopulation()
   {
       return statePopulation;
   }
  
  
   //getter methods fro capital city name and population
   public String getCapitcalCity()
   {
       return capitalCity.getCityName();
   }
  
   public int getCapitalCityPopulation()
   {
       return capitalCity.getPopulation();
   }
  
  
   //getter methods for popular city name and population
   public String getPopularCityName()
   {
       return popularCity.getCityName();
   }
  
   public int getPopularCityPopulation()
   {
       return popularCity.getPopulation();
   }
  
//no static City class inner class
   private class City
   {
       private String cityName;
       private int cityPopulation;
      
       public City(String capitalCity, int cityPopulation)
       {
           this.cityName=capitalCity;
           this.cityPopulation=cityPopulation;
       }
      
       //return city name
       public String getCityName()
       {
           return cityName;          
       }
      
       //return city population
       public int getPopulation()
       {
           return cityPopulation;
       }
  
      
   }//end of the inner class City.
}//end of State class
----------------------------------------------------------------------------------------------------------------

/**
* The java program TestState that creates two state
* objects for State class and sets the name , population
* and two cities capital and most popular cities
* names and populations and print their values
* using getter methods of State class
*
* */
//TestState.java
public class TestState
{
   public static void main(String[] args)
   {
      
       System.out.println("California State Information");
      
       //Create an instance variable for State class with six arguments
       State california_state=new State("California", 380000000,
               "Sacramento",479686,
               "New York,",3884307 );
       //print state information using getter methods
       System.out.println("State Name:"+ california_state.getName());
       System.out.println("Population :"+ california_state.getPopulation());
      
       System.out.println("Capital City Name:"+ california_state.getCapitcalCity());
       System.out.println("Population :"+ california_state.getCapitalCityPopulation());
      
       System.out.println("Populat City Name:"+ california_state.getPopularCityName());
       System.out.println("Population :"+ california_state.getPopularCityPopulation());
      
      
      
       System.out.println(" Texas State Information");
       //Create an instance variable for State class with six arguments
       State texas_state=new State("Texas", 260000000,
               "Austin",790390,
               "Houstan,",2099451 );
      
       //print state information using getter methods
       System.out.println("State Name:"+ texas_state.getName());
       System.out.println("Population :"+ texas_state.getPopulation());
      
       System.out.println("Capital City Name:"+ texas_state.getCapitcalCity());
       System.out.println("Population :"+ texas_state.getCapitalCityPopulation());
      
       System.out.println("Populat City Name:"+ texas_state.getPopularCityName());
       System.out.println("Population :"+ texas_state.getPopularCityPopulation());
      
      
      
      
      
   }
}


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

Sample output:

California State Information
State Name:California
Population :380000000
Capital City Name:Sacramento
Population :479686
Populat City Name:New York,
Population :3884307


Texas State Information
State Name:Texas
Population :260000000
Capital City Name:Austin
Population :790390
Populat City Name:Houstan,
Population :2099451

Hope this helps you

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