所以在我的在线课堂上,我必须做一些与矩形有关的事情。我对如何编辑我的主类使其使用另一个类的方法存在一些困惑。
这是项目的链接,因为你不明白我要问什么:
http://pages.eimacs.com/eimacsstatics/download/apjava/project1bj.pdf
我感到困惑的部分是为printAPRectangle添加一个定义,因为我认为我没有正确地做到这一点。
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),
这是我的APRectangle类代码:
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;
}
}这是我的APPoint类:
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; } }
最后这里是我的主类:
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!" );
}
}我不知道如何去做那些问我如何编辑主类的部分,也不知道如何去输出myTopLeft,因为它是APPoint而不是普通字符串。它说我必须使用printAPPoint,但我该如何使用它?
谢谢,
罗汉