All of you are now comfortable creating objects from existing classes and calling capabilities on them. In this lab, you will use your new found skills to do fun things with turtles. Turtles are very smart and know how to move in all sorts of ways. They can even turn angles.
This lab has to be done using the interaction pane. Make sure that you save your interaction pane history by right-clicking on the interaction pane and choosing the save history option. You will submit this history and a screen shot of your turtle drawing. You will have to set classpath before you can use the World and Turtle classes. More details on this coming later.
Create a new World object and populate it with a few turtle. Then call operations on those turtles to draw nifty graphics. The best turtle drawing will be put up on the class website and the winner will be given a small prize.
Please make sure that your drawings don't look the same. If they do, you may get into a lot of trouble.
Example turtle code.
World w = new World();
Turtle t1 = new Turtle(100, 100, w);
Turtle t2 = new Turtle(50, 50, w);
t2.setHeading(150); // makes t2 face 150 degrees North
t2.forward(200); // moving forward will draw a slanted line
The above code will put two turtles in the world. The capability calls on t1 will draw a square. t2 will draw a slanted line.
Create a new project by clicking on Project menu and then clicking on new. Make sure that you save your Project in the Turtle directory, i.e. the directory in which you extracted the Turtle files available on the course website.
ClassPath tells JDK tools and applications where to find third-party and user-defined classes i.e. classes that are not Java extensions or part of the Java platform. You only need to set the class path when you want to load a class that's not in the current directory or in any of its subdirectories, and not in a location specified by the extensions mechanism.
DrJava had made this thing very simple. In DrJava there is an option named Project Properties to set and remove the classpath in Project menu.
When we click this option, another form will open. In this form you will find various kinds of options available to set the classpath and to remove the existing classpath.
Now to add classpath of an existing class click Add button.
Now add the World class by giving it's complete path (for e.g. D:\turtle\World.class) and click OK. Again click on the add button and add the Turtle class the same way. Also make sure that the Build Directory, Project Root and Working Directory shown above point to the Turtle dir, which in our example case is D:\turtle. Now this class is available i.e. we can instantiate this class and use all its functionality. Not only this class is available to us but any class which is in this folder is accessible. We can instantiate it and use it.
Alternate way of setting ClassPath (if the above doesn't work for you)
The class path can be set by setting the CLASSPATH environment variable. Click on Start Menu button and then click on Run. In the Run dialog box, type “cmd” with the quotes and hit enter. You will see a black window similar to the one shown below. Type the command as shown but change d:\\package\\beans.jar to the directory you have extracted the Turtle files in. The Turtle files are available on the course website. If you extract your files in d:\turtle, the command will be,
CLASSPATH has to be in caps.
C :> set CLASSPATH=classpath1; classpath2...
Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
· For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
· For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
· For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
Following Error will occur if we don’t set the classpath.
But after setting class path
Make a project by clicking Project Menu and selecting ‘New’ option. Enter a name for your project and click Save. DrJava will display Project Properties window, set classpath to World.class and Turtle.class as shown in the start of the tutorial.
1. Instantiate World class. Now make two objects named sqr, rect of Turtle class. Now draw square and, rectangle using these turtles. For square take x=200 and for rectangle length=180 and width=90.
By entering the code:
World world= new World();
Turtle turtle1= new Turtle(10,10,world);
Turtle turtle2= new Turtle(50,50,world);
Turtle turtle3= new Turtle(77,77,world);
Following Window is displayed
The final result should be something like this
2. Write the first three letters of your first name with three different turtles?