Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package mgminterview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5.  
  6. //Your task is to:
  7. //- fix the code
  8. //- improve the poor quality of this code.
  9. //
  10. //(You can change anything you want
  11. //
  12. //The class should sort food instances and output the following lines:
  13. //
  14. //Potato 1
  15. //Potato 9
  16. //Potato 11
  17. //Tomato 11
  18. //Potato 12
  19. //Potato 42
  20. //Potato 46
  21. //Potato 55
  22. //Potato 77
  23. //Tomato 121
  24. //
  25.  
  26.  
  27. public class MgmInterviewFoodSort {
  28.  
  29.  
  30. /*FOOD is an abstract class that can be inherited by particular instantiations of food, and has two
  31. * properties- a size (unique to each instantiation of a food) and a name (same for every subclass)
  32. */
  33. public static abstract class FOOD implements Comparable<FOOD> {
  34. public int size;
  35. public String name;
  36. public FOOD () {
  37. this.size = -1;
  38. }
  39. public FOOD (int size) {
  40. this.size = size;
  41. }
  42.  
  43. public int getSize() {
  44. return this.size;
  45. }
  46. public String getName() {
  47. return this.name;
  48. }
  49.  
  50. public String toString () {
  51. return this.getName() + " " + this.getSize();
  52. }
  53.  
  54. //Sorts the different foods by their size and then alphabetically by type
  55. public int compareTo (FOOD other) {
  56. if (this.getSize() < other.getSize()) {
  57. return -1;
  58. }
  59. if (this.getSize() == other.getSize()) {
  60. return this.getName().compareTo(other.getName());
  61. }
  62. return 1;
  63. }
  64. }
  65. /* A Potato is a FOOD
  66. * Every Potato's name is "Potato"
  67. * Potato has no default size specific to it, so if the user doesn't input a size, it gets the default food size: -1
  68. */
  69. public static class Potato extends FOOD {
  70. public Potato () {
  71. this.name = "Potato";
  72. }
  73. public Potato (int size) {
  74. super(size);
  75. this.name = "Potato";
  76. }
  77.  
  78. }
  79. /* A Tomato is a FOOD
  80. * Every Tomato's name is "Tomato"
  81. * Its default size is 121
  82. */
  83. public static class Tomato extends FOOD {
  84. public Tomato () {
  85. super(121);
  86. this.name = "Tomato";
  87. }
  88. public Tomato (int size) {
  89. super(size);
  90. this.name = "Tomato";
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
  97. //Prints the foods
  98. public static void print(FOOD[] foods) {
  99. for (final FOOD food : Arrays.asList(foods)) {
  100. System.out.println(food.toString());
  101. }
  102. }
  103. //Creates an array of foods, then sorts and prints them
  104. public static void main(final String[] args) {
  105. FOOD[] foods = new FOOD[10];
  106.  
  107. foods[0] = new Tomato(11);
  108. foods[1] = new Tomato();
  109. foods[2] = new Potato(1);
  110. foods[3] = new Potato(42);
  111. foods[4] = new Potato(77);
  112. foods[5] = new Potato(55);
  113. foods[6] = new Potato(46);
  114. foods[7] = new Potato(12);
  115. foods[8] = new Potato(11);
  116. foods[9] = new Potato(9);
  117.  
  118.  
  119. Arrays.sort(foods);
  120. print(foods);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement