Reputation: 1
Okay so im pretty new to programming. I have an assignment for my class and the instructions are specifically:
"code in PolygonArtMaker.java's main code to call your pentagon method on the ArtisticTurtle referred to by tRef, to make your application draw a pentagon when it runs."
From my interpretation this means that when I click Run on Polygon artmaker it will also run Artistic Turtle and draw with methods app....
Ive been trying for awhile and cant figure it out, any help would be appreciated.
Polygon artmaker
public class PolygonArtMaker
{
public static void main(String [] a)
{
World wRef = new World( );
ArtisticTurtle tRef = new ArtisticTurtle ( wRef );
tref.forward( 50 );
}
}
Draw With Methods App
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!");
}
}
Upvotes: 0
Views: 325
Reputation: 1180
So from what I understand you need to call the pentagon function from the PolygonArtMaker main function. To do this you need to first make an instance of the ArtisticTurtle by calling it's constructor (which you have already). Then all you need to do is call the function via the object. In this case you need:
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);
}
}
You have a line with tref. I think you mean tRef. Hope this helps. If you need more guidance let me know :)
Upvotes: 3