Question on Searching Arrays for Information


The codes below is a version of the program. Could you please read through it and write me another version of the program that means in a different logical idea but should produce the same output. Thank you in advance for your response.




/*
* Module: AustCapitals.java
*
*
* This example shows how arrays can be used in parallel to look up related
* information. One array contains the key and the other contains the related
* information.
*
* This example can actually be used both ways. ie you can look up the state
* and retrieve the capital city, or you can look up the capital city and
* retrieve the state.
*
*/

package mapping;


public class AustCapitals {

private String [] state = null;
private String [] capital = null;

/** Creates a new instance of AustCapitals
* The constructor creates two related arrays of the capital cities and
* states of Australia. Note that the relative ordering is important.
* ie the state and the related capital must have the same index.
*
*/
public AustCapitals() {
state = new String []{"NSW", "QLD", "VIC", "SA",
"TAS", "NT", "WA", "AUS"};
capital = new String [] {"Sydney", "Brisbane", "Melbourne",
"Adelaide", "Hobart", "Darwin",
"Perth", "Canberra"};
}

/**
* Given the state this method returns the capital of that state
* @param strState the state supplied as the search key must be in the
* standard capitalised form for Australian states.
* @return String Capital city if found. If the capital is not found
* a null is returned.
*/
public String getCapital(String strState){
boolean found = false;
int index;
for (index = 0; index < state.length; index++)
if (strState.equalsIgnoreCase(state[index])){
found = true;
break;
}
if (found)
return capital[index];
else
return null;
}

/**
* Given the city this method returns the capital's state.
* @param strCity The name of the capital city is the search key.
* @return String The state in standardised abbreviated form.
*/
public String getState(String strCity){
boolean found = false;
int index;
for (index = 0; index < state.length; index++)
if (strCity.equalsIgnoreCase(capital[index])){
found = true;
break;
}
if (found)
return state[index];
else
return null;
}

public static void main(String [] args){
AustCapitals ac = new AustCapitals();
String strCapitalNSW = ac.getCapital("NSW");
if (strCapitalNSW != null)
System.out.println(strCapitalNSW + " is the capital of NSW");

String strStateHobart = ac.getState("Hobart");
if (strStateHobart != null)
System.out.println("Hobart is in " + strStateHobart);
}

}

Comments

  • :
    : The codes below is a version of the program. Could you please read through it and write me another version of the program that means in a different logical idea but should produce the same output. Thank you in advance for your response.
    :
    :
    :
    :
    : /*
    : * Module: AustCapitals.java
    : *
    : *
    : * This example shows how arrays can be used in parallel to look up related
    : * information. One array contains the key and the other contains the related
    : * information.
    : *
    : * This example can actually be used both ways. ie you can look up the state
    : * and retrieve the capital city, or you can look up the capital city and
    : * retrieve the state.
    : *
    : */
    :
    : package mapping;
    :
    :
    : public class AustCapitals {
    :
    : private String [] state = null;
    : private String [] capital = null;
    :
    : /** Creates a new instance of AustCapitals
    : * The constructor creates two related arrays of the capital cities and
    : * states of Australia. Note that the relative ordering is important.
    : * ie the state and the related capital must have the same index.
    : *
    : */
    : public AustCapitals() {
    : state = new String []{"NSW", "QLD", "VIC", "SA",
    : "TAS", "NT", "WA", "AUS"};
    : capital = new String [] {"Sydney", "Brisbane", "Melbourne",
    : "Adelaide", "Hobart", "Darwin",
    : "Perth", "Canberra"};
    : }
    :
    : /**
    : * Given the state this method returns the capital of that state
    : * @param strState the state supplied as the search key must be in the
    : * standard capitalised form for Australian states.
    : * @return String Capital city if found. If the capital is not found
    : * a null is returned.
    : */
    : public String getCapital(String strState){
    : boolean found = false;
    : int index;
    : for (index = 0; index < state.length; index++)
    : if (strState.equalsIgnoreCase(state[index])){
    : found = true;
    : break;
    : }
    : if (found)
    : return capital[index];
    : else
    : return null;
    : }
    :
    : /**
    : * Given the city this method returns the capital's state.
    : * @param strCity The name of the capital city is the search key.
    : * @return String The state in standardised abbreviated form.
    : */
    : public String getState(String strCity){
    : boolean found = false;
    : int index;
    : for (index = 0; index < state.length; index++)
    : if (strCity.equalsIgnoreCase(capital[index])){
    : found = true;
    : break;
    : }
    : if (found)
    : return state[index];
    : else
    : return null;
    : }
    :
    : public static void main(String [] args){
    : AustCapitals ac = new AustCapitals();
    : String strCapitalNSW = ac.getCapital("NSW");
    : if (strCapitalNSW != null)
    : System.out.println(strCapitalNSW + " is the capital of NSW");
    :
    : String strStateHobart = ac.getState("Hobart");
    : if (strStateHobart != null)
    : System.out.println("Hobart is in " + strStateHobart);
    : }
    :
    : }
    :
    :
    Again you ask us to do your work, to which you already know the answer: no. Here's a hint: use a Map (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html) implementation. It is especially designed for this kind of work.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion