Решение задач

1. Создать динамический массив

import java.util.Objects;

public class Box {
    private double width;
    private double height;
    private double depth;

    public Box(double width, double height, double depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getDepth() {
        return depth;
    }

    public void setDepth(double depth) {
        this.depth = depth;
    }

    double getVolume() {
        return width * height * depth;
    }

    @Override
    public String toString() {
        return "Box{" +
                "width=" + width +
                ", height=" + height +
                ", depth=" + depth +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Box box = (Box) o;
        return Double.compare(box.width, width) == 0 && Double.compare(box.height, height) == 0 && Double.compare(box.depth, depth) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(width, height, depth);
    }
}

 

import java.util.Objects;

public class HeavyBox extends Box {
    private int weight;

    public HeavyBox(double width, double height, double depth, int weight) {
        super(width, height, depth);
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "HeavyBox{" +
                "weight=" + weight +
                "} " + super.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        HeavyBox heavyBox = (HeavyBox) o;
        return weight == heavyBox.weight;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), weight);
    }
}

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListDemo {
    public static void main(String[] args) {
        List<HeavyBox> list = new ArrayList<>();
        list.add(new HeavyBox(2, 2, 2, 2));
        list.add(new HeavyBox(1, 1, 1, 1));
        list.add(new HeavyBox(3, 3, 3, 3));

        //Распечатать его содержимое используя for each
        list.forEach(System.out::println);

        //Изменить вес первого ящика на 1
        changeBoxWeight(list.get(0));

        //Удалить последний ящик
        list.remove(list.size() - 1);

        //Получить массив содержащий объекты класса HeavyBox из коллекции тремя способами
        // и вывести на консоль
        convertToArray(list);

        //Удалить все ящики
        list.clear();
    }

    private static void convertToArray(List<HeavyBox> list) {
        Object[] objects = list.toArray();
        System.out.println(Arrays.toString(objects));

        HeavyBox[] boxes1 =new HeavyBox[list.size()];
        list.toArray(boxes1);
        System.out.println(Arrays.toString(boxes1));

        HeavyBox[] boxes2 = list.toArray(new HeavyBox[0]);
        System.out.println(Arrays.toString(boxes2));
    }

    //Изменить вес ящика на 1
    private static void changeBoxWeight(HeavyBox box) {
        box.setWeight(box.getWeight() + 1);
    }
}
Read also:
Trustpilot
Trustpilot
Comments