Java:定義一個未知點,判斷未知點是否在圓內,如果在圓內,輸出true,否則輸出false,
- 題目要求
- 設計思路
- 代碼
- 點類
- 圓類
- 圓類方法
- 運行結果
題目要求

設計思路
設計思路根據題目要求:
1.設計一個類代表二維空間的一個點;
2.設計一個類代表二維空間的一個圓;
3.計算點與圓的關系,
代碼
點類
package cricle;
public class Point {//定義圓心
private double x;
private double y;
public Point(double x,double y) {//定義一個未知點坐標
this.x=x;
this.y=y;
}
public double getx() {
return x;
}
public void setx(double x) {
this.x=x;
}
public double gety() {
return y;
}
public void sety(double y) {
this.y=y;
}
public double distance(Point dot) {//點到圓心的距離公式
return Math.sqrt(Math.pow(x-dot.x, 2))+Math.pow(y-dot.y, 2);
}
}
圓類
package cricle;
public class Cricle {
private double radius;//圓的半徑
private Point center;//圓心
public Cricle(double r) {
radius=r;
}
public double area(){//計算面積的方法
return Math.PI*radius*radius;
}
public Cricle(double radius,Point center) {
this.radius=radius;
this.center=center;
}
public double girth() {//計算周長的方法
return 2*Math.PI*radius;
}
public double getRadius() {
return radius;
}
public double setRadius(double radius) {
return this.radius=radius;
}
public Point getCenter() {
return center;
}
public Point setCenter(Point center) {
return this.center=center;
}
public boolean isCricleIn(Point posion) {//判斷點是否在圓內
if(center.distance(posion)<=radius)
return true;
else
return false;
}
}
圓類方法
package cricle;
public class TestCricle {
public static void main(String[] args) {
Cricle cl=new Cricle(2,new Point(1,1)) ;//確定半徑和圓心
Point pl=new Point(2,2);//確定未知點的坐標
boolean bl=cl.isCricleIn(pl);
System.out.println(bl);
}
}
圓心和圓的半徑,以及未知點的坐標可以在方法類中修改,
運行結果


如果寫得不清楚的歡迎評論區交流;有可以優化的地方歡迎大佬指教指教,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/208916.html
標籤:其他
