在Java中创建计算面积和周长的子类三角形时,某些值的结果不正确
我有一个作业,要求我创建一个子类三角形来扩展GeometricObject。我非常接近于得到正确的解决方案,但是我的周长方法有一个问题。我遇到的问题是,我的程序有时计算正确的周长,有时使用所有3条边的初始1.0值计算周长 以下是说明: Triangle类:您的代码文件夹包含一个名为GeometricObject的类。在不以任何方式修改这个类的情况下,扩展它以创建一个名为Triangle的新类 三角形类包含:在Java中创建计算面积和周长的子类三角形时,某些值的结果不正确,java,inheritance,subclass,Java,Inheritance,Subclass,我有一个作业,要求我创建一个子类三角形来扩展GeometricObject。我非常接近于得到正确的解决方案,但是我的周长方法有一个问题。我遇到的问题是,我的程序有时计算正确的周长,有时使用所有3条边的初始1.0值计算周长 以下是说明: Triangle类:您的代码文件夹包含一个名为GeometricObject的类。在不以任何方式修改这个类的情况下,扩展它以创建一个名为Triangle的新类 三角形类包含: 名为sides的单个字段,由三个双值数组组成,分别表示side1、side2和side
- 名为sides的单个字段,由三个双值数组组成,分别表示side1、side2和side3。必须使用数组;不要使用单个实例变量。每个元素的默认值应为1.0,以表示三角形的三条边
- 创建默认三角形的无参数构造函数:(new Triangle())
- 创建具有指定三条边的三角形的构造函数:(新三角形(1.0、2.0、1.5))
- 第三个构造函数,包含三条边、颜色以及是否填充对象:(新三角形(1.0、2.0、1.5、Color.RED、true))
- 一个名为getArea()的方法,返回三角形的面积。(使用Heron公式在线查找公式。)
- 名为getPermission()的方法
- 一个名为toString()的方法,返回三角形的字符串描述李>
import java.awt.Color;
public class Triangle extends GeometricObject
{
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
//no args constructor.
public Triangle ()
{
}
public Triangle (double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getArea()
{
double s = ( side1 + side2 + side3 ) / 2.0 ;
double area = Math.sqrt ( s * ( s - side1) * (s - side2 ) * (s - side3));
return area;
}
public double getPerimeter()
{
return (side1 + side2 + side3);
}
public Triangle ( double side1 , double side2 , double side3 ,Color color, boolean statement )
{
}
public String toString()
{
return ( "t1.getArea() = " + getArea() + "\nt1.getPerimeter() = " +
getPerimeter() + "\nTriangle: side1 = " +
side1 + ", side2 = " + side2 + ", side3 = " + side3) ;
}
}
这是我从检查测试程序中得到的信息:
import java.awt.Color;
import java.util.Date;
public class GeometricObject
{
private Color color = Color.WHITE;
private boolean filled;
private Date dateCreated;
/**
* Construct a default geometric object.
*/
public GeometricObject()
{
dateCreated = new Date(); // System time.
}
/**
* Constructs a GeometricObject with a given color and filled state.
* @param color the initial color of the object
* @param filled whether the object is filled
*/
public GeometricObject(Color color, boolean filled)
{
this(); // set the date
this.color = color;
this.filled = filled;
}
/**
* Return current color.
* @return current color.
*/
public Color getColor()
{
return color;
}
/**
* Return true if the object is filled.
* @return true if the object is filled.
*/
public boolean isFilled()
{
return filled;
}
/**
* Set a new filled value.
* @param filled the new filled value.
*/
public void setFilled(boolean filled)
{
this.filled = filled;
}
/**
* Get dateCreated.
* @return the date the object was created.
*/
public Date getDateCreated()
{
return dateCreated;
}
/**
* Return a string representation of this object.
*/
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color
+ " and filled: " + filled;
}
}
测试三角形
- 定义了类三角形(0.5)
- 类三角形是公共的(0.5)
- 类三角形扩展几何对象(0.5)
- 定义了三个构造函数。(0.5)
- 已定义默认(无参数)构造函数。(1.0)
- 定义了三角形(双、双、双)构造函数。(3.0)
- 定义了三角形(double、double、double、Color、boolean)构造函数。(1.0)
- 修正简单三角形的周长。(3.0)
- 简单三角形的正确面积。(3.0)
- 边为5.39、3.56、8.44(2.0)的3-arg三角形的正确周长
- 边为10.82、4.36、10.47(2.0)的3-arg三角形的正确面积
- 边为8.41、7.65、9.8(2.0)的3-arg三角形的正确周长
- 边为2.63、4.51、1.41(2.0)的3-arg三角形的正确面积 X边为6.17、9.03、1.64的3-arg三角形的周长不正确 预期:但为:(2.0) X边为2.48、1.86、2.08的3-arg三角形的周长不正确 预期:但为:(2.0)
- 边为4.66、7.65、1.83(3.0)的3-arg三角形的正确toString-------------------------------------------------------------------------- 86%(24.0/28.0测试通过)
import java.awt.Color;
import java.util.Date;
public class GeometricObject
{
private Color color = Color.WHITE;
private boolean filled;
private Date dateCreated;
/**
* Construct a default geometric object.
*/
public GeometricObject()
{
dateCreated = new Date(); // System time.
}
/**
* Constructs a GeometricObject with a given color and filled state.
* @param color the initial color of the object
* @param filled whether the object is filled
*/
public GeometricObject(Color color, boolean filled)
{
this(); // set the date
this.color = color;
this.filled = filled;
}
/**
* Return current color.
* @return current color.
*/
public Color getColor()
{
return color;
}
/**
* Return true if the object is filled.
* @return true if the object is filled.
*/
public boolean isFilled()
{
return filled;
}
/**
* Set a new filled value.
* @param filled the new filled value.
*/
public void setFilled(boolean filled)
{
this.filled = filled;
}
/**
* Get dateCreated.
* @return the date the object was created.
*/
public Date getDateCreated()
{
return dateCreated;
}
/**
* Return a string representation of this object.
*/
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color
+ " and filled: " + filled;
}
}
我知道这是一个很长的帖子,我会非常感谢你的帮助,并提前感谢你的时间和帮助 嗨,你们到底面临着什么问题?你能给出给定输入和预期输出的例子吗?你没有为你的5参数构造函数定义任何代码-这就是为什么你得到默认值你有一个空的构造函数<代码>公共三角形(双面1,双面2,双面3,彩色,布尔语句){}对于您得到的计算错误(
是:
),我想您调用它而不是no-empty构造函数。请将此问题简化为一个-我怀疑这样做时,您会发现其他评论者已经提到的问题。我知道测试输出说它正在调用3-arg构造函数,但几乎可以肯定的是,事实并非如此。对于失败的情况,它似乎调用了5-arg构造函数。所以为了让它工作,你最好在5-arg构造函数中编写一些代码。嗨,你到底面临着什么问题?你能给出给定输入和预期输出的例子吗?你没有为你的5参数构造函数定义任何代码-这就是为什么你得到默认值你有一个空的构造函数<代码>公共三角形(双面1,双面2,双面3,彩色,布尔语句){}对于您得到的计算错误(是:
),我想您调用它而不是no-empty构造函数。请将此问题简化为一个-我怀疑这样做时,您会发现其他评论者已经提到的问题。我知道测试输出说它正在调用3-arg构造函数,但几乎可以肯定的是,事实并非如此。对于失败的情况,它似乎调用了5-arg构造函数。因此,为了使其工作,您最好在5-arg构造函数中编写一些代码。