-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4_7.java
More file actions
94 lines (91 loc) · 2.33 KB
/
Copy pathExample4_7.java
File metadata and controls
94 lines (91 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Rectangle{
double x,y,width,height;
//Rectangle(){ }
Rectangle(double x,double y,double width,double height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public void resetX(double a){
x=a;
}
public double getX(){
return x;
}
public void resetY(double b){
y=b;
}
public double getY(){
return y;
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
}
class Circle{
double x,y,radius;
//Circle(){}
Circle(double x,double y,double radius){
this.x=x;
this.y=y;
this.radius=radius;
}
public void resetX(double a){
x=a;
}
public double getX(){
return x;
}
public void resetY(double b){
y=b;
}
public double getY(){
return y;
}
public double getRadius(){
return radius;
}
}
class Geometry{
Rectangle rect;
Circle circle;
Geometry(Rectangle rect, Circle circle){
this.rect=rect;
this.circle=circle;
}
public void setCirclePosition(double x,double y){
circle.resetX(x);
circle.resetY(y);
}
public void setRectanglePosition(double x,double y){
rect.resetX(x);
rect.resetY(y);
}
public void showState(){
double circleX=circle.getX();
double rectX=rect.getX();
if(rectX-rect.getWidth()>=circleX+circle.getRadius())
System.out.println("矩形在圆的右侧");
if(rectX+rect.getWidth()<=circleX-circle.getRadius())
System.out.println("矩形在圆的左侧");
}
}
public class Example4_7 {
public static void main(String args[]){
Rectangle rect=new Rectangle(30,40,120,80);
Circle circle=new Circle(260,30,60);
Geometry geometry;
geometry=new Geometry(rect,circle);
System.out.print("几何图形中圆和矩形的位置关系是:");
geometry.showState();
System.out.println("几何图形重新调整了圆和矩形的位置.");
geometry.setRectanglePosition(220,160);
geometry.setCirclePosition(40,30);
System.out.print("调整后,几何图形中圆和矩形的位置关系是:");
geometry.showState();
}
}