Create an enumeration for a datatype that represents airports, and has the abili
ID: 3928352 • Letter: C
Question
Create an enumeration for a datatype that represents airports, and has the ability to change from airport code and city name. Include five values: PHX, LAX, SFO, NRT, and SIN. Also add a static method called getAirportCity.
getAirportCity(...) will take an Airport enumeration value as a parameter, and return a String containing the city where that airport is located (e.g., "Phoenix", "Los Angeles", "Tokyo", "San Francisco"). Use a switch statement. Have a default case that returns "Unknown City". Do not implement the switch statement for SIN (Singapore) - we want to test the default case first.
Explanation / Answer
public class enm
{
//Definition of enumeration
public enum airports
{
PHX, LAX, SFO, NRT, SIN
}
//function returns the airport city takes enumeration as a parameter
public static String getAirportCity(airports AR)
{
switch(AR)
{
case PHX:
return "Phoenix";
case LAX:
return "Los Angeles";
case SFO:
return "Tokyo";
case NRT:
return "San Francisco";
default:
return "Unknown City";
}
}
public static void main(String[] args)
{
System.out.println("City where that airport is located " + getAirportCity(airports.PHX));
System.out.println("City where that airport is located " + getAirportCity(airports.LAX));
System.out.println("City where that airport is located " + getAirportCity(airports.SFO));
System.out.println("City where that airport is located " + getAirportCity(airports.NRT));
System.out.println("City where that airport is located " + getAirportCity(airports.SIN));
}
}
Output:
City where that airport is located Phoenix
City where that airport is located Los Angeles
City where that airport is located Tokyo
City where that airport is located San Francisco
City where that airport is located Unknown City
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.