【问题标题】:Simple Objects and Constructor Issue简单对象和构造函数问题
【发布时间】:2015-02-16 08:51:15
【问题描述】:

这些是作业的说明: 在此任务中,您向 APRectangle 类添加了另外四个方法,并定义了一个静态方法,该方法生成一个报告矩形定义特征的字符串。

前三个方法 — getTopRight、getBottomLeft 和 getBottomRight — 与访问器方法 getTopLeft 一起返回代表矩形四个角的 APPoint 对象。在定义这三个新方法时,请记住 Java 图形窗口中的位置是相对于窗口的左上角描述的。因此,在图形窗口中位置越靠右,它的 x 坐标就越大。而且——出乎意料地——一个位置在图形窗口中的位置越低,它的 y 坐标就越大。这意味着矩形的底角将具有比顶角更大的 y 坐标。

第四个方法,shrink,接受一个参数,double d,并将矩形的宽度和高度更改为之前值的 d%。因此,例如,如果在双 62.5 上调用 APRectangle r 的收缩方法,则 r 的宽度和高度将更改为之前值的 0.625。

最后,静态方法printAPRectangle是这样的,当它的参数是APRectangle,其左上角为APPoint,坐标为(-5.0,3.6),宽度为7.5,高度为6.3,返回字符串

"[APRectangle (-5.0,3.6) 7.5,6.3]"

在定义此方法时,请密切注意空格的位置。您可能会发现调用 printAPPPoint 静态方法以及 APRectangle 类的所有三个访问器方法很有用。

我目前拥有的代码是:

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() + "]" ;

如果有人可以帮助我(和其他一些人)找出问题所在,我将不胜感激。谢谢!

【问题讨论】:

  • 您正在使用 getMyTopLeft() 等,但您的方法名为 getTopLeft()...
  • 你在哪里定义 getMyTopLeft() getMyWidth() 或 getMyHeight()?

标签: java object visibility symbols


【解决方案1】:

您尚未定义任何getMy... 方法。 (你有getTopLeft(),但没有getMyTopLeft()等)

由于这是类中的方法,您不妨只使用字段引用:myTopLeftmyWidthmyHeight

【讨论】:

  • 在 APRectangle 的正文中,注释说它是隐藏的,所以我只需要为它重写所有代码吗?因为我们使用的网站不应该这样要求。很抱歉,我现在不能去编辑它,因为 mclass 期已结束,但我会尽快完成。
  • @HitmarCurse - 我不明白你的评论。该错误是由于指定的代码行引用了未定义的方法。注释名称与错误行中使用的方法名称不匹配的方法。只需编辑错误行以使用实际的方法名称(getWidth() 而不是 getMyWidth() 等)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多