• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could I run some ideas by you?  RSS feed

 
Rachel Tyler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm working on the following problem:
Imagine a mechanical turtle that walks around the room under the control of
a Java program. The turtle holds a pen in one of two positions, up or down. While the
pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle
moves about freely without writing anything. In this problem, you will simulate the operation
of the turtle and create a computerized sketchpad.
Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array
that contains them. Keep track of the current position of the turtle at all times and
whether the pen is currently up or down. Assume that the turtle always starts at
position (0, 0) of the floor with its pen up.
The set of turtle commands your program must process are shown below.
Suppose that the turtle is somewhere near the center of the floor. The following “program”
would draw and print a 12-by-12 square, leaving the pen in the up position:
2
5,12
3
5,12
3
5,12
3
Command Meaning
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces (replace 10 for a different number of spaces)
6 Print the 20-by-20 array
9 End of data (sentinel)


I'm only trying to work out what to do with pen and paper at this stage, but could you please let me know what you think of my ideas for programming so far?
Rather than write lines of description, I'll write down the rough code I've planned at this stage:


For the turnRight method (and similar for the turnLeft method) I am thinking of doing something like:


For the move method:


From there I plan to increment the appropriate number of integers col and row according to what direction is being moved.
By using instance variables for col and row I hope to be able to keep their value between method calls so that the location
of the turtle on the floor array will not be lost. Also, as the turtle moves, if penDown is true, then it will change the values in the array from 0 to 1.

Before I start coding all this could anyone let me know if they think I've got the right idea? I'm not that experienced and know that
there are a lot of better ways to write the code than this, but I just feel a bit over my head on this exercise. I know I still need to write
the display method also but wanted to get some feedback first if possible. Thanks!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Tyler wrote:Before I start coding all this could anyone let me know if they think I've got the right idea? I'm not that experienced and know that
there are a lot of better ways to write the code than this, but I just feel a bit over my head on this exercise. I know I still need to write
the display method also but wanted to get some feedback first if possible.

Not bad. I particularly like the idea of a "circular" direction; indeed I've actually implemented just such a class myself.

One thing you might want to think about is using enums for your Commands and Directions. It's likely to make your program easier to read, and you can probably put a lot of your "turning" logic into the latter.

You might also want to think about a Board class that knows about its "edges" (and therefore whether a "move" is valid or not).

And then, of course, there's Turtle...

So: good start; but try and think a bit more about the classes that are going to make up your program, and how they interact, rather than all this "add 1" type stuff. That's more about mechanics (the 'how') than what you need to do.

This 'design' business takes a while to get used to, but you may find the WhatNotHow (←click) page useful.

Good luck.

Winston

PS: I edited some of those enormous lines in your post because they were screwing up the windowing here. I suggest you look at the DontWriteLongLines page.
 
Rachel Tyler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston.
I managed to get the program to work, but I'm going to go back now and try and use a more object oriented approach like you suggest.
The textbook I'm using really gets into OO in the next chapter, but I'd like to try and get into that mindset as soon as
possible. The code I wrote so far is below. It's not pretty, but got the job done. I think I will try and build a turtle object to start.



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Tyler wrote:I think I will try and build a turtle object to start...

If you like, but you may find it easier to build the "lower-level" classes first.

Just as a taster for you, here's a possibility for the start of a "circular" 4-point Direction class based on an enum:Do you see how that might help you?

BTW: It's probably a bloke thing, but I'd be very careful about capitalization with an attribute called 'penIsDown'.

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you have a capital I or not (my wife had a good laugh at that), you should not follow it by == true or == false. You should never use == true and == false at all.Using != looks even worse. You can get nasty errors if you write = false or similar by mistake.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also have an UP/DOWN enum. You can read about enums here.

Don't use \n (even though lots of books use it) unless you have been told to use the LF character. Least of all after printf, where you should use %n.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I suggest isPenDown instead?
 
Rachel Tyler
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: It's probably a bloke thing, but I'd be very careful about capitalization with an attribute called 'penIsDown'.


Whoopsie daisy! At least I didn't make a penIsUp variable

Thank you for all the advice, I'll keep on cracking!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Tyler wrote:Whoopsie daisy! At least I didn't make a penIsUp variable

Yeah, you don't want anything distracting you when you're trying to program.

Getting back on track though: You might want to have a look at the tutorials (or the relevant chapter in whatever book you're using) on enums, because they're a lot more than just a list of names - they're fully-fledged objects.

And that means they can do almost anything that a normal final class can do, including implementing interfaces and defining methods (including ones that have different implementations for each name).

And, given the problem you're trying to solve, I think you'll find them very useful.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!