m1sclick

Untitled

Aug 19th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. //
  2. //Complete the class ArrayListMethods. It consists of four short methods to manipulate an array list of strings.
  3. //The method header and javadoc are given to you.
  4. //
  5. //For the draft, provide the isSorted method.
  6. //
  7.  
  8. import java.util.ArrayList;
  9. public class ArrayListMethods
  10. {
  11. ArrayList<String> list; //instance variable
  12. /**
  13. * Constructor for objects of class ArrayListMethods
  14. */
  15. public ArrayListMethods(ArrayList<String> arrayList)
  16. {
  17. // initialise instance variables
  18. list = arrayList;
  19. }
  20.  
  21. /**
  22. * Determines if the array list is sorted (do not sort)
  23. * When Strings are sorted, they are in alphabetical order
  24. * Use the compareTo method to determine which string comes first
  25. * You can look at the String compareTo method in the Java API
  26. * @return true if the array list is sorted else false.
  27. */
  28. public boolean isSorted()
  29. {
  30. boolean sorted = true;
  31.  
  32. // TODO: Determine if the array is sorted.
  33. for (int i = 0; i < list.size() - 1; i++)
  34. {
  35. String first = list.get(i);
  36. String second = list.get(i+1);
  37. if (first.compareTo(second) > 0)
  38. {
  39. sorted = false;
  40. }
  41. }
  42.  
  43. return sorted;
  44. }
  45.  
  46. /**
  47. * Replaces all but the first and last elements with the larger of its two neighbors
  48. * You can use the compareTo() method to determine which string is larger (larger in alphabetical
  49. * order).
  50. * Example: if the list is originally
  51. * ["cat", "ape", "dog", "horse", "zebra"]
  52. * after this method it should be:
  53. * ["cat", "dog", "horse", "zebra", "zebra"]
  54. *
  55. * @return a string representation of the modified array list. (do this with list.toString())
  56. */
  57. public void replaceWithLargerNeighbor()
  58. {
  59.  
  60. // TODO: Replace all but the first and last elements with the larger of its to neighbors
  61. for (int i = 1; i < list.size() - 1; i++)
  62. {
  63. String first = list.get(i);
  64. String second = list.get(i+1);
  65. if (first.compareTo(second) > 0)
  66. {
  67. list.set(i+1, first);
  68. }
  69. else
  70. {
  71. list.set(i, second);
  72. }
  73. }
  74. }
  75.  
  76. /**
  77. * Gets the number of duplicates in the list.
  78. * Be careful to only count each duplicate once. Start at index 0. (Does it match any of the other elements?)
  79. * Get the next word. It is at index i. (Does it match any of the words with index > i?)
  80. * @return the number of duplicate words in the list
  81. */
  82. public int countDuplicates()
  83. {
  84. int duplicates = 0;
  85.  
  86. // TODO: Write the code to get the number of duplicates in the list
  87. for (int i = 0; i < list.size() - 1; i++)
  88. {
  89. for (int j = i+1; j < list.size(); j++)
  90. {
  91. if (list.get(i).equals(list.get(j)))
  92. {
  93. duplicates++;
  94. }
  95. }
  96. }
  97.  
  98. return duplicates;
  99. }
  100.  
  101. /**
  102. * Moves any word that starts with x, y, or z to the front of the ArrayList, but
  103. * otherwise preserves the order
  104. * Example: if the list is orginially
  105. * ["ape", "dog", "xantus", "zebra", "cat", "yak"]
  106. * after this method is called it should be
  107. * ["xantus", "zebra", "yak", "ape", "dog", "cat"]
  108. */
  109. public void xyzToFront()
  110. {
  111. int insertAt = 0;
  112.  
  113. // TODO: Move any word that starts with x, y, or z to the front of the ArrayList, but otherwise preserves the order
  114. for (int i = 0; i < list.size(); i++)
  115. {
  116. String word = list.get(i).substring(0, 1);
  117. if ("xyz".contains(word))
  118. {
  119. list.add(insertAt, list.remove(i));
  120. insertAt++;
  121. }
  122. }
  123. }
  124.  
  125. /**
  126. * gets the string representation of this array list
  127. * @returns the string representation of this array list in
  128. * standard collection format
  129. */
  130. public String toString()
  131. {
  132. return list.toString();
  133. }
  134. }
  135.  
  136. ///////////////////////////////
  137. ///////////////////////////////////////////////
  138. ////////////////////////////////////////////
  139. //////////////////////////////////////////
  140. ///////////////////////////////////////////////
  141. //
  142. //Complete the class TripPlan which describes the cities that are visited by a tour conducted by Java Now Tours.
  143. //Keep an arraylist of cities (just the string name). Have methods to add a city, remove a city,
  144. //to return the names of the cities in a String, and to reverse the order of the elements in the array list.
  145. //
  146. //Notice that the reverse method is void.
  147.  
  148. //
  149. //For the draft, provide the instance variable and finish the constructor.
  150. //For the toString method simply return the string "TripPlan["
  151. //
  152.  
  153. import java.util.ArrayList;
  154.  
  155. /**
  156. * A TripPlan represents a trip and holds a collection of city names.
  157. */
  158. public class TripPlan
  159. {
  160. // TODO: add instance variable here
  161. ArrayList<String> plan;
  162.  
  163. /**
  164. * Constructs an empty trip.
  165. */
  166. public TripPlan()
  167. {
  168. // TODO: Initialize the instance variable
  169. plan = new ArrayList<String>();
  170. }
  171.  
  172. /**
  173. * Add a city to the list.
  174. * @param cityName the city to add
  175. */
  176. public void addCity(String cityName)
  177. {
  178. // TODO: Write code to add a city to the array list instance variable
  179. plan.add(cityName);
  180. }
  181.  
  182. /**
  183. * Returns a string describing the object.
  184. * @return a string in the format "TripPlan[cityName1,cityName2,...]"
  185. */
  186. public String toString()
  187. {
  188. String ret = plan.toString();
  189. return "TripPlan" + ret.replace(", ", ",");
  190. }
  191.  
  192. /**
  193. * Removes a city form the this trip
  194. * @param cityName city to remove
  195. */
  196. public void removeCity(String cityName)
  197. {
  198. // TODO: Write code to remove a city to the array list instance variable
  199. if (plan.indexOf(cityName) != -1)
  200. {
  201. plan.remove(plan.indexOf(cityName));
  202. }
  203. }
  204.  
  205. /**
  206. * Reverses the elements in the itinerary.
  207. */
  208. public void reverse()
  209. {
  210. System.out.println(plan);
  211. for (int i = 0; i < plan.size(); i++)
  212. {
  213. plan.add(i, plan.remove(plan.size() - 1));
  214. }
  215. }
  216. }
  217. //////////////////////////////////
  218. ////////////////////////////
  219. //////////////////////////////////
  220. ///////////////////////////////////
  221. ////////////////////////////////////
  222. //Create a Polygon class. A polygon is a closed shape with lines joining the corner points.
  223. //You will keep the points in an array list. Use object of java.awt.Point for the point.
  224.  
  225. //Polygon will have as an instance variable an ArrayList of Points to hold the points
  226. //The constructor takes no parameters but initializes the instance variable
  227. //
  228. //The add method adds a Point to the polygon
  229. //
  230. //The perimeter method returns the perimeter of the polygon
  231. //
  232. //The draw method draws the polygon by connecting consecutive points and then
  233. //connecting the last point to the first.
  234. //
  235. //No methods headers or javadoc is provided this time. You get to try your hand at writing a class almost from scratch
  236. //
  237. //For the draft, finish the constructor.
  238. //Have the perimeter method return 0 and have the draw mwthod draw a
  239. //line from point 0, 0 to point 30, 40
  240. //
  241.  
  242. import java.util.ArrayList;
  243. import java.awt.Point;
  244. public class Polygon
  245. {
  246. // TODO: provide the required constructor, instance variable, and methods
  247. ArrayList<Point> points;
  248.  
  249. public Polygon()
  250. {
  251. points = new ArrayList<Point>();
  252. }
  253.  
  254. public void add(Point p)
  255. {
  256. points.add(p);
  257. }
  258.  
  259. public double perimeter()
  260. {
  261. Point first, second;
  262. double perimeter = 0.0;
  263.  
  264. for(int i = 0; i < points.size() - 1; i++)
  265. {
  266. first = points.get(i);
  267. second = points.get(i+1);
  268. perimeter += Math.sqrt(Math.pow(second.getX() - first.getX(), 2) + Math.pow(second.getY() - first.getY(), 2));
  269. }
  270.  
  271. first = points.get(0);
  272. second = points.get(points.size() - 1);
  273. perimeter += Math.sqrt(Math.pow(second.getX() - first.getX(), 2) + Math.pow(second.getY() - first.getY(), 2));
  274.  
  275.  
  276. return perimeter;
  277. }
  278.  
  279. public void draw()
  280. {
  281. Point first, second;
  282.  
  283. for(int i = 0; i < points.size() - 1; i++)
  284. {
  285. first = points.get(i);
  286. second = points.get(i+1);
  287.  
  288. Line line = new Line(first.getX(), first.getY(), second.getX(), second.getY());
  289. line.draw();
  290. }
  291.  
  292. first = points.get(0);
  293. second = points.get(points.size() - 1);
  294. Line line = new Line(first.getX(), first.getY(), second.getX(), second.getY());
  295. line.draw();
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment