m1sclick

7

Aug 18th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1.  
  2. //
  3. // Complete the methods below. These methods manipulate Arrays of Strings
  4. // For the draft, complete the first method
  5. //
  6. import java.util.Arrays;
  7.  
  8. public class ArrayMethods
  9. {
  10. String[] list; //instance variable
  11. /**
  12. * Constructor for objects of class ArrayMethods
  13. */
  14. public ArrayMethods(String[] list)
  15. {
  16. // initialise instance variables
  17. this.list = list;
  18. }
  19.  
  20. /**
  21. * Determines if the array is sorted (do not sort)
  22. * When Strings are sorted, they are in alphabetical order
  23. * Use the compareTo method to determine which string comes first
  24. * You can look at the String compareTo method in the Java API
  25. * @return true if the array is sorted else false.
  26. */
  27. public boolean isSorted()
  28. {
  29. boolean sorted = true;
  30.  
  31. // TODO: Write the code to loop through the array and determine that each
  32. // successive element is larger than the one before it
  33. for (int i = 0; i < list.length - 1; i++)
  34. {
  35. if (list[i].compareTo(list[i + 1]) > 0)
  36. {
  37. sorted = false;
  38. }
  39. }
  40.  
  41. return sorted;
  42. }
  43.  
  44. /**
  45. * Replaces all but the first and last with the larger of its to neighbors
  46. * You can use the compareTo to determine which string is larger (later in alphabetical
  47. * order.
  48. * @return a string representation of the array. (do this with Arrays.toString(list))
  49. */
  50. public void replaceWithLargerNeighbor()
  51. {
  52. for (int i = 1; i < list.length - 1; i++)
  53. {
  54. String first = list[i];
  55. String second = list[i+1];
  56.  
  57. if (first.compareTo(second) <= 0)
  58. {
  59. list[i] = second;
  60. }
  61. else
  62. {
  63. list[i+1] = first;
  64. }
  65. }
  66. }
  67.  
  68. /**
  69. * Gets the number of duplicates in the array.
  70. * (Be careful to only count each duplicate once. Start at index 0. Does it match any of the other element?
  71. * Get the next word. It is at index i. Does it match any of the words with index > i?)
  72. * @return the number of duplicate words in the array.
  73. */
  74. public int countDuplicates()
  75. {
  76. int duplicates = 0;
  77.  
  78. for (int i = 0; i < list.length - 1; i++)
  79. {
  80. for (int j = i + 1; j < list.length; j++)
  81. {
  82. String first = list[i];
  83. String second = list[i+1];
  84.  
  85. if (list[i].equals(list[j]))
  86. {
  87. duplicates++;
  88. }
  89. }
  90. }
  91.  
  92. return duplicates;
  93. }
  94.  
  95. /**
  96. * Moves any word that starts with x, y, or z to the front of the array, but
  97. * otherwise preserves the order
  98. */
  99. public void xyzToFront()
  100. {
  101. int insertAt = 0;
  102. String word = "";
  103. for (int i = 0; i < list.length; i++)
  104. {
  105. word = list[i].substring(0, 1);
  106. if ("xyz".contains(word))
  107. {
  108. word = list[i];
  109.  
  110. // remove
  111. for (int j = i - 1; j >= insertAt; j--)
  112. {
  113. list[j + 1] = list[j];
  114. }
  115.  
  116. list[insertAt] = word;
  117. insertAt++;
  118. }
  119. }
  120. }
  121.  
  122. /**
  123. * gets the string representation of this array
  124. * @returns the string representation of this array in
  125. * standard collection format
  126. */
  127. public String toString()
  128. {
  129. return Arrays.toString(list);
  130. }
  131. }
  132.  
  133.  
  134.  
  135. //////////////////
  136. ///////////////
  137. ////////////////////
  138. //////////////////
  139. /////////////////
  140.  
  141. //We want to get the average temperature in Anchorage, Alaska, in
  142. //January and February, 1955.
  143. //
  144. //But to get a better idea of the normal temperature,
  145. //we will discard the highest and lowest temeratures. The tester will
  146. //get the temperatures from the website
  147. //http://academic.udayton.edu/kissock/http/Weather/gsod95-current/AKANCHOR.txt
  148. //
  149. //It will put them into a double[]array that is passed to the constructor of
  150. //your class. You will complete the TemperatureNormalizer class below.
  151.  
  152. //The TemperatureNormalizer class has a constructor that takes an array of
  153. //doubles as a parameter
  154. //
  155. //public TemperatureNormalizer(double[] list)
  156.  
  157. //It also has methods:
  158. //public double getAdjustedAverage() - gets the average minus the max and min
  159. //public double getMax()
  160. //public double getMin()
  161. //public double getSum()
  162. //
  163. //For the draft, implement the getSum method. The other methods are implemented
  164. //as stubs
  165. //
  166. //Note: the tester uses code we have not covered to get the values from
  167. //the website. You can just ignore it and think of it as the plumbing that
  168. //gets you a double[]
  169.  
  170. public class TemperatureNormalizer
  171. {
  172. private double[] data;
  173.  
  174. /**
  175. /* Constructs a TemperatureNormalizer with the given array
  176. /* @param the array to process
  177. */
  178. public TemperatureNormalizer(double[] list)
  179. {
  180. data = list;
  181. }
  182.  
  183. /**
  184. * Gets the adjusted average of the values in this array. The adjusted average
  185. * is calculated by removing the highest and lowest values and calculating
  186. * the average of the values that are left
  187. * @return the adjusted average
  188. */
  189. public double getAdjustedAverage()
  190. {
  191. double min = getMin();
  192. double max = getMax();
  193. //double num = 0.0;
  194.  
  195. //for (int i = 0; i < data.length; i++)
  196. //{
  197. // num = data[i];
  198. // if (num == min || num == max)
  199. // {
  200. // // remove
  201. // for (int j = i + 1; j < data.length; j++)
  202. // {
  203. // data[j - 1] = data[j];
  204. // }
  205. // }
  206. //}
  207.  
  208. int count = 0;
  209. double sum = 0.0;
  210.  
  211. for (double temp: data)
  212. {
  213. sum += temp;
  214. count++;
  215. }
  216.  
  217. sum = sum - min - max;
  218. count = count - 2;
  219.  
  220. if (count == 0)
  221. {
  222. return 0;
  223. }
  224.  
  225. return sum/count;
  226. }
  227.  
  228. /**
  229. * Gets the maximum value in the array of doubles
  230. * @return the maximum value
  231. */
  232. public double getMax()
  233. {
  234. double max = Double.MIN_VALUE;
  235. for (double num: data)
  236. {
  237. if (max < num)
  238. {
  239. max = num;
  240. }
  241. }
  242. return max;
  243. }
  244.  
  245. /**
  246. * Gets the minimum value in the array of doubles
  247. * @return the minimum value
  248. */
  249. public double getMin()
  250. {
  251. double min = Double.MAX_VALUE;
  252. for (double num: data)
  253. {
  254. if (min > num)
  255. {
  256. min = num;
  257. }
  258. }
  259. return min;
  260. }
  261.  
  262. /**
  263. * Gets the sum of the values in the array
  264. * @return the sum of the values in the array
  265. */
  266. public double getSum()
  267. {
  268. double sum = 0;
  269. for (double num: data)
  270. {
  271. sum += num;
  272. }
  273. return sum;
  274. }
  275.  
  276.  
  277. }
  278. /////////////
  279. /////////////////////
  280. //////////////////////
  281. //////////////////////
  282.  
  283. //This problem will use the same data as the previous one. This time you are
  284. //to complete the TemperatureDifferenceCalculator. It has two methods:
  285. //
  286. //public double maxDifference() - Calculates the maximum difference between
  287. //any two consecutive days. If Jan 7 temperature is 5 degrees and
  288. //Jan 8 is -10 degrees, the difference between the two temperatures is 15.
  289. //The temperature changed 15 degrees between one day and the next. The difference
  290. // is always the absolute value..
  291. //
  292. //public double minDifference() - Calculates the minimum difference between
  293. //any two consecutive days
  294. //
  295. //For the draft, provide the method stubs (in this case the headers and
  296. //a return value of 0)
  297.  
  298. public class TemperatureDifferenceCalculator
  299. {
  300. private double[] data;
  301.  
  302. /**
  303. * Constructs a TemperatureDifferenceCalculator with the given array
  304. * @param the array to process
  305. */
  306. public TemperatureDifferenceCalculator(double[] list)
  307. {
  308. data = list;
  309. }
  310.  
  311. /**
  312. * Gets the maximum difference between any two consecutive values
  313. * @return the maximum difference
  314. */
  315.  
  316. // TODO: add the stub for the maxDifference method. That is the header, the braces, and the return statement
  317. public double maxDifference()
  318. {
  319. double diff = Double.MIN_VALUE;
  320. for (int i = 0; i < data.length - 1; i++)
  321. {
  322. double first = data[i];
  323. double second = data[i+1];
  324. if (Math.abs(first - second) > diff)
  325. {
  326. diff = Math.abs(first - second);
  327. }
  328. }
  329. return diff;
  330. }
  331.  
  332. /**
  333. * Gets the minimum difference between any two consecutive values
  334. * @return the minimum difference
  335. */
  336.  
  337. // TODO: add the stub for the minDifference method. That is the header, the braces, and the return statement
  338. public double minDifference()
  339. {
  340. double diff = Double.MAX_VALUE;
  341. for (int i = 0; i < data.length - 1; i++)
  342. {
  343. double first = data[i];
  344. double second = data[i+1];
  345. if (Math.abs(first - second) < diff)
  346. {
  347. diff = Math.abs(first - second);
  348. }
  349. }
  350. return diff;
  351. }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment