Homework #1: Answers to FAQ

This web page contains a collection of questions I've received via e-mail and the answers I've sent. It may help others in their assignment. After this assignment, we'll use either a Wiki or discussion board to post questions and answers.

Drawing in different pen colors

Question: I am trying to be able to choose a color to draw my shapes but I keep getting error messages. I know we talked about it in class but i am for some reason unable to set a color when I am making my methods and even if I am trying to set a pen color in my actual program. I looked at your hatch turtle program and see this is how you chose the color of the shapes, but is there anything else I should try?
spike.drawSquare(outsideSquareLength, java.awt.Color.BLUE);

Answer:In order to change the color of the pen used by the Turtle, you need to make a call to the setPenColor() method on the Turtle itself. The code I wrote for HatchTurtleV2.java does not set the pen color itself, it only passes to the drawSquare() method a color to use and inside of the method is where I call the setPenColor() method on the Turtle object.

You should review HatchTurtle.java for an example of how use of the setPenColor() method. Inside of a Turtle method, to change a Turtle's pen color, call the setPenColor() method like so -- just like you would call other Turtle methods:
this.setPenColor( colorValue );
// where colorValue is the color of the Turtle's pen to use.

Documentation

First a note on documentation, remember that this accounts for 15% of the points in the assignment. I will grade on consistency and adherence to the standards, so be sure that you document your code throughout, indent code consistently, and name variables according to the conventions we've discussed

Question: I am unable to find the link to web page that describes pre-existing methods for turtles. Is there any way you can e-mail me the link?

Answer: The documentation of all the pre-existing Turtle methods is in the Turtle.html file that is inside of the doc directory of bookClasses. So, in the lab computers, these would be in "C:\intro-java-prog\bookClasses\doc\Turtle.html".


Question: Is the way i'm indenting the Java code correct? Paraphrase

Answer: The indenting does not conform to what we've learned though. To do it correctly, every time you start a block of code (i.e. when you have an opening curly brace {), you need to indent the code within it... Here's an example: public class Turtle extends SimpleTurtle
{
  // lots of code here...
  // notice that everything inside the first { is indented by 2 spaces
  // from the left-edge
  /**
   * Method to draw a square with a width and height
   * of 30
   */
  public void drawSquare()
  {
    // every line inside the second { is indented by 4 spaces
    // from the left-edge
    this.turnRight();
    this.forward(30);
    this.turnRight();
    this.forward(30);
    this.turnRight();
    this.forward(30);
    this.turnRight();
    this.forward(30);
  }
} // this } is the end of class Turtle, put all new methods before this

Organizing Java code

Question: It seems like no matter how corrections I try to make to my program more errors keep popping up. I thought I was nearly complete but now I'm not so sure. Can you please point me in the right direction?

Answer: This student sent me his Turtle.java file and I found a serious problem with how he was trying to write his code which prevented him from compiling his code. Below are my recommendations to him.

You need to follow what the authors in the book explain in section 3.5. Be sure to read it in addition to doing the following.

  1. A Java class file (e.g. Turtle.java) is where you define the methods (behavior) of the Class. Thus, this is where you write code for drawSquare(), drawRectangle(), etc. As we did in the lab for the 3 drawSquare() methods whose code is given in the book.
  2. I showed in class that we create a different Java class file that contains a main() method whose code we can then run after compiling it. I've provided several examples of such Java classes, two of them are the HatchTurtle.java and HatchTurtleV2.java that are found posted on my website.

So, where have you written the methods that are required to satisfy step 2 of the assignment? That is, you need the three methods to have the Turtle draw a square -- follow what the authors explain in section 3.5. Then, you need to supply additional Turtle methods to draw a rectangle, another to draw a diamond, another to draw a hexagon, and finally three other methods each drawing different shapes of your choice. All of these methods are put in the Turtle.java class, again as explained in section 3.5

The code that calls the methods described above in order to draw your design is supposed to be in a different file -- one that is similar to my HatchTurtleV2.java file, this is the reason why I distributed the file.

Finally, I highly recommend the following:

  1. Read and follow the instructions given in section 3.5 of the book. This will have you write the three methods to draw squares. This is a required part of the assignment.
  2. Follow the same instructions to write the other methods needed to satisfy step 2 of the assignment.
  3. Test these methods in the interactions pane, by typing code similar to this:
    World pond = new World();
    Turtle pete = new Turtle(pond);

    pete.drawSquare();
    pete.drawSquare2();
    pete.drawSquare(75);
    // etcetera
    Be sure that your methods work as they are supposed to. If they don't, then change your code, compile, test it again, and keep doing that until all of your methods work just right.
  4. Download, compile, and run HatchTurtle.java. Read the code and understand how it works. If you don't understand it, send me e-mail. You can do the same with HatchTurtleV2.java, but to get it to compile you will need to write a drawSquare() method that takes two parameters, the first is the length of the square and the second is the color to use for the pen to draw the square with.
  5. Then... after all this is done, create a file similar to HatchTurtleV2.java to start drawing your own design. Remember to call the methods to draw the design... see question and answer below.


Question: I was doing part 3 of the assignment and I decided to combine part 2 into 3, this way I could call out methods in part 3. I was wondering if I could do this? By the way I did do part 2 just like you asked and I will be submitting that separately.

Answer: The assignment is a single homework -- I structured it into steps only to organize it as an outline of what you are to do. All steps are supposed to be combined. So in step 3, you're supposed to call the methods that you wrote in step two in order to draw your design. This is what is explained with this sentence in the assignment:
"d. Calls methods on each of the three Turtles to create a multifaceted geometrical shapes on the World (or Picture)."

So, you are required to call the methods you created in step 2. Also, another reason to outline the problem into steps is because step 2 involve changes to the file Turtle.java whereas step 3 is to be done on a separate file (similar to my HatchTurtle.java).

Finally, you must do one submission -- do not separate your submissions. It should be a single e-mail message with the two (or three if you include a picture) files attached.


Question: I used setHeading() with two variables because I wanted my turtle to start life on those x and y variables on the 640x480.jpg. Now it's occurring to me that setHeading() may not be used for that.

I'm still not sure where this goes on my program:
FileChooser.setMediaPath("C:/intro-prog-java/mediasources/");
Picture blank = new Picture("640x480.jpg");

Answer: In lab, I explained that the setHeading() method is used to specify the orientation of the Turtle, not to move it. The orientation of the Turtle is specified in degrees with north being 0 degrees, west being -90 degrees, etc. This was explained in last Thursday's lab. Thus, to have the Turtle draw a vertical line, you would these calls:
turtleObject.setHeading(0);
turtleObject.forward();
To move a Turtle to a specific location, you use the moveTo() method. Read the Javadoc documentation of the methods and you'll read what they are for and how they are used -- i.e. that moveTo() takes two values an X and a Y to move the Turtle to the position specified by (X,Y).

As for your other question, have you gotten HatchTurtle.java to work? If you have, then this program shows exactly how to use the FileChooser.setMediaPath() and create a Picture object from a picture there. The program does not show the picture though! To show a picture, you need to invoke its show() method as we've done in the labs many times. Instead, HatchTurtle.java opens a Picture, creates a Turtle inside of it, asks the Turtle to draw on the picture, and finally writes the Picture with all the drawings to a file. So, if you want to see the resulting Picture, you can double-click it from Windows Explorer (or the Finder) to open it up.

Have you created a second Java file to make calls to the Turtle methods? As explained in lab, you need to create a file that is the equivalent to HatchTurtle.java that will open a Picture, create turtles in it, and ask those Turtles to draw on the picture, and finally write its results to a file. Remember, you need to submit at least two files: Turtle.java and a second .java file that is the equivalent of HatchTurtle.java, you choose the name -- as long as the file name matches the name of the class that's declared inside of that file.

Compiling Java code

Question: I was working on my homework and i keep getting this error:
File: BadTurtle.java [line: 34]
Error: cannot access FileChooser
bad class file: FileChooser.class
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.

Answer: This is a sign that you are using the bookClasses code that comes with the CD in the textbook. You should avoid using this code as it is rather old and it's been updated many times since the CD was first created. Instead use the bookClasses code that I've posted on the class web page (ZIP file) -- this is the same as the one in the CECS lab computers.


Question: just encountered another problem when i tried to compile the Turtle.java
1 error found:
File: (no associated file) [line: (no source location)]
Error: No compiler is available.
is there a compiler that i have to download?

Answer: The error indicates that either you have not configured DrJava correctly, that it cannot find the Java JDK compiler, or that you do not have a Java compiler installed in your computer. DrJava is not a compiler, it is an IDE -- Integrated Development Environment. If you don't have a compiler (or you don't know if you do), then you'll need to download one. The class web page has instructions on doing this. It's step 1 titled "Download and install the Java 1.6 JDK" -- read the full instructions before starting the download.


Question: Since i write my code in notepad then copy and paste onto dr. java. How come when i enter it into the interactions, it doesnt always show everything. Because if you look at the code, i created a star as the speaker and a pentagon as the LED indicator. Also is there a way to fill? like if i had the turtle draw a box, can i fill a color on the inside?

Answer: You should be using DrJava for editing your code. As for using the interactions pane, remember that this is a testing area. No code is saved and it is executed right away. Copying/pasting into the pane does not execute every line you paste into it. In class I mention that these are the steps you should be following in this assignment:

  1. editing a file (Turtle.java to define the required methods in step 2 of the assignment) in the definitions pane
  2. saving the file
  3. compiling the source code
  4. fixing errors and repeating this process until the code compiles completely
  5. testing your code in the interactions pane or running your program.

This is what I did in class and how I developed HatchTurtle.java. Note that in step 5, you will either use the interactions pane to test your code or run your program. If you're trying to complete the methods for Turtle.java such as drawHexagon(), once it compiles, you should test it! That's exactly what the interactions pane is great for. To test if it's correct, it's faster and easier to use the interactions pane to create a World with a Turtle in it and then call the drawHexagon() method on it. If it doesn't quite work, then you edit the Turtle.java file to make necessary changes and then test it out once more in the interactions pane. You keep doing this until you get it just right and then move on to writing the code for your next Turtle method. Once all Turtle methods are done, you can move on to creating your designs by writing Java code that creates Turtles and calls the drawing methods on those turtles. This is like what I've done in HatchTurtleV2.java

Regarding, your question about filling a polygon. The drawings that the Turtle makes are just lines, so although they intersect to create polygons, they cannot be filled. We'll learn later how to do that without the use of a Turtle, that's "computer graphics".


Question: I am having a problem programing the turtles I tried to do the same we did at the class here at my computer, but still how ever I do it when ever I compile I still get the same message

"1 error found:
File: (no associated file) [line: (no source location)]
Error: No compiler is available."


I even tried changing the location of the bookclasses place and I made sure to do the location specifying

Answer: It looks like you're missing the Java JDK -- this is the Java Development Kit -- that is required in order to write Java programs. Follow the instructions on the CECS 174 -- Getting Started web page to install everything. The section on "Download and install software on a personal computer" has the steps you need to follow.

Debugging

Question: I try to do this:
  Picture blank = new Picture ("640x480.jpg");
  but this keeps showing up:
  Couldn't load the file 640x480.jpg
  What am I doing wrong?

Answer: You've not setup the mediapath correctly.  I gave an example of how to do this in HatchTurtle.java.  Here is the relevant code from that file:
   // The FileChooser class is used by the Java class in bookClasses
   // to locate files.  The "media path" should point to the folder
   // containing the pictures, e.g. mediasources.  The call to the
   // class method setMediaPath() is how we ask the FileChooser
   // class to record where we have this folder.  Note the use of
   // forward slashes / and that you must supply a / at the end.
    FileChooser.setMediaPath("C:/intro-prog-java/mediasources/");

   // The following will open the file "rose.jpg" that is in the
   // mediasources directory and a Picture object is created in
   // main memory to hold this picture

   Picture rose = new Picture("rose.jpg");


Question: can you please tell me what exactly what I'm suppossed to do with this:
4 errors found:
File: F\Turtle.java [line: 65]
Erroor: ' ; ' expected
 

there are several more like that one in my Compiler Output with the last one going like this:
File: F\Turtle.java [line: 221]
Error: ' } ' expected

Answer: You may want to read the "Common errors" sidebars in the book. e.g.: page. 21, 51, etc. The errors you included in the message seem to be that you've forgotten to end the statement with a semicolon... remember every Java statement ends with a semicolon. The '}' expected error is explained in the sidebar on page 51.  Every opening parenthesis (curly brace) must be matched with a corresponding closing parenthesis (curly brace).

Try fixing these errors.  If you still have problems compiling, then send me the file and I'll help you with it.


Question: I got your email and downloaded the example that you sent. I opened it in Dr Java and try to compile it, but unfortunately its giving me these errors:
"4 errors found:
File: HatchTurtleV2.java [line: 61]
Error: cannot find symbol
symbol  : method drawSquare(int,java.awt.Color)
location: class Turtle
File: HatchTurtleV2.java [line: 80]
Error: cannot find symbol
symbol  : method drawSquare(int,java.awt.Color)
location: class Turtle
File: TurtleArt.java [line: 62]
Error: cannot find symbol
symbol  : method drawSquare(int,java.awt.Color)
location: class Turtle
File: TurtleArt.java [line: 81]
Error: cannot find symbol
symbol  : method drawSquare(int,java.awt.Color)
location: class Turtle"

I can't seem to understand the error message. Please help!

Answer: All the errors have to do with the drawSquare method that takes in an integer as well as a color.  As explained on the class website which links to version 2 of this code:
"To run this, you will need to create drawSquare methods in Turtle.java that match the methods being called from main()."
You need to implement the methods. I have not given these to you since that is part of the assignment.

Finally... it looks like you have two files. HatchTurtleV2.java and TurtleArt.java.  The errors are occurring in both of them, which leads me to believe you've copied/pasted my code into your Java class.  That's the reason why there are 4 error messages, rather than just two.


Question:Can you help me determine what this error message is about?(Paraphrased)
File: /Users/Alvaro/Documents/CSULB/Classes/174Java/LastTurtle2.java [line: 10]
Error: class Turtle is public, should be declared in a file named Turtle.java
This student sent me his file (named LastTurtle2.java) and I tried compiling it, so the error message you see above is the one I see when I try to compile his original file.

Answer: The name of your file is LastTurtle2.java but the class being defined in that file is called Turtle. That's a mismatch. The name of the file must match the name of the class defined within the file. Do not change the name of the Turtle.java file. There are a number of reasons for that, some of which we've not covered. So... rename your file back to Turtle.java -- you were supposed to have been changing this file rather than making a copy of it and renaming it.

Programming Resources