Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab, you use what you have learned about searching an array to find an exact match to fulfill a partially prewritten Java program.

In this lab, you use what you have learned about searching an array to find an exact match to fulfill a partially prewritten Java program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in the list of valid cities in Michigan. The data file provided for this lab includes the input statements and the necessary variable declarations. You need to use a loop to examine all the items in the array and test for a match. You also need to set a flag if there is a match, and then test the flag variable to determine if you should print the "Not a city in Michigan" message. Comments in the code tell you where to write your statements.

Instructions

  1. Study the prewritten code to make sure you understand it.
  2. Write the oop statement that examines the names of cities stored in the array.
  3. Write code that tests for a match.
  4. Write code that, when appropriate, prints the message: "Not a city in Michigan.".
  5. Execute the program using the following as input:
Chicago Brooklyn Watervliet Acme

//MichiganCities.java-ThisprogramprintsamessageforinvalidcitiesinMichigan.

//Input:Interactive.

//Output:Errormessageornothing.

importjava.util.Scanner;

publicclassMichiganCities

{

publicstaticvoidmain(Stringargs[])throwsException

{

//Declarevariables.

StringinCity; //nameofcitytolookupinarray.

//InitializedarrayofcitiesinMichigan.

StringcitiesInMichigan[]={"Acme","Albion","Detroit","Watervliet","Coloma","Saginaw","Richland","Glenn","Midland","Brooklyn"};

booleanfoundIt=false; //Flagvariable.

intx; //Loopcontrolvariable.

//Getuserinput.

Scannerinput=newScanner(System.in);

System.out.println("Enterthenameofthecity:");

inCity=input.nextLine();

//Writeyourloophere.

//Writeyourteststatementheretoseeifthereis

//amatch.Settheflagtotrueifcityisfound.

}

//Testtoseeifcitywasnotfoundtodetermineif

//"NotacityinMichigan"messageshouldbeprinted.

System.exit(0);

}//Endofmain()method.

}//EndofMichiganCitiesclass.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions