亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

正在回答

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
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號