Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单对象问题_Java_Object - Fatal编程技术网

Java 简单对象问题

Java 简单对象问题,java,object,Java,Object,以下是分配的说明:在本任务中,向APRectangle类添加四个以上的方法,并定义一个静态方法,该方法生成一个报告矩形定义特征的字符串 前三个方法——GetToRight、getBottomLeft和getBottomRight——与访问器方法getTopLeft一起返回表示矩形四个角的指定对象。在定义这三个新方法时,请记住Java图形窗口中的位置是相对于窗口的左上角描述的。因此,位置在图形窗口中越靠右,其x坐标越大。出乎意料的是,图形窗口中的位置越低,其y坐标就越大。这意味着矩形的底角的y坐标

以下是分配的说明:在本任务中,向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类的所有三个访问器方法很有用

我目前拥有的代码是:

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

                                ^ 
如果有人能解释一下我错过了什么,那将非常有帮助,谢谢

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

这个方法是否也返回字符串?如果不是,您将得到一个错误。

即使您的定义不接受任何参数,您仍在传递
printAPRectangel(r)
?。。。