作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要实现一个 Triangle 类,并且我坚持比较边的长度来确定三角形是否确实是等腰三角形。这是我到目前为止所拥有的:
public class TriangleIsosceles {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public TriangleIsosceles(){
cornerA = new Point(0,0);
cornerB = new Point(10,0);
cornerC = new Point(5,5);
}
public TriangleIsosceles(int x1,int y1,int x2,int y2,int x3,int y3){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public String isIsosceles(String isIsosceles){
return isIsosceles;
}
}
我使用的Point
对象是这样的:
public class Point {
private int x;
private int y;
public Point(){
this(0,0);
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void printPoint(){
System.out.println(x + y);
}
public String toString(){
return "x = "+x+" y = "+y;
}
}
在另一个类 (LineSegment
) 中,我创建了一个方法 length()
来确定两点的距离。看起来像:
public double length() {
double length = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
return length;
}
如何使用此方法来帮助我在 TriangleIsosceles
类中找到三角形的长度?
我知道我需要查看是否(lenghtAB == lengthBC || lengthBC == lenghtCA || lengthAB == lengthCA)
。
最佳答案
一个快速、完全有效的解决方案是使您的长度方法成为静态实用方法,即
public static double length(x1, y1, x2, y2)
{
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
or
public static double length(Point p1, Point p2)
{
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
您还可以将该方法添加到 Point 本身,即在 Point 类中添加:
public double calcDistance(Point otherPoint)
{
return Math.sqrt(Math.pow(this.x - otherPoint.x, 2) + Math.pow(this.y - otherPoint.y, 2));
}
关于java - 实现类,等腰三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947711/
我需要实现一个 Triangle 类,并且我坚持比较边的长度来确定三角形是否确实是等腰三角形。这是我到目前为止所拥有的: public class TriangleIsosceles { pr
我是一名优秀的程序员,十分优秀!