Watermelon西瓜

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that’s why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that’s why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.
一个炎热的夏天,皮特和他的朋友比利决定买一个西瓜。在他们看来,他们选择了最大最成熟的。然后称西瓜的重量,秤上的重量显示为w公斤。他们急忙赶回家,渴死了,决定分浆果,然而他们面临着一个困难的问题。
皮特和比利是偶数的超级粉丝,这就是为什么他们想把西瓜分成两部分,两部分的重量都是偶数公斤&#

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目要求创建一个水果类(fruits),保证具有完整的项目只能创建10种水果。创建的水果名称比如有:apple苹果、pear梨、apricot杏、peach桃、grape葡萄、banana香蕉、pineapple菠萝、watermelon西瓜、orange橙、lemon柠檬。要求有测试类,测试上面创建的对象是否正确。 ### 回答2: 创建水果类(Fruits)是创建一个基本的OOP程序实践。Fruits类应该具有水果的名称,颜色,重量和价格。我们需要编写测试类(FruitsTest),以确保Fruits类中的对象创建和设置是正确的。 在这个例子中,我们将仅创建10种水果,包括苹果、梨、杏、桃、葡萄、香蕉、菠萝、西瓜、橙和柠檬。 首先,我们需要创建Fruits类。建议添加特定的getter和setter方法和toString方法,以便更好的访问和理解代码的实现。 public class Fruits { private String name; private String color; private Double weight; private Double price; public Fruits(String name, String color, Double weight, Double price) { this.name = name; this.color = color; this.weight = weight; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Double getWeight() { return weight; } public void setWeight(Double weight) { this.weight = weight; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Fruit Name: " + this.name + ", Color: " + this.color + ", Weight: " + this.weight + ", Price: " + this.price; } } 在完成了Fruits类的创建后,接下来我们要编写测试类FruitsTest。 public class FruitsTest { public static void main(String[] args) { Fruits apple = new Fruits("Apple", "Red", 1.0, 2.50); System.out.println(apple.toString()); Fruits pear = new Fruits("Pear", "Yellow", 1.5, 3.50); System.out.println(pear.toString()); Fruits apricot = new Fruits("Apricot", "Orange", 1.2, 3.00); System.out.println(apricot.toString()); Fruits peach = new Fruits("Peach", "Pink", 1.8, 4.50); System.out.println(peach.toString()); Fruits grape = new Fruits("Grape", "Purple", 1.0, 5.50); System.out.println(grape.toString()); Fruits banana = new Fruits("Banana", "Yellow", 1.2, 6.00); System.out.println(banana.toString()); Fruits pineapple = new Fruits("Pineapple", "Yellow", 2.0, 8.00); System.out.println(pineapple.toString()); Fruits watermelon = new Fruits("Watermelon", "Green", 6.0, 10.00); System.out.println(watermelon.toString()); Fruits orange = new Fruits("Orange", "Orange", 1.0, 4.00); System.out.println(orange.toString()); Fruits lemon = new Fruits("Lemon", "Yellow", 0.5, 3.00); System.out.println(lemon.toString()); } } 在FruitsTest测试类中,我们使用new运算符来创建每个水果对象。然后使用toString方法将其输出到控制台以确保每个对象的属性正确设置。这里只是一个简单的示例,您可以添加其他测试用例来测试不同场景。 现在您已经创建了一个简单的水果类(Fruits)和它的测试类(FruitsTest)。您可以在其他实际项目中使用这个类,在不同的方法中添加更多逻辑,例如更多的属性,更多的基本操作等等。 ### 回答3: 项目设计: 创建一个Fruits类,其中包含属性:名称(name)、颜色(color)、甜度(sweetness)和重量(weight)。还有一个构造函数,用于将传入的参数值分别赋予到类中的属性。 同时,在该类中设置10个常量属性,用于代表10种水果及它们的名称。 测试类设计: 创建一个FruitTest类,用于验证Fruits类的功能性。 首先,在FruitTest类中创建一个Main函数。在Main函数中,先创建10个Fruits对象,对于每个对象,分别以10种不同的方式进行初始化,并打印出对象的属性来检查对象是否正确构造。 接下来,从这十个对象中,选出任何两个对象进行比较,并打印出它们之间属性的相同与不同之处来验证对象比较的正确性。 最后,创建一个包含「名称」和「重量」属性的HashMap对象,并将Fruits对象通过名称作为键值存储到HashMap对象中。通过在 HashMap 中查找来检查类中打印的对象是否与通过 HashMap 存储的对象所吻合。 运行测试: 执行Main函数,检查输出结果,确保每个对象的属性与期望值相同,并且对象之间和HashMap中存储的对象比较结果正确无误。 代码示例: public class Fruits { public static final String APPLE = "apple"; public static final String PEAR = "pear"; public static final String APRICOT = "apricot"; public static final String PEACH = "peach"; public static final String GRAPE = "grape"; public static final String BANANA = "banana"; public static final String PINEAPPLE = "pineapple"; public static final String WATERMELON = "watermelon"; public static final String ORANGE = "orange"; public static final String LEMON = "lemon"; private String name; private String color; private double sweetness; private double weight; public Fruits(String name, String color, double sweetness, double weight) { this.name = name; this.color = color; this.sweetness = sweetness; this.weight = weight; } // getter and setter methods public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getSweetness() { return sweetness; } public void setSweetness(double sweetness) { this.sweetness = sweetness; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } } public class FruitTest { public static void main(String[] args) { Fruits apple1 = new Fruits(Fruits.APPLE, "Red", 7.5, 134); Fruits apple2 = new Fruits(Fruits.APPLE, "Green", 6.5, 120); Fruits pear1 = new Fruits(Fruits.PEAR, "Yellow", 5.0, 250); Fruits pear2 = new Fruits(Fruits.PEAR, "Green", 6.0, 260); Fruits apricot1 = new Fruits(Fruits.APRICOT, "Orange", 8.0, 80); Fruits apricot2 = new Fruits(Fruits.APRICOT, "Yellow", 7.5, 72); Fruits peach1 = new Fruits(Fruits.PEACH, "Pink", 6.0, 200); Fruits peach2 = new Fruits(Fruits.PEACH, "Yellow", 5.5, 190); Fruits grape1 = new Fruits(Fruits.GRAPE, "Red", 8.0, 400); Fruits grape2 = new Fruits(Fruits.GRAPE, "Green", 7.5, 420); System.out.println(apple1.getName() + ", " + apple1.getColor() + ", " + apple1.getSweetness() + ", " + apple1.getWeight()); System.out.println(orange1.getName() + ", " + orange1.getColor() + ", " + orange1.getSweetness() + ", " + orange1.getWeight()); System.out.println(lemon1.getName() + ", " + lemon1.getColor() + ", " + lemon1.getSweetness() + ", " + lemon1.getWeight()); System.out.println(apple1.equals(apple2)); // true System.out.println(apple1.equals(pear1)); // false Map<String, Fruits> map = new HashMap<>(); map.put(apple1.getName(), apple1); map.put(apple2.getName(), apple2); map.put(pear1.getName(), pear1); map.put(pear2.getName(), pear2); map.put(apricot1.getName(), apricot1); map.put(apricot2.getName(), apricot2); map.put(peach1.getName(), peach1); map.put(peach2.getName(), peach2); map.put(grape1.getName(), grape1); map.put(grape2.getName(), grape2); for (String key : map.keySet()) { Fruits fruits = map.get(key); System.out.println(fruits.getName() + ", " + fruits.getWeight()); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值