简体   繁体   中英

Coding something about a rectangle (Java)

so in my online class I have to do something related to rectangles. I have some confusion on how to edit my main class to make it use a method from another class.

Here is the link of the project just incase you don't understand what I'm going to ask: http://pages.eimacs.com/eimacsstatics/download/apjava/project1bj.pdf

The part I'm confused about is adding a definition for printAPRectangle, because I don't think I'm doing it correctly.

Add accessor instance methods for the three instance variables, and then click the Compile button on the APRectangle class editor window to compile your code and check for errors.

Reopen the definition of MainClass and insert a definition of the static method printAPRectangle after the definition of printAPPoint. This method should be defined in such a way that, if it is applied to the APRectangle object whose top left corner is the APPoint object with coordinate (-5.0,3.6),

Here is my APRectangle class code:

    public class APRectangle
   {
    private APPoint myTopLeft;
    private double  myWidth;
    private double  myHeight;

    public APRectangle( APPoint topLeft, double width, double height )
    {
        myTopLeft = topLeft;
        myWidth = width;
        myHeight = height;
    }

    public APPoint getTopLeft()
    {
        return myTopLeft;
    }

    public double getWidth()
    {
        return myWidth;
    }

    public double getHeight()
    {
        return myHeight;
    }
}

Here is my APPoint class:

public class APPoint { private double myX; private double myY;

 public APPoint( double x, double y ) { myX = x; myY = y; } public double getX() { return myX; } public void setX( double x ) { myX = x; } public double getY() { return myY; } public void setY( double y ) { myY = y; } } 

and finally here is my Main Class:

public class MainClass
{
 public MainClass()
 {
    }

 public static String printAPPoint( APPoint p )
 {
     return "(" + p.getX() + "," + p.getY() + ")";
    }

 public static String printAPRectangle( APRectangle R)
 {
     return "[APRectangle " + printAPPoint( +
            " " + getWidth() + "," + getHeight() + "]" ;
 }

 public static void main(String[] args)
 {
     APPoint p = new APPoint( 1.0, 2.0 );
     APRectangle R = new APRectangle( q, 7.5, 3.6);
     System.out.println( "p is " + printAPPoint( p ) );
     System.out.println( "Done!" );
  }

}

I don't know how to do the part that asks me how to edit the main class, and also the part where I have to output myTopLeft because it's an APPoint and not a normal string. It says I have to use printAPPoint but how do i use it?

Thanks, Rohan

In order to use APRectangle's methods, you must call them from an APRectangle object. The printAPRectangle method receives an APRectangle object called R. You must use R and call it's member functions. You need to use these:

  • R.getTopLeft()
  • R.getWidth()
  • R.getHeight()

    public static String printAPRectangle( APRectangle R) { return "[APRectangle " + printAPPoint(R.getTopLeft) + " " + R.getWidth() + "," + R.getHeight() + "]" ; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM