File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
teachingkidsprogramming/recipes/completed/section00demos Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,27 @@ public static void show()
2626 {
2727 turtle ().show ();
2828 }
29+ /**
30+ * Gets the name for the tortoise
31+ * <div><b>Example:</b> {@code String name = Tortoise.getName("Anita Borg");}</div>
32+ *
33+ * @return the name for the Tortoise
34+ */
35+ public static String getName ()
36+ {
37+ return turtle ().getName ();
38+ }
39+ /**
40+ * Sets the name for the tortoise
41+ * <div><b>Example:</b> {@code Tortoise.setName("Ada Lovelace")}</div>
42+ *
43+ * @param name
44+ * The name for the Tortoise
45+ */
46+ public static void setName (String name )
47+ {
48+ turtle ().setName (name );
49+ }
2950 /**
3051 * Gets the speed that the tortoise moves
3152 * <div><b>Example:</b> {@code int speed = Tortoise.getSpeed(8);}</div>
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ public class Turtle
2727{
2828 public static final int TEST_SPEED = Integer .MIN_VALUE ;
2929 private static final double MAX_MOVE_AMOUNT = 5.0 ;
30+ private String DEFAULT_NAME = "Grace Hopper" ;
3031 public TurtleWindow panel = new TurtleWindow ();
3132 public List <LineSegment > trail = new ArrayList <LineSegment >();
3233 private double x = 640 / 2 ;
@@ -40,6 +41,7 @@ public class Turtle
4041 private boolean hidden ;
4142 private Animals animal ;
4243 private Sound sound = new Sound ();
44+ private String name = DEFAULT_NAME ;
4345 public BufferedImage getImage ()
4446 {
4547 BufferedImage image = panel .getWindowImage ();
@@ -133,6 +135,21 @@ private long getDelay()
133135 if (getSpeed () == TEST_SPEED ) { return TEST_SPEED ; }
134136 return 100 / getSpeed ();
135137 }
138+ public String getName ()
139+ {
140+ return name ;
141+ }
142+ /**
143+ * Sets the name for a turtle instance
144+ * <p><b>Example:</b> {@code myTurtle.setName(name)}</p>
145+ *
146+ * @param name
147+ * Your turtle's name
148+ */
149+ public void setName (String name )
150+ {
151+ this .name = name ;
152+ }
136153 public int getSpeed ()
137154 {
138155 return speed ;
Original file line number Diff line number Diff line change 11package org .teachingkidsprogramming .recipes .completed .section00demos ;
22
33import org .teachingextensions .logo .Tortoise ;
4+ import org .teachingextensions .logo .utils .EventUtils .MessageBox ;
45
56public class QuickTortoise
67{
@@ -9,5 +10,8 @@ public static void main(String[] args) throws Exception
910 Tortoise .show ();
1011 // Use the Tortoise object to draw a Tortoise!
1112 Tortoise .drawTortoise ();
13+ Tortoise .setName ("Sam" );
14+ String t = (Tortoise .getName ());
15+ MessageBox .showMessage (t );
1216 }
1317}
You can’t perform that action at this time.
0 commit comments