課程
/后端開發(fā)
/Java
/Java入門第二季 升級(jí)版
求解答
2017-05-17
源自:Java入門第二季 升級(jí)版 10-1
正在回答
//類 public?class?Circle?{ //私有屬性radius private?int?radius; //getter、setter public?int?getRadius(){ return?radius; } public?void?setRadius(int?radius){ this.radius?=?radius; } //構(gòu)造方法 public?Circle()?{}??//無參構(gòu)造 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("兩個(gè)圓相等!"); } } } //主函數(shù)入口 public?static?void?main(String[]?args)?{ //默認(rèn)構(gòu)造?半徑為5 Circle?circle1?=?new?Circle(); circle1.setRadius(5); //帶參構(gòu)造 Circle?circle2?=?new?Circle(8); Circle?circle3?=?new?Circle(10); Circle?circle4?=?new?Circle(5); //比較 circle1.compareCircle(circle2); circle1.compareCircle(circle3); circle1.compareCircle(circle4); }
慕設(shè)計(jì)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 提問者
舉報(bào)
課程升級(jí)!以終為始告別枯燥,在開發(fā)和重構(gòu)中體會(huì)Java面向?qū)ο缶幊痰膴W妙
7 回答感覺寫了一萬年才寫出來,寫的亂七八糟,哎...
2 回答怎么開始寫???先寫什么?在寫什么?
2 回答這里講的構(gòu)造方法 和 第一季第七章的方法是同一種的方法嗎?
2 回答請(qǐng)問第二季 第三季學(xué)完了怎么辦 ? 之后一個(gè)怎么學(xué) 路線 ?
3 回答隨機(jī)數(shù)怎么寫
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
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() );
}
}