Instructions: Follow the steps given in each section below. For each question, click the radio button next to the most correct answer. When you are finished, type your name and your partner at the end of the document, write down your answers for your records, then press the Submit button. You may submit just one lab response. If you submit more than one, only the first one counts for a grade.
This lab is designed to introduce you to classes and methods, and to see why they are useful. By the end of the lab, you should be able to write a simple class that extends the Turtle class.
// Draw a line straight left import turtlegraphics.*; public class DrawStraightLineLeft { public static void main(String[] args) throws TurtleException { Turtle aTurtle = new Turtle(); // Face left by using two turnRight() instructions aTurtle.turnRight(180); // Face down aTurtle.turnRight(90); // Face left // Draw a line aTurtle.move(300); } }
// Draw a square import turtlegraphics.*; public class DrawSquare { public static void main(String[] args) throws TurtleException { Turtle aTurtle = new Turtle(); // Draw first edge aTurtle.move(300); aTurtle.turnRight(90); // Draw second edge aTurtle.move(300); aTurtle.turnRight(90); // Draw third edge aTurtle.move(300); aTurtle.turnRight(90); // Draw fourth edge aTurtle.move(300); aTurtle.turnRight(90); } }
class SmartTurtle extends Turtle { }
Turtle aTurtle = new Turtle();
to
SmartTurtle aTurtle = new SmartTurtle();
(This changes the turtle from a regular Turtle to a SmartTurtle turtle.)
// turnLeft90: Turns the turtle 90 degrees to its left public void turnLeft90() throws TurtleException { this.turnRight(180); this.turnRight(90); }
SmartTurtle aTurtle = new Turtle(); // Note "Turtle" after "new"
Turtle aTurtle = new SmartTurtle(); // Note "SmartTurtle" after "new"
// turnLeft: Turns the turtle the given degrees to its left public void turnLeft(int amount) throws TurtleException { this.turnRight(180); this.turnRight(180 - amount); }
// moveBack: Makes the turtle move backward the given distance public void moveBack(int distance) throws TurtleException { this.turnRight(180); this.move(distance); this.turnRight(180); }
aTurtle.moveBack(500);
Write down your answers for your records, then press the Submit button below to turn in the lab assignment.