创建一个水果类(fruits),保证整个项目只能创建10种水果,创建的水果名称比如有:apple苹果、pear梨、apricot杏、peach桃、grape葡萄、banana香蕉、pineapple菠萝、watermelon西瓜、orange橙、lemon柠檬。要求有测试类,测试上面创建的对象是否正确。
时间: 2023-05-31 16:19:37 浏览: 100
8类水果图片数据集(每类100张图片左右)['橘子','菠萝','苹果','木瓜','火龙果','香蕉','樱桃','芒果']
5星 · 资源好评率100%
### 回答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());
}
}
}
阅读全文