2014-12-18 52 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方法返回一個字符串嗎?如果不是,你會得到一個錯誤。