练习1:分析reusing/SprinklerSystem.java的SprinklerSystem.toString()方法,看看明确地使用StringBuilder是否确实能够避免产生过多的StringBuilder对象。
package test13.Ex01;
import javax.xml.transform.Source;
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = "Constructed";
}
public String toString() {
return s;
}
}
public class SprinklerSystem {
private String s1,s2,s3, s4;
private WaterSource waterSource = new WaterSource();
private int i;
private float f;
public String toString() {
return
"s1=" + s1 + " " +
"s2=" + s2 + " " +
"s3=" + s3 + " " +
"s4=" + s4 + " \n" +
"i=" + i + " " +
"f=" + f + " " +
"waterSource=" + waterSource + " ";
}
public static void main(String[] args) {
SprinklerSystem sprinklerSystem = new SprinklerSystem();
System.out.println(sprinklerSystem);
}
}
结果显示:
WaterSource()
s1=null s2=null s3=null s4=null
i=0 f=0.0 waterSource=Constructed
Process finished with exit code 0
练习2:(1)修复InfiniteRecursion.java。
package test13.Ex02;
import java.util.ArrayList;
import java.util.List;
public class InfiniteRecursion {
public String toString() {
return "InfiniteRecursion address: " + super.toString() + "\n";
}
public static void main(String[] args) {
List<InfiniteRecursion> v = new ArrayList<>();
for (int i = 0; i < 10; i++) {
v.add(new InfiniteRecursion());
}
System.out.println(v);
}
}
结果显示:
[InfiniteRecursion address: test13.Ex02.InfiniteRecursion@677327b6
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@14ae5a5
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@7f31245a
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@6d6f6e28
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@135fbaa4
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@45ee12a7
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@330bedb4
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@2503dbd3
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@4b67cf4d
, InfiniteRecursion address: test13.Ex02.InfiniteRecursion@7ea987ac
]
Process finished with exit code 0
练习3:(1)修改Turtle.java,使之将结果输出到System.err中。
package test13.Ex03;
import java.io.PrintStream;
import java.util.Formatter;
public class Turtle01 {
private String name;
private Formatter f;
public Turtle01(String name, Formatter f) {
this.name = name;
this.f = f;
}
public void move(int x, int y) {
f.format("%s The Turtle is at (%d,%d)\n", name, x, y);
}
public static void main(String[] args) {
PrintStream outAlias = System.err;
Turtle01 Tom = new Turtle01("Tom", new Formatter(System.err));
Turtle01 Tony = new Turtle01("Tony", new Formatter(System.err));
Tom.move(0, 0);
Tony.move(4, 8);
Tom.move(3, 4);
Tony.move(2, 5);
Tom.move(3, 3);
Tony.move(3, 3);
}
}
结果显示:
Tom The Turtle is at (0,0)
Tony The Turtle is at (4,8)
Tom The Turtle is at (3,4)
Tony The Turtle is at (2,5)
Tom The Turtle is at (3,3)
Tony The Turtle is at (3,3)
Process finished with exit code 0
练习4:(3)修改Receipt.java,令所有的宽度都由一个常量来控制。目的是使宽度的改变更容易,只需修改一处的值即可。
package test13.Ex04;
import java.util.Formatter;
public class Receipt01 {
private double total = 0;
private Formatter f = new Formatter(System.err);
private static final int W1 = 15;
private static final int W2 = 5;
private static final int W3 = 10;
private String s1 = "%-" + W1 + "s%" + W2 + "s%" + W3 + "s\n";
private String s2 = "%-" + W1 + ".15s%" + W2 + "d%" + W3 + ".2f\n";
private String s3 = "%-" + W1 + "s