0

好的,我对编程很陌生。我的班级有一个作业,具体说明是:
“PolygonArtMaker.java 的主代码中的代码,用于在 tRef 引用的 ArtisticTurtle 上调用您的五边形方法,以使您的应用程序在运行时绘制一个五边形。”

根据我的解释,这意味着当我在 Polygon artmaker 上单击 Run 时,它还将运行 Artistic Turtle 并使用方法应用程序绘制...。

我已经尝试了一段时间,但无法弄清楚,任何帮助将不胜感激。

多边形美术师

public class PolygonArtMaker
{
   public static void main(String [] a)
   {
     World wRef = new World( );
     ArtisticTurtle tRef = new ArtisticTurtle ( wRef );
     tref.forward( 50 );
   }
}

使用方法应用程序绘制

public class DrawWithMethodsApp
{
  public static void main(String[] a)
  {
    System.out.println("Hello from main!");
    World wref = new World();
    ArtisticTurtle tref = new ArtisticTurtle( wref );

    tref.setPenColor( java.awt.Color.RED );
    tref.pentagon( 2 );
    tref.penUp();
    tref.moveTo( 50,463);
    tref.penDown();
    tref.pentagon( 5 );
    tref.penUp();
    tref.moveTo(350,89);
    tref.penDown();
    tref.pentagon( 8 );
    tref.penUp();
    tref.moveTo( 100, 260);
    tref.penDown();
    tref.hexagon( 5 );
    tref.penUp();
    tref.moveTo( 500, 110 );
    tref.penDown();
    tref.hexagon( 7 );
    tref.penUp();
    tref.moveTo( 360, 310 );
    tref.penDown();
    tref.hexagon( 9 );



**Artistic Turtle **


public class ArtisticTurtle extends Turtle
{
  public void hook( int numberParam )
  {
    System.out.println( "The hook method has been called on ArtisticTurtle "
                         +
                         this
                         +
                         "with parameter value equal to "
                         +
                         numberParam
                      );

  }
  public void pentagon( int numberParam )
  {
    System.out.println
      ("pentagon called with param "
         + numberParam);
    int curLen;
    curLen = (numberParam) ;
    this.forward ( 10*curLen );
    this.turn( 360.0/5 );
    this.forward( 10*curLen );
    this.turn( 360.0/5 );
    this.forward( 10*curLen);
    this.turn( 360.0/5 );
    this.forward( 10*curLen );
    this.turn( 360.0/5 );
    this.forward( 10*curLen );
  }    
 public void hexagon( int numberParam) 
 {
   System.out.println 
     ("hexagon called with param " 
        + numberParam);
   int curLen;
   curLen = (numberParam) ;
    this.forward( 10*curLen );
    this.turn( 60 );
    this.forward( 10*curLen);
    this.turn( 60 );
    this.forward( 10*curLen);
    this.turn( 60 );
    this.forward( 10*curLen);
    this.turn( 60 );
    this.forward( 10*curLen);
    this.turn( 60 );
    this.forward( 10*curLen );


 }








  public ArtisticTurtle( World wrefParam )
  {
    super( wrefParam );
  }
  public static void main(String[] a)
  {
    System.out.println("DONT RUN ArtisticTurtle!!");
    System.out.println("Select DrawWithMethodsApp");
    System.out.println("and RUN that!");
  }
}
4

1 回答 1

3

因此,据我了解,您需要从 PolygonArtMaker 主函数调用五边形函数。为此,您需要首先通过调用它的构造函数(您已经拥有)来创建 ArtisticTurtle 的实例。然后你需要做的就是通过对象调用函数。在这种情况下,您需要:

public class PolygonArtMaker{
   public static void main(String [] args){
        World wRef = new World( );
        ArtisticTurtle tRef = new ArtisticTurtle ( wRef );
        // this is just some random number I chose, not sure where you are supposed to get this value.
        int num = 7;
        // here is where the magic happens - call the function in the other class 
        tRef.pentagon(num);
   }
}

你有一条线与tref。我想你的意思是 tRef。希望这可以帮助。如果您需要更多指导,请告诉我:)

于 2013-09-25T23:19:28.030 回答