Java - Think in Java 第3章 习题

标签: MystraJava编程思想第3章习题
737人阅读 评论(0) 收藏 举报
分类:

1

参考: http://blog.csdn.net/caroline_wendy/article/details/46784889

2&3

参考: http://blog.csdn.net/caroline_wendy/article/details/46792233

4

//: Main.java

class VelocityCalculator {
    static float velocity (float d, float t) {
        if (t == 0) return 0f; // 注意除法分母
        return d/t;
    }
}

/**
 * 速度
 */
public class Main {
    public static void main(String[] args) {
        float distance = 10.0f; // 距离
        float time = 2.0f; // 时间
        System.out.println("速度: " + VelocityCalculator.velocity(distance, time));
    }
}
/**
 * Output:
 * 速度: 5.0
 *///:~

5&6

//: Main.java

class Dog {
    public String name;
    public String says;
}

/**
 * 速度
 */
public class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.name = "spot";
        d1.says = "Ruff!";
        Dog d2 = new Dog();
        d2.name = "scruffy";
        d2.name = "Wurf";
        System.out.println("Dog " + d1.name + ": " + d1.says);
        System.out.println("Dog " + d2.name + ": " + d2.says);
        Dog d3 = new Dog();
        d3 = d1;
        System.out.println("d3 == d1 : " + (d1 == d3));
        System.out.println("d3 equal d1 : " + (d1.equals(d3)));
    }
}
/**
 * Output:
 * d3 == d1 : true
 * d3 equal d1 : true
 *///:~

7

//: Main.java

import java.util.Random;

/**
 * 模拟抛硬币
 */
public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        for (int i=0; i<8; ++i) {
            boolean b = rand.nextBoolean();
            System.out.println("抛硬币["+ (i+1) + "次]: " + (b?"正面":"反面"));
        }
    }
}
/**
 * Output:
 * 抛硬币[1次]: 反面
 * 抛硬币[2次]: 正面
 * 抛硬币[3次]: 正面
 * 抛硬币[4次]: 反面
 * 抛硬币[5次]: 正面
 * 抛硬币[6次]: 反面
 * 抛硬币[7次]: 正面
 * 抛硬币[8次]: 反面
 *///:~

8

//: Main.java

/**
 * 模拟抛硬币
 */
class Main {
    public static void main(String[] args) {
        long l1 = 0x10;
        @SuppressWarnings("OctalInteger") long l2 = 010;
        long r = l1*l2;
        System.out.println("l1 = " + l1 + ", 二进制: " + Long.toBinaryString(l1));
        System.out.println("l2 = " + l2 + ", 二进制: " + Long.toBinaryString(l2));
        System.out.println("l1*l2 = " + r + ", 二进制: " + Long.toBinaryString(r));
    }
}
/**
 * Output:
 * l1 = 16, 二进制: 10000
 * l2 = 8, 二进制: 1000
 * l1*l2 = 128, 二进制: 10000000
 *///:~

9

//: Main.java

/**
 * 指数计数法double和float
 */
class Main {
    public static void main(String[] args) {
        System.out.println("double max = " + Double.MAX_VALUE);
        System.out.println("double min = " + Double.MIN_VALUE);
        System.out.println("float max = " + Float.MAX_VALUE);
        System.out.println("float min = " + Float.MIN_VALUE);
    }
}
/**
 * Output:
 * double max = 1.7976931348623157E308
 * double min = 4.9E-324
 * float max = 3.4028235E38
 * float min = 1.4E-45
 *///:~

10

//: Main.java

/**
 * 按位操作符
 */
class Main {
    public static void main(String[] args) {
        int i1 = Integer.valueOf("10101010", 2);
        int i2 = Integer.valueOf("10101011",2);
        System.out.println("i1 = " + i1 + ", (2)" + Integer.toBinaryString(i1));
        System.out.println("i2 = " + i2 + ", (2)" + Integer.toBinaryString(i2));
        System.out.println("i1|i2 = " + (i1|i2) + ", (2)" + Integer.toBinaryString((i1|i2)));
        System.out.println("i1^i2 = " + (i1^i2) + ", (2)" + Integer.toBinaryString((i1^i2)));
        System.out.println("i1&i2 = " + (i1&i2) + ", (2)" + Integer.toBinaryString((i1&i2)));
        System.out.println("~i1 = " + (~i1) + ", (2)" + Integer.toBinaryString((~i1)));
    }
}
/**
 * Output:
 * i1 = 170, (2)10101010
 * i2 = 171, (2)10101011
 * i1|i2 = 171, (2)10101011
 * i1^i2 = 1, (2)1
 * i1&i2 = 170, (2)10101010
 * ~i1 = -171, (2)11111111111111111111111101010101
 *///:~

11

//: Main.java

/**
 * 移位操作
 */
class Main {
    public static void main(String[] args) {
        int x = 0x1ea7;
        System.out.println("x = " + Integer.toBinaryString(x));
        for(int i=0; i<12; ++i) {
            x >>= 1;
            System.out.println(Integer.toBinaryString(x));
        }
    }
}
/**
 * Output:
 * x = 1111010100111
 * 111101010011
 * 11110101001
 * 1111010100
 * 111101010
 * 11110101
 * 1111010
 * 111101
 * 11110
 * 1111
 * 111
 * 11
 * 1
 *///:~

12

//: Main.java

/**
 * 移位操作
 */
class Main {
    public static void main(String[] args) {
        int x = -1;
        x <<= 12;
        for (int i=0; i<32; ++i) {
            x >>>= 1; // 无符号右移高位加0
            // x >>= 1; // -1有符号右移高位加1
            System.out.println(Integer.toBinaryString(x));
        }
    }
}
/**
 * Output:
 * ...
 *///:~

13

//: Main.java

/**
 * 字符二进制操作
 */
class Main {
    public static void main(String[] args) {
        char c = 'a';
        System.out.println(c + ":" + Integer.toBinaryString(c) + "," + Integer.toString(c));
        c = 'e';
        System.out.println(c + ":" + Integer.toBinaryString(c) + "," + Integer.toString(c));
        c = '=';
        System.out.println(c + ":" + Integer.toBinaryString(c) + "," + Integer.toString(c));
        c = 'A';
        System.out.println(c + ":" + Integer.toBinaryString(c) + "," + Integer.toString(c));
    }
}
/**
 * Output:
 * a:1100001,97
 * e:1100101,101
 * =:111101,61
 * A:1000001,65
 *///:~

14

//: Main.java

/**
 * 字符串比较
 */
class Main {
    public static void print(boolean b) {
        System.out.println(b?"True":"False");
    }

    // String类型不允许使用大小(<, >)比较
    public static void compare(String s1, String s2) {
        print(s1 != s2);
        print(s1 == s2);
        print(s1.equals(s2));
        System.out.println();
    }

    public static void main(String[] args) {
        // 创建对象指针不相等
        String s1 = new String("Hello");
        String s2 = new String("Hello");
        compare(s1, s2);
        s1 = "Hello";
        s2 = "Hello";
        // 字符串指针相等
        compare(s1, s2);
        compare("Hello", "Hello");
    }
}
/**
 * Output:
 * True
 * False
 * True
 * 
 * False
 * True
 * True
 * 
 * False
 * True
 * True
 *///:~

娱乐

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2355220次
    • 积分:31541
    • 等级:
    • 排名:第154名
    • 原创:705篇
    • 转载:1篇
    • 译文:0篇
    • 评论:380条
    自我简介
    文章分类
    最新评论