Java 简单对象和构造函数问题
以下是作业说明: 在本任务中,您将向APRectangle类添加四个以上的方法,并定义一个静态方法,该方法生成一个报告矩形定义特征的字符串 前三个方法——GetToRight、getBottomLeft和getBottomRight——与访问器方法getTopLeft一起返回表示矩形四个角的指定对象。在定义这三个新方法时,请记住Java图形窗口中的位置是相对于窗口的左上角描述的。因此,位置在图形窗口中越靠右,其x坐标越大。出乎意料的是,图形窗口中的位置越低,其y坐标就越大。这意味着矩形的底角的y坐标将大于顶角 第四种方法是shrink,它接受一个参数,即双d,并将矩形的宽度和高度更改为它们以前值的d%。因此,例如,如果在double 62.5上调用APRectangle r的收缩方法,则r的宽度和高度将更改为其先前值的0.625 最后,静态方法printAPRectangle是这样的:当它的参数是APRectangle,其左上角是坐标(-5.0,3.6),宽度为7.5,高度为6.3时,它返回字符串 “[投影角(-5.0,3.6)7.5,6.3]” 定义此方法时,请密切注意空间的位置。您可能会发现调用printAssign静态方法以及APRectangle类的所有三个访问器方法很有用 我目前拥有的代码是:Java 简单对象和构造函数问题,java,object,visibility,symbols,Java,Object,Visibility,Symbols,以下是作业说明: 在本任务中,您将向APRectangle类添加四个以上的方法,并定义一个静态方法,该方法生成一个报告矩形定义特征的字符串 前三个方法——GetToRight、getBottomLeft和getBottomRight——与访问器方法getTopLeft一起返回表示矩形四个角的指定对象。在定义这三个新方法时,请记住Java图形窗口中的位置是相对于窗口的左上角描述的。因此,位置在图形窗口中越靠右,其x坐标越大。出乎意料的是,图形窗口中的位置越低,其y坐标就越大。这意味着矩形的底角的y
public class APRectangle
{
private APPoint myTopLeft;
private double myWidth;
private double myHeight;
public APRectangle( APPoint topLeft, double width, double height )
{
// Code for the body of this constructor is hidden
}
/*
* Code for the accessor methods getTopLeft, getWidth, and getHeight and
* the modifier methods setTopLeft, setWidth, and setHeight is hidden
*/
public String getTopRight()
{
APPoint myTopRight = new APPoint( myWidth + myTopLeft.getX(), myTopLeft.getY() );
return myTopRight.printAPPoint();
}
public String getBottomLeft()
{
APPoint myBottomLeft = new APPoint( myTopLeft.getX(), myTopLeft.getY()- myWidth );
return myBottomLeft.printAPPoint();
}
public String getBottomRight()
{
APPoint myBottomRight = new APPoint( myWidth + myTopLeft.getX(), myTopLeft.getY()- myWidth );
return myBottomRight.printAPPoint();
}
public double shrink(double d)
{
myWidth *= (d / 100.0);
myHeight *= (d / 100.0);
}
// Definitions of the APPoint class and the static method printAPPoint are hidden
public String printAPRectangle()
{
return "[APRectangle " + getMyTopLeft() + " " + getMyWidth() + "," + getMyHeight() + "]" ;
}
public static void main( String[] args )
{
APRectangle r = new APRectangle( new APPoint( 25, 50 ), 30, 15 );
System.out.println( printAPRectangle( r ) );
System.out.println( "top right: " + printAPPoint( r.getTopRight() ) );
System.out.println( "bottom left: " + printAPPoint( r.getBottomLeft() ) );
System.out.println( "bottom right: " + printAPPoint( r.getBottomRight() ) );
r.shrink( 80 );
System.out.println( "shrunk to 80%: " + printAPRectangle( r ) );
}
我一直收到这个错误:
TC1.java:11 error: cannot find symbol
return "[APRectangle " + getMyTopLeft() + " " + getMyWidth() + "," + getMyHeight() + "]" ;
如果有人能帮助我(和其他一些人)找出问题所在,我将不胜感激。谢谢大家! 您尚未定义任何
getMy…
方法。(您有一个getTopLeft()
,但没有getMyTopLeft()
等。)
由于这是类中的一个方法,您最好只使用字段引用:
myTopLeft
、myWidth
、和myHeight
,您使用的是getMyTopLeft()e.t.c,但您的方法名为getTopLeft()…您甚至在主体中为一个角度定义getMyTopLeft()getMyWidth()或getMyHeight(),评论说它是隐藏的,所以我需要重写它的所有代码吗?因为我们使用的网站不应该要求这样。很抱歉,我现在不能去编辑它,因为McClass课程已经结束,但我会尽快开始。@Hitmarcusse-我不理解你的评论。错误是由于指示的代码行引用了未定义的方法。注释名称与错误行中使用的方法名称不匹配的方法。只需编辑带有错误的行以使用实际的方法名称(getWidth()
而不是getMyWidth()
等)。