Моя Java-программа не сортирует выходные данные.У меня нет опыта работы с Java, и после просмотра похожих вопросов я не смог выяснить проблему с моим кодом.
В выводе отображаются 3 сферы и их цвета, но не отображаются радиусы и не сортируется ихпо их области, как это должно быть.
Ниже приведены 3 .java-файла, участвующих в моей программе, у меня нет ошибок или предупреждений в Eclipse, поэтому я предполагаю, что я поместил некоторый параметр или значение в неправильное место ..Я ценю любую помощь, спасибо большое!
ComparableSphere.java
public class ComparableSphere extends GeometricObject implements Comparable<ComparableSphere>{
private double radius;
public ComparableSphere(){
this("white",0);
this.radius = 0;
}
public ComparableSphere(String color, double radius){
super(color);
this.radius = radius;
}
public double area() {
return Math.PI * this.radius * this.radius * 4;
}
public double perimeter() {
return 2 * Math.PI * this.radius;
}
@Override
public int compareTo(ComparableSphere o) {
// TODO Auto-generated method stub
return 0;
}
}
GeometricObject.java
public abstract class GeometricObject {
private String color;
protected GeometricObject(){
this("white");
}
protected GeometricObject(String color) {
this.color = color;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color = color;
}
public abstract double area();
public abstract double perimeter();
public String toString() {
return this.getClass().getSimpleName() + ": color= " + this.color;
}
public boolean equals(Object obj) {
if(!(obj instanceof GeometricObject)){
return false;
}
GeometricObject other = (GeometricObject)obj;
return this.color.equalsIgnoreCase(other.color);
}
}
driver.java
import java.util.ArrayList;
import java.util.Collections;
public class driver {
public static void main(String[] args){
ComparableSphere sphere1 = new ComparableSphere("Purple", 10.1);
ComparableSphere sphere2 = new ComparableSphere("Orange", 3.8);
ComparableSphere sphere3 = new ComparableSphere("Tan", 5.2);
ArrayList<ComparableSphere> sphereList = new ArrayList<ComparableSphere>();
sphereList.add(sphere1);
sphereList.add(sphere2);
sphereList.add(sphere3);
System.out.println("Unsorted list: \n"+sphereList+"\n");
Collections.sort(sphereList);
System.out.println("Sorted list: \n"+sphereList);
}
}
введите описание изображения здесь