Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- public class Ball {
- private double volume;
- private Color color;
- public Ball(double volume, Color color) {
- this.volume = volume;
- this.color = color;
- }
- public Ball(double volume) {
- this(volume, Color.WHITE);
- }
- public double getVolume() {
- return volume;
- }
- public void setVolume(double volume) {
- this.volume = volume;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- Ball ball = (Ball) o;
- return Double.compare(ball.volume, volume) == 0 && color == ball.color;
- }
- @Override
- public int hashCode() {
- return Objects.hash(volume, color);
- }
- @Override
- public int compareTo(Ball o) {
- return Double.compare(this.volume, o.volume);
- }
- }
- 2
- public class BallContainer {
- protected Collection<Ball> balls;
- public BallContainer() {
- balls = new ArrayList<>();
- }
- /**
- * Adds a ball to the container.
- * @param b the Ball to be added
- * @return true if b was successfully added
- */
- public boolean add(Ball b) {
- return balls.add(b);
- }
- /**
- * Removes a ball from the container.
- * @param b the Ball to be removed
- * @return true if b was present in the collection
- */
- public boolean remove(Ball b) {
- return balls.remove(b);
- }
- /**
- * Returns the sum of the volumes of all balls in the container.
- * @return
- */
- public double getVolume() { return balls.stream().mapToDouble(Ball::getVolume).sum(); }
- /**
- * Returns the total count of balls in the container.
- * @return
- */
- public int size() {
- return balls.size();
- }
- /**
- * Removes all balls from the container.
- */
- public void clear() {
- balls.clear();
- }
- /**
- * Checks whether a Ball is present in the container.
- * @param b the Ball to check
- * @return true if b is present
- */
- public boolean contains(Ball b) {
- return balls.contains(b);
- }
- }
- }
- 3
- public class Box extends BallContainer {
- private double capacity;
- public Box(double capacity) {
- this.capacity = capacity;
- }
- @Override
- public boolean add(Ball b) throws BallContainerException {
- if (balls.size() == capacity) {
- throw new BallContainerException();
- }
- return super.add(b);
- }
- public Iterator<Ball> getBallsFromSmallest() {
- // Collections.sort((List)balls);
- List<Ball> sorted = new ArrayList<Ball>(balls);
- sorted.sort(Comparator.comparing(Ball::getVolume));
- return sorted.iterator();
- }
- }
- 4
- public class MainClass {
- public static void main(String[] args) {
- Box box = new Box(100);
- try (Scanner s = new Scanner(System.in)) {
- while (s.hasNext()) {
- double param = s.nextDouble();
- Color c = param % 2 == 0 ? Color.WHITE : Color.BLACK;
- box.add(new Ball(param, c));
- }
- }
- BallContainer container = new BallContainer();
- for (Iterator<Ball> it = box.getBallsFromSmallest(); it.hasNext(); ) {
- container.add(it.next());
- }
- UniqueBallContainer uniq = new UniqueBallContainer();
- for (Iterator<Ball> it = box.getBallsFromSmallest(); it.hasNext(); ) {
- System.out.println(it.next().getVolume());
- }
- if (box.size() == container.size() && box.size() == uniq.size()) {
- System.out.println("success");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement