我试图创建一个类CircleClass但我有一些错误,我无法理解。我可以帮助找到错误吗?
import java.util.*;
public final class CircleClass
{
//class fields
private int diameter;
private double area;
/*Default Constructor
*IMPORT none
*EXPORT address of new CircleClass object
*ASSERTION area 0 diameter 0 is default
*/
public CircleClass()
{
area = 0.0;
diameter = 0;
}
/*copy constructor
*IMPORT inCircle (CircleClass)
*EXPORT address of new CircleClass object
*ASSERTION creates an object with an identical object state as the import
*/
public CircleClass(CircleClass inCircleClass)
{
area = inCircleClass.getArea ();
diameter = inCircleClass.getDiameter ();
}
/*alternate constructor
*IMPORT inDiameter (integer)
*EXPORT address of new Circle object
*ASSERTION creates an object if imports are valid or fails otherwise
*/
public CircleClass (int inDiameter)
{
if (validateNumber (inDiameter))
{
diameter = inDiameter;
area = calcArea (diameter);
}
else
{
throw new IllegalArgumentException("invalid value");
}
}
/*SUB setArea
*IMPORT area (real)
*EXPORT none
*ASSERTION sets the area if import is valid
*/
public void setArea (double area)
{
if (area >= 0.0)
{
area = inArea;
}
else
{
throw new IllegalArgumentException("invalid area value");
}
}
/*SUB setDiameter
*IMPORT inDiameter (integer)
*EXPORT none
*ASSERTION sets diater if valid
*/
public void setDiameter (int inDiameter)
{
if ( validateNumber(inDiameter))
{
diameter = inDiameter;
}
else
{
throw new IllegalArgumentException("invalid diameter");
}
}
//ACCESSORS
public int getDiameter ()
{
return diameter;
}
public double getArea ()
{
return area;
}
//SUB equals
//IMPORT diameter
//EXPORT same (boolean)
//ASSERTION same = true if diameter is same as inDiameter
public boolean equals (int diameter)
{
boolean same = false;
if (diameter == inDiameter)
{
same = true;
}
return same;
}
//SUB equals
//IMPORT inObject
//EXPORT same (booleana)
public boolean equals (Object inObject)
{
boolean same = false;
if (inObject instanceof CircleClass)
{
CircleClass inCircle = (CircleClass) inObject;
same = (diameter == inCircle.getDiameter);
}
}
//SUB clone
//IMPORT none
//EXPORT cloneCircleClass (Object)
//ASSERTION returns cloned object of current object
public Object clone ()
{
CircleClass cloneCircleClass;
cloneCircleClass = new CircleClass ( diameter, area);
return cloneCircleClass;
}
//PRIVATE SUB validateNumber
//IMPORT inDiameter
//EXPORT valid
//ASSERTION number should be gtreater or equal to 0
private boolean validateNumber (int inDiameter)
{
return (inDiameter >= 0);
}
//SUB calcArea
//IMPORT inDiameter
//EXPORT area
//ASSERTION calculates area
private boolean calcArea (int diameter)
{
double radius, area;
radius = (double)diameter / 2.0;
area = Math.PI * radius * radius;
return area;
}
}
我得到的错误是boolean不能转换为double
发现符号错误
并没有为circleClass
找到合适的构造函数我正在制作类对象,所以我没有idead最后一个错误的含义。谢谢你的帮助
答案 0 :(得分:0)
您的函数boolean calcArea(int diameter)
返回布尔值。应该只是double
您尚未创建像public CircleClass (int diameter, gouble area) {}
在java中,最好在{
之后写()
(不是从新行开始)
public void setArea(double area) {
if (area >= 0.0) {
this.area = area;
} else {
throw new IllegalArgumentException("invalid area value");
}
}
这个setter函数是正确的。看看你的并纠正别人。
还有更多错误,但请尝试修复这些错误。