I need a Java app written Write an app that has a class name

I need a Java app written. Write an app that has a class named City, which has attributes name, state, yearOfFounding, and population. Provide at least 2 different constructor methods, plus getters and setters for each attribute. Create a 2nd class named CityDemo that allows the user to create and edit some City instances, so we need a loop that asks: “Would you like to create another City instance (y/n)?” The CityDemo logic that asks the user to enter year must contain an input validation loop. Same idea for population.

Solution


//City.java
public class City {
  
  
   //instance variables of class
   private String name;
   private String state;
   private int yearOfFounding;
   private double population;
  
  
   public City() {
       name=\"\";
       state=\"\";
       yearOfFounding=0;
       population=0;
   }
  
   //Constructor that takes name, state, founding and population
   public City(String name,
           String state,
           int yearOfFounding,
           double population) {
       setName(name);
       setState(state);      
       setYear(yearOfFounding);
       setPopulation(population);
   }
  
   //Setter methods of the class instance values
   public void setName(String name){
       this.name=name;
   }
  
   public void setState(String state){
       this.state=state;
   }
  
   public void setYear(int yearOfFounding){
       this.yearOfFounding=yearOfFounding;
   }
  
   public void setPopulation(double population){
       this.population=population;
   }
  
   //getter methods of the class instance values
   public String getName(){
       return name;
   }
  
   public String getState(){
       return state;
   }
  
   public int getYear(){
       return yearOfFounding;
   }
  
   public double getPopulation(){
       return population;
   }
  
  
   //Returns the string representation of City object
   @Override
   public String toString() {      
       return String.format(\"%-10s%-10s%-20d%5.2f\ \", name,state,yearOfFounding,population);
   }

}


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


//CityDemo.java
import java.util.Scanner;
public class CityDemo {

   public static void main(String[] args) {

       //Create an instance of Scanner class
       Scanner scanner=new Scanner(System.in);
      
       String choice=\"y\";
       City city=null;
       String name;
       String state;
       int yearOfFounding;
       double population;

       //prompt user to enter details of City class object
       //until user enter n to stop
       do
       {
           System.out.println(\"Enter name of city :\");
           name=scanner.nextLine();

           System.out.println(\"Enter name of state :\");
           state=scanner.nextLine();

          
           //validation of year
           do
           {
               System.out.println(\"Enter year of founding :\");
               yearOfFounding=Integer.parseInt(scanner.nextLine());

               if(yearOfFounding<0)
                   System.out.println(\"Invalid founding year.\");

           }while(yearOfFounding<0);

          
           //validation of population
           do
           {
               System.out.println(\"Enter population of city :\");
               population = Double.parseDouble(scanner.nextLine());
               if(population<0)
                   System.out.println(\"Invalid population.\");

           }while(population<0);

           System.out.println(\"Would you like to create another City instance (y/n)?\");
           choice=scanner.nextLine();

          
           //create an instance of city
           city=new City(name, state, yearOfFounding, population);

          
           System.out.println(\"City object\");
           System.out.printf(\"%-10s%-10s%-20s%-10s\ \",
                   \"Name\",\"State\",\"YearOfFounding\",\"Population\");
           //print city object
           System.out.println(city);

       }while(!choice.equals(\"n\"));
   }

}

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

Sample Output:

Enter name of city :
Delhi
Enter name of state :
Delhi
Enter year of founding :
1500
Enter population of city :
15000000
Would you like to create another City instance (y/n)?
n
City object
Name      State     YearOfFounding      Population
Delhi        Delhi        1500                           15000000.00

I need a Java app written. Write an app that has a class named City, which has attributes name, state, yearOfFounding, and population. Provide at least 2 differ
I need a Java app written. Write an app that has a class named City, which has attributes name, state, yearOfFounding, and population. Provide at least 2 differ
I need a Java app written. Write an app that has a class named City, which has attributes name, state, yearOfFounding, and population. Provide at least 2 differ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site