Advertisement
INSECURE-FLOWERPOT

Untitled

Feb 22nd, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. public class Place implements Comparable
  2. {
  3. /**Stores the cities.**/
  4.  
  5. private ArrayList<String>city=new ArrayList<String>();
  6.  
  7. /**Stores the state.**/
  8. private String state;
  9.  
  10. /**
  11. * This default constructor will assign a "fake" city and state if a city or state was not constructed.
  12. * <P>
  13. * Algorithm:<br>
  14. * 1. Assign the field variables a null or "".<br>
  15. * </P>
  16. */
  17. public Place()
  18. {
  19. state = null;
  20. }
  21.  
  22. /**
  23. * This constructor will assign a specific city and state when constructed.
  24. * <P>
  25. * Algorithm:<br>
  26. * 1. Assign a specific city and state to the field variables.<br>
  27. * </P>
  28. * @param city Passes in the name of the city.
  29. * @param state Passes in the name of the state.
  30. */
  31. public Place(String city, String state)
  32. {
  33. this.state = state;
  34.  
  35. this.city.add(city);
  36.  
  37. }
  38.  
  39. /**
  40. * The getCity() method will return the city name that was assigned to the field variable "city".
  41. * <P>
  42. * Algorithm:<br>
  43. * 1. Retrieve the city field and return it.<br>
  44. * </P>
  45. * @return This method returns the city.
  46. */
  47. public ArrayList<String> getCity()
  48. {
  49. Collections.sort(city);
  50.  
  51. return city;
  52. }
  53. /**
  54. * The getState() method will return the State abbreviation that was assigned to the field variable "state".
  55. * <P>
  56. * Algorithm:<br>
  57. * 1. Retrieve the state field and return it.<br>
  58. * </P>
  59. * @return This method returns the state.
  60. */
  61. public String getState()
  62. {
  63. return state;
  64. }
  65.  
  66. @Override
  67. /**
  68. * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
  69. * <P>
  70. * Algorithm:<br>
  71. * 1. Construct a Place object and cast parameter "temp" to Place.<br>
  72. * 2. Compare the current object "this" to another object "temp"<br>
  73. * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
  74. * </P>
  75. * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
  76. */
  77. public int compareTo(Object temp)
  78. {
  79. Place other=(Place)temp;
  80. String otherOne=other.getState();
  81.  
  82. return state.compareTo(otherOne);
  83. }
  84. //Test
  85. public static void main(String [] args)
  86. {
  87. ArrayList<String>list=new ArrayList<String>();
  88.  
  89. Place p1=new Place("New York City","NY");
  90. p1=new Place("Yonkers","NY");
  91. p1=new Place("Albany","NY");
  92. p1=new Place("Syracuse","NY");
  93. list=p1.getCity();
  94. for(String x:list)
  95. System.out.println(x);
  96.  
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement