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

Question: Write a Java console application that manages city names. Use an array

ID: 3794185 • Letter: Q

Question

Question:

Write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following menu to the user:

Menu

         1 – Add city

         2 – Change city name

         3 – Delete city

         4 – List cities

         9 – Exit

         Enter an option:

-Continue to read an option, process it, and display the menu until the user enters option 9. Here are option descriptions:

         Add city – prompt for a city name. If the city is already in the list, don’t add it and tell the user. If the city is not in the list, add it and tell the user. You don’t need to test if the array is full before adding a city.

         Change city name – prompt for a city (array) index. If the index is invalid, tell the user. If the index is valid, prompt for the new city name. If the new city name is already in the list, don’t add it and tell the user. If the new city name is not in the list, add it and tell the user.

         Delete city – prompt for a city (array) index. If the index is invalid, tell the user. If the index is valid, delete the city and move all the cities after the index up one to close the gap.

         List cities – use printf to format the list in two columns with the first column containing an array index and the second column containing the city name. Also, show a city count after the list.

Redisplay the menu after each option is processed. Start the array with three values already in it: City1, City2, and City3. Use these inputs for your last run:

         Option         Value(s)

         1               City4

         4

         2                6

         2                3, City5

         4

         2                3, City2

         3                6

         3                0

         4

Note: I'm a beginner at Java, so nothing too advanced. Thanks.

Explanation / Answer

import java.util.Scanner;
public class ne {
public static void main(String[] args) {
System.out.println("Hello, World!");
       Scanner in = new Scanner(System.in);
      
       String[] city = new String[20];
       String xcity;
      
       int choice, i, j, loop, pos, flag;
       flag = 1;
       i = 0;
       loop = 1;
      
       for(j = 0; j < 20; j++)
       {
           city[j] = "No city";
       }
       for(j = 0; j<20; j++)
                   {
                       System.out.println(city[j]);
                   }
       while(loop == 1)
       {
           System.out.println(" Enter the choice 1. Add City 2. Change city name 3. Delete City 4. List Ciies 9. Exit");
           choice = in.nextInt();
      
           switch(choice)
           {
               case 1: System.out.println(" Enter the city name ");
                   xcity = in.next();
                   for(j = 0; j <20; j++)
                   {
                       if(city[j] == xcity)
                       {
                           System.out.println(" City Already exists ");
                           flag = 0;
                           break;
                       }
                   }
                   if(flag == 1)
                       {
                           city[i] = xcity;
                           i++;
                       }
                  
                   break;
              
               case 2: System.out.println("Enter the position of city (1 to 20) ");
                   pos = in.nextInt();
                   pos--;
                   if(city[pos] == "No city")
                   {
                       System.out.println(" No city exists here ");
                   }
                   else
                   {
                       System.out.println(" Enter the new name of the city");
                       xcity = in.next();
                       for(j = 0; j <20; j++)
                       {
                           if(city[j] == xcity)
                           {
                               System.out.println(" City Already exists ");
                               flag = 0;
                               break;
                           }
                       }
                       if(flag == 1)
                       {
                           city[pos] = xcity;
                       }
                   }
                   break;
              
               case 3: System.out.println(" Enter the positon of the city (1 to 20) ");
                   pos = in.nextInt();
                   pos--;
                   if(city[pos] == "No city")
                   {
                       System.out.println(" No city exists here ");
                   }
                   else
                   {
                       city[pos] = "No city";
                   }
                   break;
              
               case 4: System.out.println(" The cities are as follows ");
                   for(j = 0; j<20; j++)
                   {
                       System.out.println(city[j]);
                   }
                   break;
              
               case 9: System.out.println(" Exiting function ");
                   System.exit(0);
                   break;
              
               default : System.out.println(" Wrong Choice!!!");
                   break;
           }
          
          
          
          
          
       }
   }
}

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