2 Repair InfiniteRecursion.java
package job;
import java.util.*;
public class Main {
public String toString() {
return " InfiniteRecursion address: " + super.toString() + "\n";
}
public static void main(String[] args) {
List<Main> v = new ArrayList<>();
for(int i = 0; i < 10; i++)
v.add(new Main());
System.out.println(v);
}
}
output:
[ InfiniteRecursion address: job.Main@1e643faf
, InfiniteRecursion address: job.Main@668bc3d5
, InfiniteRecursion address: job.Main@3cda1055
, InfiniteRecursion address: job.Main@7a5d012c
, InfiniteRecursion address: job.Main@3fb6a447
, InfiniteRecursion address: job.Main@79b4d0f
, InfiniteRecursion address: job.Main@6b2fad11
, InfiniteRecursion address: job.Main@79698539
, InfiniteRecursion address: job.Main@73f792cf
, InfiniteRecursion address: job.Main@2ed94a8b
]
3 Modify Turtle.java so that it sends all output to System.err.
package job;
import java.util.*;
import java.io.*;
public class Main {
private String name;
private Formatter f;
public Main(String name, Formatter f) {
this.name = name;
this.f = f;
}
public void move(int x, int y) {
f.format("%s The Ja13_5_3 is at (%d,%d)\n", name, x, y);
}
public static void main(String[] args) {
PrintStream outAlias = System.err;
Main tommy = new Main("Tommy", new Formatter(System.err));
Main terry = new Main("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);
}
}
output:
Tommy The Ja13_5_3 is at (0,0)
Terry The Ja13_5_3 is at (4,8)
Tommy The Ja13_5_3 is at (3,4)
Terry The Ja13_5_3 is at (2,5)
Tommy The Ja13_5_3 is at (3,3)
Terry The Ja13_5_3 is at (3,3)
4 Modify Main.java so that the widths are all controlled by a single set of constant values. The goal is to allow you to easily change a width by changing a single value in one place.
package job;
import java.util.*;
public class Main {
private double total = 0;
private Formatter f = new Formatter(System.out);
private int i=20;
private String s1="%-"+i+"s %5s %10s\n";
private String s2="%-"+i+".15s %5d %10.2f\n";
private String s3="%-"+i+"s %5s %10.2f\n";
public void printTitle() {
f.format(s1, "Item", "Qty", "Price");
f.format(s1, "----", "---", "-----");
}
public void print(String name, int qty, double price) {
f.format(s2, name, qty, price);
total += price;
}
public void printTotal() {
f.format(s3, "Tax", "", total*0.06);
f.format("%-15s %5s %10s\n", "", "", "-----");
f.format(s3, "Total", "",
total * 1.06);
}
public static void main(String[] args) {
Main receipt = new Main();
receipt.printTitle();
receipt.print("Jack's Magic Beans", 4, 4.25);
receipt.print("Princes