您的位置:首页 > 编程语言 > Java开发

《Java语言程序设计基础篇》第8版第11章代码

2016-01-10 11:18 399 查看
程序清单11-1

GeometricObject1.java

package yinchaoTest;

public class GeometricObject1{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/**Construct a default geometric object*/
public GeometricObject1(){
dateCreated = new java.util.Date();
}
/**Construct a geometric object with specified color
* and filled value*/
public GeometricObject1(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. Since filled is boolean,
* its get method is named isFilled*/
public boolean isFilled(){
return filled;
}
/**Set a new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
/**Get dateCreate*/
public java.util.Date getDateCreate(){
return dateCreated;
}
//**Return a String representation of this object*/
public String toString(){
return "create on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}


程序清单11-2

Circle4.java

package yinchaoTest;

public class Circle4 extends GeometricObject1{
private double radius;

public Circle4(){
}

public Circle4(double radius){
this.radius = radius;
}

public Circle4(double radius,String color,boolean filled){
this.radius = radius;
setColor(color);
setFilled(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 radius*radius*Math.PI;
}

/**Return diameter*/
public double getDiameter(){
return 2 * radius;
}

/**Return perimeter*/
public double getPerimeter(){
return 2 * radius * Math.PI;
}
/**Print the circle info*/
public void printCircle(){
System.out.println("The circle is created" + getDateCreate() +
"and the radius is " + radius);
}
}


程序清单11-3

Rectangle1.java

package yinchaoTest;

public class Rectangle1 extends GeometricObject1{
private double width;
private double height;
public Rectangle1(){

}

public Rectangle1(double width, double height){
this.width = width;
this.height = height;
}

public Rectangle1(double width, double height, String color,
boolean filled){
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

/**Return width*/
public double getWidth(){
return width;
}

/**Set a new width*/
public void setWidth(){
this.width = width;
}

/**Return heigth*/
public double getHeigth(){
return height;
}

/**Set a new heigth*/
public void setHeigth(){
this.height = height;
}

/**Return area*/
public double getArea(){
return width * height;
}

/**Return perimeter*/
public double getPerimeter(){
return 2 * (width + height);
}
}


程序清单11-4

TestCircleRectangle.java

package yinchaoTest;

public class TestCircleRectangle {
public static void main(String[] args){
Circle4 circle = new Circle4(1);
System.out.println("A circle " + circle.toString());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());

Rectangle1 rectangle = new Rectangle1(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: