简单对象和构造函数问题

时间:2014-12-17 17:53:51

标签: java object visibility symbols

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

前三个方法 - getTopRight,getBottomLeft和getBottomRight - 与访问器方法getTopLeft一起返回表示矩形四个角的APPoint对象。在定义三种新方法时,请记住,Java图形窗口中的位置是相对于窗口的左上角描述的。因此,图形窗口中右侧的位置越远,其x坐标越大。并且 - 意外地 - 位置在图形窗口中的下方,其y坐标越大。这意味着矩形的底角将具有比顶角更大的y坐标。

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

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

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

在定义此方法时,请密切注意空格的位置。您可能会发现调用printAPPoint静态方法以及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() + "]" ;

如果有人能帮助我(以及其他一些人)找出问题所在,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

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

由于这是类中的方法,因此您也可以使用字段引用:myTopLeftmyWidthmyHeight