Java 箱子类实现:存储苹果,计算重量、排序、查找最重苹果

本文将介绍如何使用 Java 实现一个箱子类,该箱子类可以用来存储苹果,并提供一系列功能,包括:

  • 计算箱子中所有苹果的总重量
  • 获取箱子中苹果的数量
  • 比较两个箱子的体积大小
  • 创建一个空的箱子(长宽高随机)
  • 创建一个包含指定数量苹果的箱子(苹果的重量和颜色随机)
  • 获取箱子中所有红色苹果
  • 按颜色对箱子中的苹果进行排序
  • 在不打乱原有苹果顺序的情况下,获取箱子中最重的那个苹果

1. 定义苹果类 (Apple)

class Apple {
    private int weight;
    private String color;

    public Apple(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }

    // 省略getter和setter方法
}

2. 定义箱子类 (Box)

class Box {
    private int c;
    private int k;
    private int g;
    private String color;
    private List<Apple> apples;

    public Box(int c, int k, int g, String color, List<Apple> apples) {
        this.c = c;
        this.k = k;
        this.g = g;
        this.color = color;
        this.apples = apples;
    }

    // 省略getter和setter方法

    // 获取该箱子中所有苹果的总重量
    public int getTotalAppleWeight() {
        int totalWeight = 0;
        for (Apple apple : apples) {
            totalWeight += apple.getWeight();
        }
        return totalWeight;
    }

    // 获取该箱子中苹果的数量
    public int getAppleNum() {
        return apples.size();
    }

    // 与其他的箱子比较体积,返回箱子的体积差
    public int compareTo(Box box) {
        int thisVolume = c * k * g;
        int boxVolume = box.getC() * box.getK() * box.getG();
        return thisVolume - boxVolume;
    }

    // 生成一个长宽高随机的没有苹果的空箱子
    public static Box createEmptyBox() {
        Random random = new Random();
        int c = random.nextInt(10) + 1;
        int k = random.nextInt(10) + 1;
        int g = random.nextInt(10) + 1;
        String color = "";
        List<Apple> apples = new ArrayList<>();
        return new Box(c, k, g, color, apples);
    }

    // 创建具有n个苹果的非空箱子(箱子中苹果的重量和颜色随机)
    public static Box createBox(int n) {
        Random random = new Random();
        int c = random.nextInt(10) + 1;
        int k = random.nextInt(10) + 1;
        int g = random.nextInt(10) + 1;
        String color = "";
        List<Apple> apples = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int weight = random.nextInt(100) + 1;
            int colorIndex = random.nextInt(3);
            switch (colorIndex) {
                case 0:
                    color = '红色';
                    break;
                case 1:
                    color = '绿色';
                    break;
                case 2:
                    color = '黄色';
                    break;
            }
            Apple apple = new Apple(weight, color);
            apples.add(apple);
        }
        return new Box(c, k, g, color, apples);
    }

    // 获取该箱子中所有的红色苹果
    public List<Apple> getRedApples() {
        List<Apple> redApples = new ArrayList<>();
        for (Apple apple : apples) {
            if (apple.getColor().equals('红色')) {
                redApples.add(apple);
            }
        }
        return redApples;
    }

    // 将该箱子中的所有的苹果按照颜色排好序
    public void sortApplesByColor() {
        Collections.sort(apples, new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        });
    }

    // 在不打乱原有苹果的顺序的前提下,获取箱子中最重的那个苹果
    public Apple getMaxApple() {
        Apple maxApple = apples.get(0);
        for (Apple apple : apples) {
            if (apple.getWeight() > maxApple.getWeight()) {
                maxApple = apple;
            }
        }
        return maxApple;
    }
}

3. 测试类 (TestBox)

public class TestBox {
    public static void main(String[] args) {
        Box box1 = Box.createEmptyBox();
        Box box2 = Box.createBox(5);

        System.out.println("box1中苹果的数量:" + box1.getAppleNum());
        System.out.println("box2中苹果的数量:" + box2.getAppleNum());

        System.out.println("box1中所有苹果的总重量:" + box1.getTotalAppleWeight());
        System.out.println("box2中所有苹果的总重量:" + box2.getTotalAppleWeight());

        System.out.println("box1与box2的体积差:" + box1.compareTo(box2));

        List<Apple> redApples = box2.getRedApples();
        System.out.println("box2中所有红色苹果的数量:" + redApples.size());

        box2.sortApplesByColor();
        System.out.println("box2中按颜色排序后的苹果:");
        for (Apple apple : box2.getApples()) {
            System.out.println(apple.getColor() + " " + apple.getWeight());
        }

        Apple maxApple = box2.getMaxApple();
        System.out.println("box2中最重的苹果重量为:" + maxApple.getWeight());
    }
}

说明:

  • 以上代码中省略了 Apple 类和 Box 类的 getter 和 setter 方法,读者可以根据需要自行添加。
  • 在 createBox 方法中,我们随机生成三种颜色:'红色', '绿色', '黄色',读者可以根据需要添加更多颜色。
  • 在 sortApplesByColor 方法中,我们使用了 Comparator 接口来实现按颜色排序的功能,读者也可以使用其他排序方法。
  • 在 getMaxApple 方法中,我们遍历箱子中的所有苹果,找到重量最大的那个。读者可以根据需要修改比较条件,例如可以根据苹果的颜色或其他属性进行比较。

总结:

本文提供了一个简单的 Java 箱子类实现,演示了如何使用 Java 实现一些常见的功能,例如计算总重量、获取数量、比较体积、创建随机对象、排序、查找最大值等。读者可以根据自己的需要对代码进行修改和扩展。


原文地址: https://www.cveoy.top/t/topic/n8EY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录