JAVA编程思想第十三章练习题

 

import java.util.stream.Stream;

public class InfiniteRecursion {
    @Override
    public String toString() {
        return super.toString();
    }

    public static void main(String[] args) {
        Stream.generate(InfiniteRecursion::new)
                .limit(10)
                .forEach(System.out::println);
    }
}

import java.util.Formatter;

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){
        f.format("%s The Turtle is at (%d,%d)%n",name,x,y);
    }

    public static void main(String[] args) {
        Formatter f = new Formatter(System.err);
        Turtle tommy = new Turtle("Tommy",f);
        Turtle terry = new Turtle("Terry",f);
        tommy.move(0,0);
        terry.move(4,8);
        tommy.move(3,4);
        terry.move(2,5);
        tommy.move(3,3);
        terry.move(3,3);

    }
}

import java.util.*;
class Receipt{
    public static final int ITEM_WIDTH = 15;
    public static final int QTY_WIDTH = 5;
    public static final int PRICE_WIDTH = 10;
    private static final String TITLE_FRMT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + "s\n";
    private static final String ITEM_FRMT = "%-" + ITEM_WIDTH + "." + ITEM_WIDTH + "s %" + QTY_WIDTH + "d %" + PRICE_WIDTH + ".2f\n";
    private static final String TOTAL_FRMT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + ".2f\n";
    private double total = 0;
    Formatter f = new Formatter(System.out,Locale.US);
    public void printTitle(){
        f.format(TITLE_FRMT, "Item", "Qty", "Price");
        f.format(TITLE_FRMT, "----", "---", "-----");
    }
    public void print(String name, int qty, double price) {
        f.format(ITEM_FRMT, name, qty, price);
        total += price;
    }
    public void printTotal() {
        f.format(TOTAL_FRMT, "Tax", "", total*0.06);
        f.format(TITLE_FRMT, "", "", "-----");
        f.format(TOTAL_FRMT, "Total", "",
                total * 1.06);
    }
}
public class E04 {
    public static void main(String[] args) {
        Receipt receipt = new Receipt();
        receipt.printTitle();
        receipt.print("Jack's Magic Beans", 4, 4.25);
        receipt.print("Princess Peas", 3, 5.1);
        receipt.print("Three Bears Porridge", 1, 14.29);
        receipt.printTotal();
    }
}

 

 

import java.util.*;
import static java.lang.String.*;
class DataHolder {
    int intField = 1;
    long longField = 2L;
    float floatField = 3.0f;
    double doubleField = 4.0;
    public String toString() {
        StringBuilder result =
                new StringBuilder("DataHolder: \n");
        result.append(
                format(Locale.US, "intField: %d\n", intField));
        result.append(
                format(Locale.US, "longField: %d\n", longField));
        result.append(
                format(Locale.US, "floatField: %f\n", floatField));
        result.append(
                format(Locale.US, "doubleField: %e\n", doubleField));
        return result.toString();
    }
}
public class E06 {
    public static void main(String[] args) {
        System.out.println(new DataHolder());
    }
}

 

练习7:

public class E07 {
    public static boolean matches(String text){
        return text.matches("\\p{javaUpperCase}.*\\.");
    }

    public static void main(String[] args) {
        System.out.println(matches("Hello!"));
        System.out.println(matches("How are you?"));
        System.out.println(matches("I am fine."));
    }
}

练习8:

import java.util.Arrays;

public class E08 {
    public static void split(String regex){
        System.out.println(Arrays.toString(Splitting.knights.split(regex)));
    }

    public static void main(String[] args) {
        System.out.println(Splitting.knights);
        split("the|you");
    }
}

 练习9:

public class E09 {
    public static void main(String[] args) {
        System.out.println(Splitting.knights.replaceAll("(?i)[aeiou]","_"));
    }
}

 练习10:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class E10 {
    public static void main(String[] args) {
        String source = "Java now has regular expressions";
        String[] regEx = {"^Java","\\Breg.*","n.w\\s+h(a|i)s","s?","s+","s{4}","s{1}.","s{0,3}"};
        System.out.println("Source string: " + source);
        for(String pattern : regEx){
            System.out.println("Regular expression: \"" + pattern + "\"");
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(source);
            while (m.find()){
                System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1));
            }
        }
    }
}

 练习11:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class E11 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("(?i)((^[aeiou])|(\\s+[aeiou]))\\w+?[aeiou]\\b");
        Matcher m = p.matcher("Arline ate eight apples and " + "one orange while Anita hadn't any");
        while (m.find()){
            System.out.println("Match \"" + m.group() +
                    "\" at positions " + m.start() + "-" +
                    (m.end() - 1));        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值