Karel

Draw A Face, the Sequel

8 points

Introduction
In this program we revisit the Draw-A-Face Karel program we did earlier. Instead of putting all our commands into 'main(String[] args),' we put a lot of our code into methods. A method is a chunk of code that enables an object to do something specific. For example, you will write a method that draws one of the eyes in your sketch. Then whenever you need an eye to be drawn, you can just call that method. You can see the advantage: your sketch has 2 eyes, but you only need to write the method once. Calling it twice to get both a left and right eye is easy.

Starting Point
You can download DrawAFace_2.java to use as a starting point in writing your program.

betterRobot class
In 'DrawAFace_2.java' you will notice that a class 'betterRobot' has been defined. 'betterRobot' extends the original 'Robot' class, which means that a betterRobot can do everything a Robot can do -- and more. What more it can do is given in the 'betterRobot' class. For example, there is a 'turnRight()' method in 'betterRobot', so a betterRobot knows how to turn right (while a Robot does not). You might want to start at this point and write the code for the 'turnRight()' method.

You'll notice that a 'tl()' method has been defined in the 'betterRobot' class. All this method does is cause the robot to execute a 'turnLeft()'. Why not just call 'turnLeft()' if you need the robot to turn left? Well, because it is quicker to write 'tl()' when you are writing code than it is to write 'turnLeft(),' especially if you are having the robot make a lot of left turns.

You'll notice that a 'tr()' method has been defined in the 'betterRobot' class. Since 'tr' is short for 'turnRight,' you can go ahead and complete this method.

Feel free to mimic the methods 'tl()' and 'tr()' and create other shortcuts for Karel commands. In 'main(),' for example, is the line 'k.put()'. The method 'put()' is short for 'putBeeper(),' so you can mimic how the 'tl()' and 'tr()' methods were written and create a method 'put()'. In my program, I also created these shortcut methods: 'm()' and 'pick()'.

You'll also notice in the 'betterRobot' class the methods 'drawEye()' and 'drawMouth()'. Your job is to copy the code from your earlier Draw-A-Face program that draws an eye into the 'drawEye()' method. Similarly, copy the code that draws the mouth into the 'drawMouth()' method. Notice that, when defining a method, you omit the object: it is 'move()' not 'karel.move()'.

main(String[] args)
In 'main()' you call the methods you need; I've kept 'k.drawEye()' and 'k.drawMouth()' in 'main()'. You will also need to add some code to get the robot to move around the world. After the first call to 'k.drawEye(),' for example, the robot has to get in position to draw the second eye. You can put that code in 'main()' between calls to 'k.drawEye()'.

If you drew something other than a face, you won't write a 'drawEye()' or 'drawMouth()' method. But you will still be able to create some methods. If you drew a house, for example, you might have methods like 'drawMainHouse()' and 'drawChimney()'.