Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package mgminterview;
  2.  
  3. import java.util.Arrays;
  4.  
  5. //Your task is to:
  6. //- fix the code
  7. //- improve the poor quality of this code.
  8. //
  9. //(You can change anything you want
  10. //
  11. //The class should sort food instances and output the following lines:
  12. //
  13. //Potato 1
  14. //Potato 9
  15. //Potato 11
  16. //Tomato 11
  17. //Potato 12
  18. //Potato 42
  19. //Potato 46
  20. //Potato 55
  21. //Potato 77
  22. //Tomato 121
  23. //
  24.  
  25.  
  26. public class MgmInterviewFoodSort {
  27.  
  28. public static FOOD[] foods = null;
  29.  
  30. public abstract class FOOD {
  31. public int size;
  32. public FOOD () {
  33. this.size = -1;
  34. }
  35.  
  36. //i removed the whoami method, since it wasn't serving any real purpose beyond what instanceof does
  37.  
  38.  
  39.  
  40. }
  41.  
  42. public class Potato extends FOOD {
  43. public Potato () {
  44. super();
  45. }
  46. public Potato (int size) {
  47. super();
  48. this.size = size;
  49. }
  50.  
  51. public void setSize (int size) {
  52. this.size = size;
  53. }
  54. public int getSize () {
  55. return this.size;
  56. }
  57. public String toString () {
  58. return "Potato" + " " + this.getSize();
  59. }
  60.  
  61. }
  62.  
  63. public class Tomato extends FOOD {
  64. public Tomato () {
  65. super();
  66. this.size = 121;
  67. }
  68. public Tomato (int size) {
  69. super();
  70. this.size = size;
  71. }
  72. public void setSize (int size) {
  73. this.size = size;
  74. }
  75. public int getSize () {
  76. return this.size;
  77. }
  78. public String toString () {
  79. return "Tomato" + " " +this.getSize();
  80. }
  81. }
  82.  
  83. /*replaced the O(n^2) sort method with merge sort. if the sizes equal, this favors potato over tomato
  84. * not really necessary when you're sorting 10 things, but now the sort method scales up
  85. */
  86. void merge(FOOD arr[], int l, int m, int r) {
  87. int n1 = m - l + 1;
  88. int n2 = r - m;
  89.  
  90. FOOD L[] = new FOOD [n1];
  91. FOOD R[] = new FOOD [n2];
  92.  
  93. for (int i=0; i<n1; ++i)
  94. L[i] = arr[l + i];
  95. for (int j=0; j<n2; ++j)
  96. R[j] = arr[m + 1+ j];
  97.  
  98.  
  99.  
  100. int i = 0, j = 0;
  101.  
  102. int k = l;
  103. while (i < n1 && j < n2)
  104. {
  105.  
  106. int first =0;
  107. int second =0;
  108.  
  109. if (L[i] instanceof Potato) {
  110. first = ((Potato)L[i]).getSize();
  111. }
  112. else {
  113. first = ((Tomato)L[i]).getSize();
  114. }
  115.  
  116. if (R[j] instanceof Potato) {
  117. second = ((Potato)R[j]).getSize();
  118. }
  119. else {
  120. second = ((Tomato)R[j]).getSize();
  121. }
  122.  
  123.  
  124. if ((first == second && L[i] instanceof Potato) ||first < second) {
  125. arr[k] = L[i];
  126. i++;
  127. }
  128. else
  129. {
  130. arr[k] = R[j];
  131. j++;
  132. }
  133. k++;
  134. }
  135.  
  136. while (i < n1)
  137. {
  138. arr[k] = L[i];
  139. i++;
  140. k++;
  141. }
  142.  
  143. while (j < n2)
  144. {
  145. arr[k] = R[j];
  146. j++;
  147. k++;
  148. }
  149. }
  150.  
  151. void sort(FOOD arr[], int l, int r) {
  152. if (l < r) {
  153. int m = (l+r)/2;
  154.  
  155. sort(arr, l, m);
  156. sort(arr , m+1, r);
  157.  
  158. merge(arr, l, m, r);
  159. }
  160. }
  161.  
  162. public void print() {
  163. for (FOOD food : Arrays.asList(foods)) {
  164. if (food instanceof Potato) {
  165. System.out.println(((Potato)food).toString());
  166. }
  167.  
  168. else {
  169. System.out.println(((Tomato)food).toString());
  170.  
  171. }
  172. }
  173. }
  174.  
  175. public static void main(final String[] args) {
  176.  
  177. MgmInterviewFoodSort fs=new MgmInterviewFoodSort();
  178.  
  179. foods = new FOOD[10];
  180.  
  181.  
  182. Tomato t1 = fs.new Tomato(11);
  183. foods[0] = t1;
  184.  
  185.  
  186. Tomato t2 = fs.new Tomato();
  187. foods[1] = t2;
  188.  
  189.  
  190.  
  191. Potato p1 = fs.new Potato(1);
  192. foods[2] = p1;
  193.  
  194.  
  195. Potato p2 = fs.new Potato(42);
  196. foods[3] = p2;
  197.  
  198. Potato p3 = fs.new Potato(77);
  199. foods[4] = p3;
  200.  
  201. Potato p4 = fs.new Potato(55);
  202. foods[5] = p4;
  203.  
  204. Potato p5 = fs.new Potato(46);
  205. foods[6] = p5;
  206.  
  207. Potato p6 = fs.new Potato(12);
  208. foods[7] = p6;
  209.  
  210. Potato p7 = fs.new Potato(11);
  211. foods[8] = p7;
  212.  
  213. Potato p8 = fs.new Potato(9);
  214. foods[9] = p8;
  215.  
  216.  
  217.  
  218. fs.sort(foods, 0, foods.length-1);
  219. fs.print();
  220. }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement