设计Circle,Rectangle,Geometry类三个类有不同的属性,该怎么去设计 问题看简介
设计Circle,Rectangle,Geometry类,Circle类有属性x,y,radius。Rectangle类有左上顶点坐标(x,y),width,height。Geometry类将Circle,Rectangle类组合,设计getDistance函数返回圆与矩形中心的距离
这里计算的是圆心到矩形中心的距离,示例代码如下:
public class Cricle {
private double x;
private double y;
private double radius;
public Cricle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getRadius() {
return radius;
}
}
public class Rectangle {
private double x;
private double y;
private double width;
private double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
public class Geometry {
private Cricle cricle;
private Rectangle rectangle;
public Geometry(Cricle cricle, Rectangle rectangle) {
this.cricle = cricle;
this.rectangle = rectangle;
}
/**
* 圆心到矩形中心的距离
*
* @return
*/
public double getDistance() {
double diffX = Math.abs(this.cricle.getX() - (this.rectangle.getX() + this.rectangle.getWidth() / 2));
double diffY = Math.abs(this.cricle.getY() - (this.rectangle.getY() + this.rectangle.getHeight() / 2));
// 勾股定理
return Math.sqrt(diffX * diffX + diffY * diffY);
}
}
测试代码如下:
public class Test {
public static void main(String[] args) {
Cricle cricle = new Cricle(0,0,5);
Rectangle rectangle=new Rectangle(0,0,6,8);
Geometry geometry=new Geometry(cricle,rectangle);
// 5.0
System.out.println(geometry.getDistance());
}
}
Geometry定义为父类,Rectangle和Circle是Geometry的子类
简介呢
参考jdk 集合的设计思想 (List, ArrayList, LinkedList)
可以将Geometry 设计为接口,里面定义几何的共有行为。比如面积 , 周长 等
Circle 和 Rectangle 作为Geometry 的实现类
设计Circle,Rectangle,Geometry类,Circle类有属性x,y,radius。Rectangle类有左上顶点坐标(x,y),width,height。Geometry类将Circle,Rectangle类组合,设计getDistance函数返回圆与矩形中心的距离 先给你吧你的问题复制下来.都不写完全,怎么给你做
可以将Geometry定义为抽象类,声明一些属性抽象方法,比如面积、周长等,将Circle 和 Rectangle声明为普通类,继承Geometry类并实现其方法,示例代码如下:
public abstract class Geometry {
public double area;//面积
public double perimeter;//周长
public abstract double scaleArea();//计算面积
public abstract double scalePerimeter();//计算周长
}
public class Circle extends Geometry {
private double R;
@Override
public double scaleArea() {
return Math.PI * R * R;
}
@Override
public double scalePerimeter() {
return 2 * Math.PI * R;
}
}
public class Rectangle extends Geometry {
private double lenth;
private double width;
@Override
public double scaleArea() {
return lenth * width;
}
@Override
public double scalePerimeter() {
return 2 * (lenth + width);
}
}
望采纳
三个类的设计如下:
/**
* 如果坐标x,y需要小数,把int改成double即可
*/
public class Answer7668231 {
/**
* 测试代码如下
*/
public static void main(String... args) {
// 构造一个圆形
Circle circle = new Circle(3, 1, 7);
// 构造一个长方形
Rectangle rectangle = new Rectangle(4, -1, 4, 4);
// 构造组合图形
Geometry geometry = new Geometry(circle, rectangle);
// 计算圆心与长方形中心点距离
System.out.println(geometry.getDistance());
}
}
/**
* 圆形
*/
class Circle {
int x;
int y;
int radius;
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
}
/**
* 长方形
*/
class Rectangle {
int x;
int y;
int width;
int height;
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
/**
* 组合后的几何图形
*/
class Geometry {
private Circle circle;
private Rectangle rectangle;
public Geometry(Circle circle, Rectangle rectangle) {
this.circle = circle;
this.rectangle = rectangle;
}
/**
* 计算圆与矩形中心的距离
*/
public double getDistance() {
// 第1步:计算长方形中心点坐标
// 长方形中心点x坐标
int rectangleCenterX = rectangle.x + (rectangle.width / 2);
// 长方形中心点y坐标
int rectangleCenterY = rectangle.y - (rectangle.height / 2);
// 第2步: 计算圆心与长方形中心点x和y的距离
int distanceX = Math.abs(circle.x - rectangleCenterX);
int distanceY = Math.abs(circle.y - rectangleCenterY);
// 第3步:根据勾股定理计算三角形斜边的长度,这个斜边的长度就是圆心与长方形中心点的距离
// Math.pow(distanceX, 2)表示distanceX的平方
// Math.sqrt求平方根
double distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
return distance;
}
}
请采纳,十分感谢!