Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Question on Searching Arrays for Information

flytofallflytofall Posts: 6Member

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

  • zibadianzibadian Posts: 6,349Member
    :
    : 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.