課程
/后端開發
/Java
/Java入門第二季 升級版
求解答
2017-05-17
源自:Java入門第二季 升級版 10-1
正在回答
//類 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); }
慕設計6383936
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 ){
? ? ? ? this.color = color;
? ? ? ? this.filled = filled;
? ? /** return color */
? ? public String getColor(){
? ? ? ? return color;
? ? /** set a new color */
? ? public void setColor( String color ){
? ? /** return filled */
? ? public boolean isFilled(){
? ? ? ? return filled;
? ? /** set a new filled */
? ? public void setFilled( boolean 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 );
// 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 ){
/** 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 ){
/** return width */
public double getWidth(){
return width;
/** set a new width */
public void setWidth(double width){
/** get height */
public double getHeight(){
return height;
/** set a new height */
public void setHeight( double height ){
return width * height;
return 2 * ( width + height );
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() );
BonnieLLLL 提問者
舉報
課程升級!以終為始告別枯燥,在開發和重構中體會Java面向對象編程的奧妙
7 回答感覺寫了一萬年才寫出來,寫的亂七八糟,哎...
2 回答怎么開始寫?。肯葘懯裁??在寫什么?
2 回答這里講的構造方法 和 第一季第七章的方法是同一種的方法嗎?
2 回答請問第二季 第三季學完了怎么辦 ? 之后一個怎么學 路線 ?
1 回答這個怎么寫啊
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-05-17
2017-05-17
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() );
}
}