2015-09-07 11:44:36 -06:00
|
|
|
// strings/Turtle.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class Turtle {
|
|
|
|
private String name;
|
|
|
|
private Formatter f;
|
|
|
|
public Turtle(String name, Formatter f) {
|
|
|
|
this.name = name;
|
|
|
|
this.f = f;
|
|
|
|
}
|
|
|
|
public void move(int x, int y) {
|
2017-05-10 11:45:39 -06:00
|
|
|
f.format("%s The Turtle is at (%d,%d)%n",
|
|
|
|
name, x, y);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
PrintStream outAlias = System.out;
|
|
|
|
Turtle tommy = new Turtle("Tommy",
|
|
|
|
new Formatter(System.out));
|
|
|
|
Turtle terry = new Turtle("Terry",
|
|
|
|
new Formatter(outAlias));
|
|
|
|
tommy.move(0,0);
|
|
|
|
terry.move(4,8);
|
|
|
|
tommy.move(3,4);
|
|
|
|
terry.move(2,5);
|
|
|
|
tommy.move(3,3);
|
|
|
|
terry.move(3,3);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Tommy The Turtle is at (0,0)
|
|
|
|
Terry The Turtle is at (4,8)
|
|
|
|
Tommy The Turtle is at (3,4)
|
|
|
|
Terry The Turtle is at (2,5)
|
|
|
|
Tommy The Turtle is at (3,3)
|
|
|
|
Terry The Turtle is at (3,3)
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|