实现点(Point)类
横坐标x和纵坐标y
实现圆(Circle)类和矩形(Rectangle)类
1) 圆(Circle)类包含圆心(Point p)和半径(int r)两个属性
2) 矩形(Rectangle)类包含中心点(Point p)、宽(int w)、高(int h)三个属性
3) 提供合理的构造器
4) 提供方法:
. 计算当前图形的面积getArea(); pi*r*r w*h
. 计算当前图形是否包含(contains(Point p))指定的点;
横坐标x和纵坐标y
实现圆(Circle)类和矩形(Rectangle)类
1) 圆(Circle)类包含圆心(Point p)和半径(int r)两个属性
2) 矩形(Rectangle)类包含中心点(Point p)、宽(int w)、高(int h)三个属性
3) 提供合理的构造器
4) 提供方法:
. 计算当前图形的面积getArea(); pi*r*r w*h
. 计算当前图形是否包含(contains(Point p))指定的点;
. 重载方法 contains(int x, int y) 图形是否包含坐标x,y;
Point类
public class Point {
public int x;
public int y;
public Point(int a,int b) {
this.x = a;
this.y = b;
System.out.println("点的坐标为:" +"(" +x + "," + y + ")");
}
public static void main(String[] args) {
Point point = new Point(5,2);
}
}
Circle类:
public class Circle {
Point p;
int r;
/*
public Circle() {
this((new Point(5,6)),5);
}
*/
public Circle(int r) {
this((new Point(0,0)),r);
}
public Circle(Point p,int r) {
this.p = p;
this.r= r;
}
public Circle(int x,int y,int r) {
this((new Point(x,y)),r);
}
public Circle getArea() {
double s = r *r *3.14;
System.out.println("圆的面积为:" + s);
return this;
}
public Circle getZhou() {
double z = r *2 *3.14;
System.out.println("圆的周长为:" + z);
return this;
}
public static void main(String[] args) {
// Circle c = new Circle();
// System.out.println("圆的半径为:" + c.r);
// c.getArea().getZhou();
}
}
Rectangle类:
public class Rectangle {
Point p;
public int w;
public int h;
public Rectangle(int w,int h) {
this(new Point(0, 0),w,h);
}
public Rectangle(Point p,int w,int h){
this.p = p;
this.w = w;
this.h = h;
}
public Rectangle(int a,int b,int w,int h){
this(new Point(a, b),w,h);
}
public void getArea() {
int s = w*h;
System.out.println("矩形的面积为:" + s);
}
public void contains(double x,double y){
double w2 = this.w / 2;
double h2 = this.h/2;
double t = Math.sqrt((this.w-x)*(this.w-x)+(this.h-y)*(this.h-y));
if(t <= w2 && t<= h2) {
System.out.println("点(" + x + ", " + y + ")包含在图形内!");
}else {
System.out.println("点(" + x + ", " + y + ")不包含在图形内!");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Rectangle re = new Rectangle(new Point(2, 2),5, 6);
// re.getArea();
// re.contains(10, 5);
}
}