1,Point java
package org.jsoft.circle;ide
public class Point {
//Point 的屬性x,y
private float x, y;
//get set的方法
public float getX() {
return x;
}測試
public void setX(float x) {
this.x = x;
}ui
public float getY() {
return y;
}this
public void setY(float y) {
this.y = y;
}
//構造方法
public Point(float x, float y) {
this.x = x;
this.y = y;
}
//顯示輸出 x,y
public void dispaly() {
System.out.print("The point is:" + x + "," + y);
}
}spa
2測試Point對象
package org.jsoft.circle;ci
public class TestPoint {
//引入main方法
public static void main(String[]args){
//創造對象
Point p=new Point(3,4);
//調用dispaly
p.dispaly();
}
}get
2 Rectangleit
package org.jsoft.circle;
public class Rectangle {
//引入類Point 屬性width height
private Point point;
//加入set get 方法
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
private float width;
private float height;
//構造Rectangle 方法 矩形的引入p,width ,height
public Rectangle(Point p, float width, float height) {
this.point = p;
this.width = width;
this.height = height;
}
//構造方法 把Point 具體化到點
public Rectangle(float x, float y, float width, float height) {
point = new Point(0, 0);
point.setX(x);
point.setY(y);
this.width = width;
this.height = height;
}
//構造方法是無用的
public Rectangle(Rectangle r) {
// this.height=r.height;
// this.width=r.width;
}
//構造方法是無用的
public Rectangle() {
}
//產生矩形的面積
public float getArea() {
float a = width * height;
return a;
}
//顯示矩形的長 高
public void display() {
System.out.println("The rectangle's width and height is:"
+ point.getX() + "," + point.getY());
}
}
3 測試Rectangle
package org.jsoft.circle;
public class TestRectangle {
public static void main(String[]args){
//建立對象
Rectangle r=new Rectangle(2,2,2,2);
Point p=new Point(3,3);
//調用對象的方法
r.display();
r.getArea();
System.out.println(r.getArea());
}
}
4 Circle
package org.jsoft.circle;
public class Circle {
//構造類的屬性。引入 Point類
private Point p;
//建立 get set
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public void setPoint(float x,float y){
this.p.setX(x);
p.setY(y);
}
private float radius;
//構造方法 ,無用
public Circle() {
}
//構造圓 有圓心 半徑
public Circle(float x, float y, float r) {
p = new Point(x, y);
p.setX(x);
p.setY(y);
this.radius = r;
}
//構造圓 有Point(圓心) 半徑
public Circle(Point p, float r) {
this.p=p;
this.radius=r;
}
//構造一個新圓
public Circle(Circle c) {
p=new Point(0,0);
this.p=c.p;
this.radius=c.radius;
}
//判斷點與圓心的距離
public boolean isInside(Point point) {
boolean b = false;
float result = (float) Math.sqrt((point.getX() - p.getX())
* (point.getX() - p.getX()) + (point.getY() - p.getY())
* (point.getX() - p.getX()));
if (result < radius) {
b = true;
} else {
b = false;
}
return b;
}
//產生矩形的外拉圓
public Circle outerRect(Rectangle r) {
float x = r.getHeight() / 2 + r.getPoint().getX();
float y = r.getWidth() / 2 + r.getPoint().getY();
float rr = (float) Math.sqrt((r.getHeight() * r.getHeight() + r
.getWidth() * r.getWidth()) / 2);
Circle c = new Circle((float)x,(float) y, rr);
return c;
}
//兩個圓的最小外接圓
public Circle join(Circle c1,Circle c2){
float x=(c1.getP().getX()+c2.getP().getX())/2;
float y=(c1.getP().getY()+c2.getP().getY())/2;
float r=(float) (Math.sqrt((c1.getP().getX()-c2.getP().getX())*(c1.getP().getX()-c2.getP().getX())+(c1.getP().getY()-c2.getP().getY())*(c1.getP().getY()-c2.getP().getY()))+c1.radius+c2.radius)/2;
Circle c=new Circle(x,y,r);
return c;
}
//兩個圓心的距離
public float distance (Circle c1, Circle c2){
float distance=(float) Math.sqrt((c1.getP().getX()-c2.getP().getX())*(c1.getP().getX()-c2.getP().getX())+(c1.getP().getY()-c2.getP().getY())*(c1.getP().getY()-c2.getP().getY()));
return distance;
}
//把輸出圓的屬性轉化
public String toString() {
return "半徑爲" + radius + "點的座標" + p.getX() + "," + p.getY();
}
}
5.圓的測試
import java.util.Scanner;
public class TestCircle {
public static void main(String[] args) {
//建立對象
Point p = new Point(0, 0);
Circle c = new Circle(0, 0, 0);
//調入輸入語句
Scanner sc = new Scanner(System.in);
//進行循環
while (true) {
//輸入命令
System.out.print("請輸入命令:(isInside/outerRect/join/distance/quit)");
String st = sc.next();
//判斷命令是不是isInside
if (st.equals("isInside")) {
System.out.print("請指定圓心的x座標:");
float x = sc.nextFloat();
System.out.print("請指定圓心的y座標:");
float y = sc.nextFloat();
System.out.print("請指定半徑的長度:");
float r= sc.nextFloat();
// c.setRadius(sc.nextFloat());
System.out.print("請輸入測試點的x座標:");
p.setX(sc.nextFloat());
System.out.print("請輸入測試點的y座標:");
p.setY(sc.nextFloat());
// c.setP(p);
c = new Circle(x, y, r);
if (c.isInside(p)) {
System.out.println("isInside的結果 爲:測試點(" + p.getX() + ","
+ p.getY() + ")在你所指定的圓");
} else {
System.out.print("isInside的結果爲:測試點(" + p.getX() + ","
+ p.getY() + ")不在你所指定的圓");
}
}
//判斷命令outerRect
else if (st.equals("outerRect")) {
Rectangle r = new Rectangle();
Circle cc = new Circle();
System.out.print("請輸入矩形的左下角的x座標:");
float x=sc.nextFloat();//r.setPoint(p);
//r.getPoint().setX(sc.nextFloat());
System.out.print("請指定矩形的左下角點的y座標:");
float y=sc.nextFloat();//r.getPoint().setY(sc.nextFloat());
System.out.print("請指定矩形的寬:");
float w=sc.nextFloat();//r.setWidth(sc.nextFloat());
System.out.print("請指定矩形的高:");
float h=sc.nextFloat();//r.setHeight(sc.nextFloat());
r=new Rectangle(x,y,w,h);//r.setPoint(p);
cc.outerRect(r);
System.out.print("outerRect的結果爲:矩形(" + x
+ "," + y + "," + w + ","
+ h + ")的外接圓爲(" + cc.outerRect(r)+ ")");
}
//判斷命令join
else if (st.equals("join")) {
Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();
System.out.print("請輸入圓1的x座標:");
float x1 = sc.nextFloat();
System.out.print("請輸入圓1的y座標:");
float y1 = sc.nextFloat();
System.out.print("請輸入圓2的x座標:");
float x2 = sc.nextFloat();
System.out.print("請輸入圓2的y座標:");
float y2 = sc.nextFloat();
System.out.print("請輸入圓1的半徑:");
float r1 = sc.nextFloat();
System.out.print("請輸入圓2的半徑:");
float r2 = sc.nextFloat();
c1 = new Circle(x1, y1, r1);
c2 = new Circle(x2, y2, r2);
c3.join(c1, c2);
//c.setP(p);
System.out.print(c3.join(c1, c2));
}
//判斷 命令distance
else if (st.equals("distance")) {
Circle c1 = new Circle();
Circle c2 = new Circle();
System.out.print("請輸入圓1的x座標:");
float x1 = sc.nextFloat();
System.out.print("請輸入圓1的y座標:");
float y1 = sc.nextFloat();
System.out.print("請輸入圓2的x座標:");
float x2 = sc.nextFloat();
System.out.print("請輸入圓2的y座標:");
float y2 = sc.nextFloat();
c1 = new Circle(x1, y1, 0);
c2 = new Circle(x2, y2, 0);
System.out.print("兩個圓心的距離是:" + c.distance(c1, c2));
}
//輸入離開
else if (st.equals("quit")) {
System.out.println("結束 ");
break;
} //輸入錯誤 else { System.out.println("對不起,你輸入的命令有誤,請從新輸入。"); } } } }