Java编程思想第四版第十三章习题

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值