Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. 1
  2. public class Ball {
  3.  
  4. private double volume;
  5. private Color color;
  6.  
  7. public Ball(double volume, Color color) {
  8. this.volume = volume;
  9. this.color = color;
  10. }
  11.  
  12. public Ball(double volume) {
  13. this(volume, Color.WHITE);
  14. }
  15.  
  16. public double getVolume() {
  17. return volume;
  18. }
  19.  
  20. public void setVolume(double volume) {
  21. this.volume = volume;
  22. }
  23.  
  24. public Color getColor() {
  25. return color;
  26. }
  27.  
  28. public void setColor(Color color) {
  29. this.color = color;
  30. }
  31.  
  32. @Override
  33. public boolean equals(Object o) {
  34. if (this == o) {
  35. return true;
  36. }
  37.  
  38. if (o == null || getClass() != o.getClass()) {
  39. return false;
  40. }
  41.  
  42. Ball ball = (Ball) o;
  43.  
  44. return Double.compare(ball.volume, volume) == 0 && color == ball.color;
  45. }
  46.  
  47. @Override
  48. public int hashCode() {
  49. return Objects.hash(volume, color);
  50. }
  51.  
  52. @Override
  53. public int compareTo(Ball o) {
  54. return Double.compare(this.volume, o.volume);
  55. }
  56. }
  57. 2
  58.  
  59. public class BallContainer {
  60.  
  61. protected Collection<Ball> balls;
  62.  
  63. public BallContainer() {
  64. balls = new ArrayList<>();
  65. }
  66.  
  67. /**
  68. * Adds a ball to the container.
  69. * @param b the Ball to be added
  70. * @return true if b was successfully added
  71. */
  72. public boolean add(Ball b) {
  73. return balls.add(b);
  74. }
  75.  
  76. /**
  77. * Removes a ball from the container.
  78. * @param b the Ball to be removed
  79. * @return true if b was present in the collection
  80. */
  81. public boolean remove(Ball b) {
  82. return balls.remove(b);
  83. }
  84.  
  85. /**
  86. * Returns the sum of the volumes of all balls in the container.
  87. * @return
  88. */
  89. public double getVolume() { return balls.stream().mapToDouble(Ball::getVolume).sum(); }
  90.  
  91. /**
  92. * Returns the total count of balls in the container.
  93. * @return
  94. */
  95. public int size() {
  96. return balls.size();
  97. }
  98.  
  99. /**
  100. * Removes all balls from the container.
  101. */
  102. public void clear() {
  103. balls.clear();
  104. }
  105.  
  106. /**
  107. * Checks whether a Ball is present in the container.
  108. * @param b the Ball to check
  109. * @return true if b is present
  110. */
  111. public boolean contains(Ball b) {
  112. return balls.contains(b);
  113. }
  114. }
  115.  
  116. }
  117. 3
  118. public class Box extends BallContainer {
  119.  
  120. private double capacity;
  121.  
  122. public Box(double capacity) {
  123. this.capacity = capacity;
  124. }
  125.  
  126. @Override
  127. public boolean add(Ball b) throws BallContainerException {
  128. if (balls.size() == capacity) {
  129. throw new BallContainerException();
  130. }
  131.  
  132. return super.add(b);
  133. }
  134.  
  135. public Iterator<Ball> getBallsFromSmallest() {
  136. // Collections.sort((List)balls);
  137. List<Ball> sorted = new ArrayList<Ball>(balls);
  138.  
  139. sorted.sort(Comparator.comparing(Ball::getVolume));
  140.  
  141. return sorted.iterator();
  142. }
  143.  
  144. }
  145. 4
  146. public class MainClass {
  147.  
  148. public static void main(String[] args) {
  149. Box box = new Box(100);
  150.  
  151. try (Scanner s = new Scanner(System.in)) {
  152. while (s.hasNext()) {
  153. double param = s.nextDouble();
  154. Color c = param % 2 == 0 ? Color.WHITE : Color.BLACK;
  155.  
  156. box.add(new Ball(param, c));
  157. }
  158. }
  159.  
  160. BallContainer container = new BallContainer();
  161.  
  162. for (Iterator<Ball> it = box.getBallsFromSmallest(); it.hasNext(); ) {
  163. container.add(it.next());
  164. }
  165.  
  166. UniqueBallContainer uniq = new UniqueBallContainer();
  167.  
  168. for (Iterator<Ball> it = box.getBallsFromSmallest(); it.hasNext(); ) {
  169. System.out.println(it.next().getVolume());
  170. }
  171.  
  172. if (box.size() == container.size() && box.size() == uniq.size()) {
  173. System.out.println("success");
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement