例4-17

public class Rectangle{
 private double x,y,width,height;
 public void setX(double x){
  this.x=x;
 }
 public double getX(){
  return x;
 }
 public void setY(double y){
  this.y=y;
 }
 public double getY(){
  return y;
 }
 public void setWidth(double width){
  if(width<=0)
   this.width=0;
  else
   this.width=width;
 }
 public double getWidth(){
  return width;
 }
 public void setHeight(double height){
  if(height<=0)
    height=0;

 

public class Circle{
 private double x,y,radius;
 public void setX(double x){
  this.x=x;
 }
 public double getX(){
  return x;
 }
 public void setY(double y){
  this.y=y;
 }
 public double getY(){
  return y;
 }
 public void setRadius(double radius){
  if(radius<0)
    this.radius=0;
  else
    this.radius=radius;
 }
 public double getRadius(){
  return radius;
 }
}
  else
    this.height=height;
  }
 public double getHeight(){
  return height;
 }
}
 

public class Geometry{
 private Rectangle rect;
 private Circle circle;
 Geometry(Rectangle rect,Circle circle){
  this.rect=rect;
  this.circle=circle;
 }
 public void setCirclePosition(double x,double y){
  circle.setX(x);
  circle.setY(y);
 }
 public void setCircleRadius(double radius){
  circle.setRadius(radius);
 }
 public void setRectanglePosition(double x,double y){
  rect.setX(x);
  rect.setY(y);
 }
 public void setRectangleWidthAndHeight(double w,double h){
  rect.setWidth(w);
  rect.setHeight(h);
 }
 public void showState(){
  double circleX=circle.getX();
  double rectX=rect.getX();
  if(rectX-circleX==circle.getRadius()*2)
    System.out.println("图形中的矩形在圆的右侧");
  if(circleX-rectX==rect.getWidth())
    System.out.println("图形中的矩形在圆的左侧");
   }
  }
  

public class MainClass{
  public static void main(String args[]){
   Rectangle rect1=new Rectangle(),rect2=new Rectangle();
   Circle circle1=new Circle(),circle2=new Circle();
   Geometry geometryOne,geometryTwo;
   geometryOne=new Geometry(rect1,circle1);
   geometryOne.setRectanglePosition(30,40);
   geometryOne.setRectangleWidthAndHeight(120,80);
   geometryOne.setCirclePosition(150,30);
   geometryOne.setCircleRadius(60);
   geometryTwo=new Geometry(rect2,circle2);
   geometryTwo.setRectanglePosition(160,160);
   geometryTwo.setRectangleWidthAndHeight(120,80);
   geometryTwo.setCirclePosition(40,30);
   geometryTwo.setCircleRadius(60);
   geometryOne.showState();
   geometryTwo.showState();
  }
 }
   


  
 

posted @ 2013-03-30 12:50  张欣博同学  阅读(277)  评论(0编辑  收藏  举报