2014-12-18 55 views
-2

这些是分配的说明:在此任务中,向APRectangle类添加四个更多的方法,并定义一个生成报告矩形定义特征的字符串的静态方法。简单对象问题

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

第四种方法shrink使用单个参数double d,并将矩形的宽度和高度更改为其以前值的d%。所以,例如,如果在double 62.5上调用一个APRectangle r的缩小方法,那么r的宽度和高度将改变为其以前值的0.625。

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

“[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 getMyTopLeft() 
    { 
     return myTopLeft.printAPPoint(); 
    } 

    public double getMyWidth() 
    { 
     return myWidth; 
    } 

    public double getMyHeight() 
    { 
     return myHeight; 
    } 

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

           ^

如果有人能请解释什么,我缺少这将会是非常有帮助,谢谢!

+0

或者,看看它。 – keyser

+2

即使您的定义不接受任何参数,您正在传递'printAPRectangel(r)'?... – brso05

回答

1

1)您正在创建APRectangle类的新实例,但是您不访问此对象方法。你的主要应该看起来像这样。在不引用对象的情况下调用方法名称只会导致关于静态上下文的错误。

public static void main(String [] args){ 
    APRectangle r = new APRectangle(new APPoint(25, 50), 30, 15); 
    System.out.println(r.printAPRectangle()); 

2)您将参数传递到不带任何参数,在你的主要方法

APRectangle r = new APRectangle(new APPoint(25, 50), 30, 15); 
System.out.println(printAPRectangle(r)); 

3的方法)有一个名为myTopLeft一个指定的对象,在你getMyTopLeft方法,你试图通过调用返回一个字符串

myTopLeft.printAPPoint 

这样做printAPPoint方法返回一个字符串吗?如果不是,你会得到一个错误。