Questions on HW6-Spring08
Questions on HW6PART1-Spring08
Question: I get the following error when I try to compile the TurtleDrawingFileIO file: invalid method declaration; return type required. This is referring to my TurtleDrawing constructor. I changed everything it said to change, but are we supposed to add a constructor or remove one? |
- Follow the instructions carefully. Who said to write a constructor? Every method in TurtleDrawingFileIO is static; no constructor is necessary. You also cannot paste the constructor from TurtleDrawing into TurtleDrawingFileIO. Dawn Finney.
Question: I keep getting this error: non-static variable commands cannot be referenced from a static context. All I did was take the provided TurtleDrawing.java and everywhere it said TurtleDrawing, I put TurtleDrawingFileIO. Then I changed the method headers by copy and pasting out of the directions and put "return commands;" before the last bracket in the first two methods. What am I missing? |
Question: In TurtleDrawingFileIO, when outputToFile() calls toString(), I get the following error message: "Error: non-static method toString() cannot be referenced from a static context." Are we really supposed to be renaming toString() to convertCommandsToString() as listed in the instructions? |
Question: Are we supposed to be changing toString()'s name in the outputToFile() method too then? It doesn't say anything about that in the instructions. How does one reference static methods in main()? If there's no constructor, what do the methods work on? |
- Yeah. You're supposed to figure this out. You should know this one. We've told you about static methods before and you've used them. Think about FileChooser.getMediaPath() and Math.random(). getMediaPath is a static method in FileChooser. random() is a static method in Math. Dawn Finney
Question: Using either extractCommands("commands2.txt"); or TurtleDrawingFileIO.extractCommands("commands2.txt"); in main() gives me a FileNotFoundException, even though commands2.txt is in the same folder as TurtleDrawingFileIO. What's causing this? |
Question: Is JPanel supposed to be in java-source? TurtlePanel can't find it for some reason. |
I've looked through everything I can find on Turtles, but can't find a "speed" variable or setter anywhere. How are we supposed to set the speed of a Turtle?
Nevermind, found it further down in the instructions...
Question: When I run the given TurtleDrawing.java for hw6 pt1, all it does is create a blank output .txt file. I have the command files open... ? |
- Don't have the commands file open. I also found an error and uploaded the new version to the coweb but forgot to do it for t-square as well. Basically the extractCommands method isn't supposed to be declaring a new LinkedList. Dawn Finney
Question: What does this line mean, from the instructions about drawCommands(): "You should also add the incoming the LinkedList of new commands to the list of old commands." |
- What it says. Add new commands to the big LinkedList of old commands. Dawn Finney.
Question: When I'm testing the TurtleDrawingFileIO methods in my main method, I'm having trouble figuring out how to use addMoreCommandsFromFile. It calls extractCommands, but never uses the result anywhere, and just returns the commands that you input to it without adding anything. Do we need to change the method in order to make it do something relevant, or am I missing something? |
- The addMoreCommandsFromFile method will extract the commands from the inputted file and add it to the inputted list. You are required to have this method but you are not required to use it in the rest of the homework. I included it so some creative person could do something with it for extra credit. Dawn Finney
Question: In order to make all the variables global, do we need a Global class? Is there another way to go about making them global? |
Question: I keep getting a NullPointerException on line 6. I have printed the commands LL and it is fine. For some reason it cant get the parameters out of the command. Do you know why? |
1public void drawCommands(LinkedList<TurtleCommand> commands){
2 this.commands.addAll(commands);
3 for(int i = 0; i < commands.size();i++){
4 TurtleCommand command = commands.get(i);
5 if(command.getType().equals("forward"))
6 turtle.forward(command.getParameter(0));
if(command.getType().equals("turn"))
turtle.turn(command.getParameter(0));
if(command.getType().equals("moveTo"))
turtle.moveTo(command.getParameter(0), command.getParameter(1));
if(command.getType().equals("setHeading"))
turtle.setHeading(command.getParameter(0));
}
}
Question: When it says "add the incoming LinkedList of new commands to the list of old commands," how and where should we create the new LinkedList? |
I'm unclear about which commands are "old" and which are "new". Are we pulling the "old" linked list (which I'm assuming will be called commands?) from TurtleDrawingFileIO?
- The incoming is new and the old is whatever is already there. TurtleDrawingFileIO does not contain any instance variables. Dawn Finney.
Question: Why when I am trying to create a new TurtlePanel, in the TurtlePanel class, do I get the error (and I have imported all the necessary stuff at the top): Error: C:\cs1316\java-source-Fall06\TurtlePanel.java:59: unreported exception java.io.IOException; must be caught or declared to be thrown |
- Read the error. You should know how to handle this. Dawn Finney.
Question: How can I call the outputToFile method we have on TurtleDrawingFileIO from the TurtlePanel class? |
Question: I am confused on how you would make a turtle command out of setHeading? |
- Make a TurtleCommand of type setHeading and addParameter of whatever heading needs to be passed into the setHeading method. Dawn Finney
I'm having trouble getting my window to show. I think my problem is one of two things: either my main method is wrong or I have not made the proper adjustments to my TurtleEtchASketch method (such as flow layout, i.e. layout manager).
Here is my main method:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setLocation(30, 60);
TurtleSketchPanel sketch = new TurtleSketchPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(sketch);
frame.setVisible(true);
}
}
- Look at the example provided in the homework more carefully. You are missing at least two lines. Dawn Finney
Everything works except for when we press the save, load, and shake buttons, it does what it is supposed to do, but when we press the up, down, etc. buttons afterwards, it won't draw. Why is this?
How are we supposed to use two layout managers? It's confusing because are we supposed to include them both somehow into the SketchPanel? Or are we supposed to use one in the SketchPanel and one in the EtchASketch?
- It's not confusing. Use two LayoutManagers in TurtleSketchPanel. Dawn Finney.
I am getting an FileIO exception error in my TurtleSketchPanel. Its occuring within my ButtonListener class so its not allowing me to throw an exceptioin or do a try catch. I don't know where the problem is.
- You need to throw the Exception or put the statement in a try catch. Java is not "not allowing you" to do either of the forementioned items. You are probably just doing it wrong. Dawn Finney
Does BorderLayout.NORTH and BorderLayout.SOUTH count as two different Layout Managers? We used it in the same "My Turtle Panel."
My ButtonListener works fine, but my TextListener keeps giving me the following error:
Error: ...TurtleSketchPanel.java:114: TurtleSketchPanel.TextListener is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener. What's wrong?
- You need to have the actionPerformed method in the TextListener too, just like what you did with ButtonListener. Dawn Finney.
Thanks for the above. All is working now. I was trying to use: textValueREMOVEDged(TextEvent te) instead of the actionPerformed method (I had found it in the java api). I still don't understand why it wouldn't work and why you need to have the actionPerformed method there in order to not get that error. Maybe it is something that would be easier for you to explain in person... if so, i'll ask you sometime.
- You need the actionPerformed method because it is an abstract method. Any class implementing an interface class is forced (by the Java com) to override all of the abstract methods. Dawn Finney
i am confused about my load method, when i put the text in it will save, but it wont load up in java, if i look at the physical file, it exists
public void load() throws java.io.FileNotFoundException{
this.clear();
commands = new LinkedList();
String chosenFile2 = FileChooser.pickAFile();
TurtleDrawingFileIO.extractCommands(chosenFile2);
drawCommands(commands);
}
and i added this to my main method panel.load();
- You never save the LinkedList returned from the extractCommands method. Dawn Finney
OK, our save and load methods work when tested, but the save in certain cases just writes a blank file. For example if we press up, down, left, right and then press save, the file created by save is blank. However if we load an already filled file like commands2.txt and then save it, the save works.
heres our save button;
else if(source.equals(saveButton)){
try{
turtlePanel.save();
newCommand = "save";
}
catch(Exception e){
System.out.println("error");
e.printStackTrace();
}}
- You obviously don't put the commands from the directional buttons into the linkedlist. Dawn Finney.
Links to this Page