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)); }
}
}