67,513
社区成员
发帖
与我相关
我的任务
分享
Circle c1 = new Circle(1, "BLUE", true);
Circle c2 = new Circle(2, "BLUE", true);
Circle c3 = new Circle(3, "BLUE", false);
Circle c4 = new Circle(4, "BLACK", true);
System.out.println( "c1 ? c1 " + c1.compareTo(c1) );
System.out.println( "c1 ? c2 " + c1.compareTo(c2) );
System.out.println( "c1 ? c3 " + c1.compareTo(c3) );
System.out.println( "c1 ? c4 " + c1.compareTo(c4) );
Rectangle r1 = new Rectangle(1,1,"BLUE", true);
Rectangle r2 = new Rectangle(2,2,"RED", true);
Rectangle r3 = new Rectangle(4,4,"GREEN", false);
Rectangle r4 = new Rectangle(4,4,"GREEN", true);
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "c1 ? r2 " + c1.compareTo(r2) );
System.out.println( "r3 ? r4 " + r3.compareTo(r4) );
public abstract class GeometricObject implements Comparable<GeometricObject> {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject(){
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Created on " + dateCreated + "\ncolor: " + color +
"and filled: " + filled;
}
public static void main(String[] args) {
Circle1 c1 = new Circle1(1, "BLUE", true);
Circle1 c2 = new Circle1(2, "BLUE", true);
Circle1 c3 = new Circle1(3, "BLUE", false);
Circle1 c4 = new Circle1(4, "BLACK", true);
System.out.println( "c1 ? c1 " + c1.compareTo(c1) );
System.out.println( "c1 ? c2 " + c1.compareTo(c2) );
System.out.println( "c1 ? c3 " + c1.compareTo(c3) );
System.out.println( "c1 ? c4 " + c1.compareTo(c4) );
Rectangle r1 = new Rectangle(1,1,"BLUE", true);
Rectangle r2 = new Rectangle(2,2,"RED", true);
Rectangle r3 = new Rectangle(4,4,"GREEN", false);
Rectangle r4 = new Rectangle(4,4,"GREEN", true);
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "c1 ? r2 " + c1.compareTo(r2) );
System.out.println( "r3 ? r4 " + r3.compareTo(r4) );
}
public static GeometricObject max (GeometricObject o1, GeometricObject o2){
if (((Comparable)o1).compareTo(o2) > 0 )
return o1;
else
return o2;
}
private String compareTo(Circle1 c3) {
// TODO Auto-generated method stub
return null;
}
private String compareTo(Rectangle r2) {
// TODO Auto-generated method stub
return null;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}}
public class Rectangle extends GeometricObject implements Comparable{
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double Width, double Height){
this.width = Width;
this.height = Height;
}
public Rectangle(double Width, double Height, String Color, boolean Filled){
this.width = Width;
this.height = Height;
setColor(Color);
setFilled(Filled);
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public void setHeight(double Height){
this.height = Height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}}
class Circle1 extends GeometricObject implements Comparable<> {
private double radius;
public Circle1() {
}
public Circle1(double radius){
this.radius = radius;
}
public Circle1(double radius, String color, boolean filled){
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getArea(){
return radius * radius * Math.PI;
}
public double getDiameter(){
return 2 * radius;
}
public double getPerimeter(){
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}}
Circle c1 = new Circle(1, "BLUE", true);
Circle c2 = new Circle(2, "BLUE", true);
Circle c3 = new Circle(3, "BLUE", false);
Circle c4 = new Circle(4, "BLACK", true);
System.out.println( "c1 ? c1 " + c1.compareTo(c1) );
System.out.println( "c1 ? c2 " + c1.compareTo(c2) );
System.out.println( "c1 ? c3 " + c1.compareTo(c3) );
System.out.println( "c1 ? c4 " + c1.compareTo(c4) );
Rectangle r1 = new Rectangle(1,1,"BLUE", true);
Rectangle r2 = new Rectangle(2,2,"RED", true);
Rectangle r3 = new Rectangle(4,4,"GREEN", false);
Rectangle r4 = new Rectangle(4,4,"GREEN", true);
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "c1 ? r2 " + c1.compareTo(r2) );
System.out.println( "r3 ? r4 " + r3.compareTo(r4) );
去算出c1比c1 以此类推,然后得出
c1 ? c2 -1
c1 ? c3 -1
c1 ? c4 1
r1 ? r2 -1
r1 ? r2 -1
c1 ? r2 1
r3 ? r4 -1
class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getDiameter() {
return 2 * radius;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println("The circle is created " + getDateCreated()
+ " and the radius is " + radius);
}
}
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double Width, double Height) {
this.width = Width;
this.height = Height;
}
public Rectangle(double Width, double Height, String Color, boolean Filled) {
this.width = Width;
this.height = Height;
setColor(Color);
setFilled(Filled);
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public void setHeight(double Height) {
this.height = Height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
public abstract class GeometricObject implements Comparable<GeometricObject> {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject() {
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Created on " + dateCreated + "\ncolor: " + color
+ "and filled: " + filled;
}
public static void main(String[] args) {
Circle c1 = new Circle(1, "BLUE", true);
Circle c2 = new Circle(2, "BLUE", true);
Circle c3 = new Circle(3, "BLUE", false);
Circle c4 = new Circle(4, "BLACK", true);
System.out.println("c1 ? c1 " + c1.compareTo(c1));
System.out.println("c1 ? c2 " + c1.compareTo(c2));
System.out.println("c1 ? c3 " + c1.compareTo(c3));
System.out.println("c1 ? c4 " + c1.compareTo(c4));
Rectangle r1 = new Rectangle(1, 1, "BLUE", true);
Rectangle r2 = new Rectangle(2, 2, "RED", true);
Rectangle r3 = new Rectangle(4, 4, "GREEN", false);
Rectangle r4 = new Rectangle(4, 4, "GREEN", true);
System.out.println("r1 ? r2 " + r1.compareTo(r2));
System.out.println("r1 ? r2 " + r1.compareTo(r2));
System.out.println("c1 ? r2 " + c1.compareTo(r2));
System.out.println("r3 ? r4 " + r3.compareTo(r4));
}
public abstract double getArea();
private double getTrueArea() {
return (filled ? 1 : 0.5f) * getArea();
}
@Override
public int compareTo(GeometricObject obj) {
if (obj.getClass().equals(getClass())
&& getTrueArea() == obj.getTrueArea()
&& getColor().equals(obj.getColor())) {
return 0;
}
if (this instanceof Circle && "BLUE".equals(getColor())
&& (!(obj instanceof Circle && "BLUE".equals(obj.getColor())))) {
return 1;
}
if (getTrueArea() > obj.getTrueArea()) {
return 1;
}
return -1;
}
}
@Override
public int compareTo(Object o) {
if(o==null || !(o instanceof Circle))return 1;
Circle c = (Circle)o;
return this.getColor().compareTo(c.getColor());
}}
@Override
public int compareTo(Object o) {
if(o==null || !(o instanceof Rectangle))return 1;
Rectangle r = (Rectangle )o;
if(r.width == this.width && r.height == this.height){
return 0;
}
if(this.width < r.width && this.height < r.height){
return -1;
}
return 1;
}}