一、题目描述
运用面向对象的抽象和封装理论,对二维平面坐标系中的点进行抽象,采用直角坐标系的视角,类名为RPoint,该类对象应该具有双精度类型的x、y坐标值,提供相应的构造方法和getter方法和setter方法,给出有意义的equals方法、toString方法实现,运用方法重载理论给出至少两个用于计算该点到另一点的距离的distance方法,无形参代表到原点的距离,有形参代表到形参点对象的距离。
重新抽象二维平面坐标系中的点,这次采用极坐标模式,类名为PPoint,该类对象具有双精度类型的θ和r坐标值,提供相应的构造方法(至少两个)和getter方法和setter方法, 给出有意义的equals方法、hashcode方法、toString方法实现,并提供一个对象层次的转换到直角坐标系的方法convertToRPoint(),也需要提供计算两点间距离的distance方法(建议直接调用直角坐标系中的对应方法)。并且给前题中的类RPoint添加一个convertToPPoint()方法, 把一个直角坐标系的点转换为极坐标的点。
在前面练习的基础上,试将平面上的圆抽象为类,公有类名为Circle,请采用合适的视角和逻辑进行抽象和封装,成员变量和成员方法参考前面的题目;另外还需要在对象中提供两个方法,一个方法传入一个点对象,判断该圆和点对象位置关系(即圆外、圆上、圆内);另一一个方法传入一个圆对象,判断两圆之间的关系(发挥自己主观能动性抽象吧)。
二、思路
实现RPoint类
1.因为需要运用封装的思想,所以所有的成员变量都需要变成私有。
2.两个构造方法的设计
3.自动生成get和set方法,equals方法
4.写成toString方法
5.实现方法时,为了实现方法重载,要考虑传入的形参和返回值的类型问题
6.而且与PPoint类之间的关系,ConvertToPPoint方法的设计
实现PPoint类
1.成员变量都需要变成私有。
2.两个构造方法的设计
3.自动生成get和set方法,equals方法
4.写成toString方法
5.实现方法时,利用RPoint类的方法实现
实现Circle类
1、实现的成员方法和成员变量参考前面的类,圆心利用RPoint类实现
2、判断点和圆之间的关系,依据点和圆心的距离实现,可以利用前面类的方法
3、判断两圆之间的关系,依据圆心之间的距离实现,可以利用前面类的方法
三、代码
RPoint类
package project;
public class RPoint {
//实现成员变量两个坐标的私有
private double x, y;
//无参构造方法
public RPoint() {
//调用父类方法实现
super();
}
//带参构造方法,传入两坐标的值
public RPoint(double x, double y) {
//调用父类构造方法
super();
this.x = x;
this.y = y;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString() 返回两个点的坐标
*/
@Override
public String toString() {
return "RPoint [x=" + x + ", y=" + y + "]";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode() 设置hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object) 设置equals方法
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof RPoint)) {
return false;
}
RPoint other = (RPoint) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
return false;
}
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) {
return false;
}
return true;
}
/**
* @return the x 获取横坐标
*/
public double getX() {
return x;
}
/**
* @param x the x to set 设置横坐标
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y 获取纵坐标
*/
public double getY() {
return y;
}
/**
* @param y the y to set 设置纵坐标
*/
public void setY(double y) {
this.y = y;
}
/**
* 实现对PPoint的转化
* @return PPoint 放回PPoint类
*/
public PPoint convertToPPoint() {
//将直角坐标系的值转化成极坐标的值
double r=(x*x+y*y);
double θ=Math.asin(y/r);
//生成PPoint类
return new PPoint(θ,r);
}
/**
* 实现计算两点之间的距离
* @param RPoint another 传入另一点
* @return double 返回两点之间的距离
*/
public double distance(RPoint another) {
//检测传入值得有效性
if (another == null)
return 0;
//返回计算的值
return Math.sqrt((this.x - another.x) * (x - another.x) + (y - another.y) * (y - another.y));
}
/**
* 实现计算两点之间的距离(方法重载)
* @return double 返回与原点之间的距离
*/
public double distance() {
return distance(new RPoint(0, 0));
}
}
PPoint类
package project;
public class PPoint {
//私有成员变量,半径和角度
private double θ, r;
//无参构造方法
public PPoint() {
super();
}
//带参构造方法
public PPoint(double θ, double r) {
super();
this.θ = θ;
this.r = r;
}
/* (non-Javadoc)
* @see java.lang.Object#toString() 自动生成
*/
@Override
public String toString() {
return "PPoint [θ=" + θ + ", r=" + r + "]";
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode() 自动生成
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(r);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(θ);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object) 自动生成
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof PPoint)) {
return false;
}
PPoint other = (PPoint) obj;
if (Double.doubleToLongBits(r) != Double.doubleToLongBits(other.r)) {
return false;
}
if (Double.doubleToLongBits(θ) != Double.doubleToLongBits(other.θ)) {
return false;
}
return true;
}
/**
* @return the θ 获取角度值
*/
public double getΘ() {
return θ;
}
/**
* @param θ the θ to set 设置角度值
*/
public void setΘ(double θ) {
this.θ = θ;
}
/**
* @return the r 获取半径值
*/
public double getR() {
return r;
}
/**
* @param r the r to set 设置半径值
*/
public void setR(double r) {
this.r = r;
}
/**
* 实现对直角坐标系的转化
* @return RPoint类
*/
public RPoint convertToRPoint() {
//实现对坐标的转化
double x=r*Math.cos(θ);
double y=r*Math.sin(θ);
return new RPoint(x,y);
}
/**
* 实现计算两点之间的距离
* @param PPoint another 传入另一点
* @return double 返回两点之间的距离
*/
public double distance(PPoint obj) {
//内层方法的实现依靠RPoint类的距离方法
return this.convertToRPoint().distance(obj.convertToRPoint());
}
/**
* 实现计算两点之间的距离(方法重载)
* @return double 返回与原点之间的距离
*/
public double distance() {
//利用上一个方法实现
return this.distance(new PPoint(0, 0));
}
}
Circle类
package project;
public class Circle {
//私有成员变量,圆心利用RPoint类实现
private RPoint center;
//圆的半径
private double r;
//无参构造方法
public Circle() {
center = new RPoint();
r = 0;
}
//带参构造方法
public Circle(RPoint center, double r) {
//检测传入参数的有效性
if (center != null)
this.center = center;
else
this.center = new RPoint();
//对于半径的只取正值
if (r < 0)
this.r = -r;
else
this.r = r;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString() 自动生成
*/
@Override
public String toString() {
return "Circle [center=" + center + ", r=" + r + "]";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode() 自动生成
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((center == null) ? 0 : center.hashCode());
long temp;
temp = Double.doubleToLongBits(r);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object) 自动生成
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Circle)) {
return false;
}
Circle other = (Circle) obj;
if (center == null) {
if (other.center != null) {
return false;
}
} else if (!center.equals(other.center)) {
return false;
}
if (Double.doubleToLongBits(r) != Double.doubleToLongBits(other.r)) {
return false;
}
return true;
}
/**
* @return the center 获取圆心
*/
public RPoint getCenter() {
return center;
}
/**
* @param center the center to set 设置圆心
*/
public void setCenter(RPoint center) {
//检测传入参数的有效性
if (center == null) {
this.center = new RPoint();
} else {
this.center = center;
}
}
/**
* @return the r 获取半径
*/
public double getR() {
return r;
}
/**
* @param r the r to set 设置半径
*/
public void setR(double r) {
if (r < 0)
this.r = -r;
else
this.r = r;
}
/**
* 实现传入一个点,判断点和圆的关系
* @param RPoint类 传入比较点的值
* @return int
*/
public int whereIsPoint(RPoint point) {
double d=center.distance(point);
//在圆内返回1,在圆上返回0,在圆外返回-1
if(d>r) return -1;
else if(d<r) return 1;
else return 0;
}
/**
* 判断两圆之间的关系(两圆的半径分别为R和r,且R≥r,圆心距为d:外离d>R+r;外切d=R+r;相交R-r<d<R+r;内切d=R-r;内含d<R-r)
* @param Circle类 传入要比较的圆
* @return int 两圆远离,返回-1,两圆相切返回0,两圆相交返回1,内切返回2,内含返回3,两圆完全重合返回4
*/
public int whereIsCircle(Circle anothercircle) {
//获取两个圆心的距离
double d=center.distance(anothercircle.center);
//两圆重合的情形,同时设置判断的精度值
if(Math.abs(d)<0.000001||Math.abs(r-anothercircle.r)<0.000001) return 4;
//两圆内含
else if(d<Math.abs(r-anothercircle.r)) return 3;
//两圆内切,同时设置判断的精度值
else if(Math.abs(d-Math.abs(r-anothercircle.r))<0.000001) return 2;
//两圆相交
else if(d>Math.abs(r-anothercircle.r)&&d<(r+anothercircle.r)) return 1;
//两圆相切
else if(d==r+anothercircle.r) return 0;
//两圆相离
else return -1;
}
}
四、测试类
RPoint类
public class Lianxi2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
RPoint p1=new RPoint(3,4);
RPoint p2=new RPoint(6,8);
System.out.println("p1="+p1);
System.out.println(p1.distance(p2));
System.out.println(RPoint.getDesigner());
}
}
PPoint类
public class Lianxi3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PPoint p1 = new PPoint(Math.PI/6, 4);
PPoint p2 = new PPoint(Math.PI/3, 8);
System.out.println("p1=" + p1);
System.out.println(p1.distance(p2));
System.out.println(RPoint.getDesigner());
}
}
Circle类
public class Lianxi4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle c=new Circle(new RPoint(4,5),5);
RPoint p=new RPoint(3,4);
System.out.println(c.whereIsPoint(p));
}
}