新手求大神帮忙解决问题了

Game-Over 2014-10-15 10:40:24
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 0
c1 ? c2 -1
c1 ? c3 -1
c1 ? c4 1
r1 ? r2 -1
r1 ? r2 -1
c1 ? r2 1
r3 ? r4 -1


我写了一些,但是怎么都搞不出来,求救。。。。
代码在下面
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;
}}



求各位大神帮忙改改写写解决下,谢谢了。。。
...全文
263 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你的Circle与Rectangle都要实现compareTo方法 @Override public int compareTo(Object o) {         Circle c = (Circle)o;         return this.getColor().compareTo(c.getColor()); }} @Override public int compareTo(Object o) {         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; }}   
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
引用 3 楼 wojiaolinaaa 的回复:
c1 ? c1 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
_浪子 2014-10-15
  • 打赏
  • 举报
回复
c1 ? c1 0谁知道这是什么。你要达到什么效果
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
引用 1 楼 wojiaolinaaa 的回复:
你要做什么都没说怎么改
大神。。。我已经在开头写了,就是用最开始的那个代码去运行出来 c1 ? c1 0 c1 ? c2 -1 c1 ? c3 -1 c1 ? c4 1 r1 ? r2 -1 r1 ? r2 -1 c1 ? r2 1 r3 ? r4 -1 而且要用到GeometricObject, circle1, 和rectangle。。。 谢谢啦!
_浪子 2014-10-15
  • 打赏
  • 举报
回复
你要做什么都没说怎么改
coffee-time 2014-10-15
  • 打赏
  • 举报
回复
全都帮你改了下

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;
	}
}
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
求各位大神帮忙看看啊,真的不知道怎么改了
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
引用 9 楼 sirius93 的回复:
你第十五行把两个不同对象比,所以报了强转的异常,把五楼的代码再改一下,强转前先判断一下

@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;
}}
谢谢大哥,改了后已经可以全部运行出来了。可是不知道为什么运算出来的结果和它要求的不一样呢。 c1 ? c1 0 c1 ? c2 0 c1 ? c3 0 c1 ? c4 20 r1 ? r2 -1 r1 ? r2 -1 c1 ? r2 1 r3 ? r4 0
coffee-time 2014-10-15
  • 打赏
  • 举报
回复
你第十五行把两个不同对象比,所以报了强转的异常,把五楼的代码再改一下,强转前先判断一下

@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;
}}
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
引用 7 楼 littlebrain4solving 的回复:
[quote=引用 6 楼 u012945394 的回复:] [quote=引用 5 楼 littlebrain4solving 的回复:] 你的Circle与Rectangle都要实现compareTo方法 @Override public int compareTo(Object o) {         Circle c = (Circle)o;         return this.getColor().compareTo(c.getColor()); }} @Override public int compareTo(Object o) {         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; }}   
大神,我用了override可是我的结果为什么和它要求的不一样呢? c1 ? c1 0 c1 ? c2 -1 c1 ? c3 -1 c1 ? c4 -1 r1 ? r2 -1 r1 ? r2 -1 Exception in thread "main" java.lang.ClassCastException: CSU.Tim.Rectangle cannot be cast to CSU.Tim.Circle1 at CSU.Tim.Circle1.compareTo(Circle1.java:58) at CSU.Tim.Testa.main(Testa.java:22) [/quote] 你把你想要的描述清楚,对象和对象比较的时候你到底想比较什么东西.[/quote] 大神应该是比较面积, The rules for comparison are 1. A blue circle is larger than any other figure (regardless of the areas involved). 2. For other cases compare the figure’s areas. However; transparent objects (filled attribute is false) contribute with only 50% of their area as opposed to a 100% for opaque objects.
  • 打赏
  • 举报
回复
引用 6 楼 u012945394 的回复:
[quote=引用 5 楼 littlebrain4solving 的回复:] 你的Circle与Rectangle都要实现compareTo方法 @Override public int compareTo(Object o) {         Circle c = (Circle)o;         return this.getColor().compareTo(c.getColor()); }} @Override public int compareTo(Object o) {         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; }}   
大神,我用了override可是我的结果为什么和它要求的不一样呢? c1 ? c1 0 c1 ? c2 -1 c1 ? c3 -1 c1 ? c4 -1 r1 ? r2 -1 r1 ? r2 -1 Exception in thread "main" java.lang.ClassCastException: CSU.Tim.Rectangle cannot be cast to CSU.Tim.Circle1 at CSU.Tim.Circle1.compareTo(Circle1.java:58) at CSU.Tim.Testa.main(Testa.java:22) [/quote] 你把你想要的描述清楚,对象和对象比较的时候你到底想比较什么东西.
Game-Over 2014-10-15
  • 打赏
  • 举报
回复
引用 5 楼 littlebrain4solving 的回复:
你的Circle与Rectangle都要实现compareTo方法 @Override public int compareTo(Object o) {         Circle c = (Circle)o;         return this.getColor().compareTo(c.getColor()); }} @Override public int compareTo(Object o) {         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; }}   
大神,我用了override可是我的结果为什么和它要求的不一样呢? c1 ? c1 0 c1 ? c2 -1 c1 ? c3 -1 c1 ? c4 -1 r1 ? r2 -1 r1 ? r2 -1 Exception in thread "main" java.lang.ClassCastException: CSU.Tim.Rectangle cannot be cast to CSU.Tim.Circle1 at CSU.Tim.Circle1.compareTo(Circle1.java:58) at CSU.Tim.Testa.main(Testa.java:22)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧