为了账号安全,请及时绑定邮箱和手机立即绑定

正在回答

2 回答

//类
public class Circle {
	//私有属性radius
	private int radius;
	
	//getter、setter
	public int getRadius(){
		return radius;
	}
	public void setRadius(int radius){
		this.radius = radius;
	}
	
	//构造方法
	public Circle() {}  //无参构造
	public Circle(int radius){
		this.radius = radius;
	}
	
	//比较方法
	public void compareCircle(Circle c){
		if(this.radius>c.radius){
			System.out.println("该圆比较大!");
		}else if(this.radius<c.radius){
			System.out.println("该圆比较小!");
		}else{
			System.out.println("两个圆相等!");
		}
	}
}

//主函数入口
public static void main(String[] args) {
		//默认构造 半径为5
		Circle circle1 = new Circle();
		circle1.setRadius(5);
		
		//带参构造
		Circle circle2 = new Circle(8);
		Circle circle3 = new Circle(10);
		Circle circle4 = new Circle(5);
		
		//比较
		circle1.compareCircle(circle2);
		circle1.compareCircle(circle3);
		circle1.compareCircle(circle4);
}



1 回复 有任何疑惑可以回复我~
#1

慕设计6383936

题目要求比较方法另外定义一个主类,里面的radius就不能直接引用了吧,因为是私有的...
2017-06-12 回复 有任何疑惑可以回复我~


public class Geometry{

    private String color = "white";

    private boolean filled;

    private java.util.Date dateCreated;

    /** construct a default geometry object*/

    public Geometry(){

        dateCreated = new java.util.Date();

    }

    /** construct a geometry object with specified color and filled value */

    public Geometry( String color, boolean filled ){

        dateCreated = new java.util.Date();

        this.color = color;

        this.filled = filled;

    }

    /** return color */

    public String getColor(){

        return color;

    }

    /** set a new color */

    public void setColor( String color ){

        this.color = color;

    }

    /** return filled */

    public boolean isFilled(){

        return filled;

    }

    /** set a new filled */

    public void setFilled( boolean filled ){

        this.filled = filled;

    }

    /** get dateCreated */

    public java.util.Date getDateCreated(){

        return dateCreated;

    }

    /** return a string representation of the object */

    public String toString(){

        return "Geometry created on " + dateCreated + "\ncolor: " + color + "\nfilled is: " + filled;

    }

}

public class Circle extends Geometry{

private double radius;

public Circle(){

}

public Circle( double radius ){

this.radius = radius;

}

public Circle( double radius, String color, boolean filled ){

super( color, filled );

this.radius = radius;

// setColor( color );

// setFilled( filled );

// this.color = color;

// this.filled = filled;

}

/** return radius */

public double getRadius(){

return radius;

}

/** set a new radius */

public void setRadius( double radius ){

this.radius = radius;

}

/** return area */

public double getArea(){

return Math.PI * radius * radius;

}

/** return perimeter */

public double getPerimeter(){

return 2 * radius * Math.PI;

}

/** return diameter */

public double getDiameter(){

return 2 * radius;

}

/** print circle info */

public void printCircle(){

System.out.println( "The circle is created " + super.getDateCreated() + " and radius is " + radius );

}

/** return a string representation of the object */

public String toString(){

return super.toString() + "\nradius is : " + radius;

}

public String getColorCircle(){

return super.getColor();

}

public boolean equals( Object obj ){

if( obj instanceof Circle ){

return radius == ((Circle)obj).radius;

}

else

return false;

}

}


public class Rectangle extends Geometry{

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 ){

super( color, filled );

this.width = width;

this.height = height;

}

/** return width */

public double getWidth(){

return width;

}

/** set a new width */

public void setWidth(double width){

this.width = width;

}

/** get height */

public double getHeight(){

return height;

}

/** set a new height */

public void setHeight( double height ){

this.height = height;

}

/** return area */

public double getArea(){

return width * height;

}

/** return perimeter */

public double getPerimeter(){

return 2 * ( width + height );

}

/** return a string representation of the object */

public String toString(){

return super.toString() + "\nwidth is : " + width + "\nheight is : " + height;

}

}


public class TestGeometry{

public static void main( String[] args ){

Geometry geo = new Geometry();

Rectangle rec = new Rectangle( 4.0, 5.0 );

Circle cir = new Circle( 2.0 );

System.out.println( geo.toString() );

System.out.println( rec.toString() );

System.out.println( rec.getArea() );

System.out.println( rec.getPerimeter()  );

System.out.println( cir.toString() );

System.out.println( cir.getArea() );

System.out.println( cir.getPerimeter() );

}

}


0 回复 有任何疑惑可以回复我~
#1

BonnieLLLL 提问者

你写的都是些什么呀
2017-05-17 回复 有任何疑惑可以回复我~
#2

BonnieLLLL 提问者

你写的都是些什么呀
2017-05-17 回复 有任何疑惑可以回复我~
#3

BonnieLLLL 提问者

能不能不要这样敷衍
2017-05-17 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

第七题怎么写

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号