Java - Think in Java 第3章 习题

###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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpikeKing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值