Thinking in Java 第四版完整版 第二章练习题

Thinking in Java 第四版完整版 第二章练习题,记录一下(jdk1.8.0_111)

1.

/**
 * 练习1: 创建一个类,它包含一个int域和一个char域,它们都没有被初始化,将它们的值打印出来,以验证Java执行了默认初始化。
 * @author admin11
 * @date 2018年2月27日
 */
public class Exercise201 {

    int count;
    char c;

    public Exercise201() {
        System.out.println("int count = " + count);
        System.out.println("char c = " + c);
    }

    public static void main(String[] args) {
        new Exercise201();
    }
}

运行结果:
这里写图片描述

2.

/**
 * 练习2:参照本章的HelloDate.java这个例子,创建一个“Hello, World”程序,
 * 该程序只要输出这句话即可。你所编写的类里只需一个方法(即“main”方法,在程序启动时被执行)。
 * 记住要把它设为static形式,并指定参数列表-即使根本不会用到这个列表。用javac进行编译,
 * 再用java运行它。如果你使用的是不同于JDK的开发环境,请了解如何在你的环境中进行编译和运行。
 * @author admin11
 * @date 2018年2月27日
 */
public class Exercise202 {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

运行结果:
这里写图片描述

3.

/**
 * 练习3:找出含有ATypeName的代码段,将其改写成完整的程序,然后编译、运行。
 * @author admin11
 * @date 2018年2月27日
 */

class ATypeName {
}

public class Exercise203 {

    public static void main(String[] args) {
        ATypeName a = new ATypeName();
    }
}

4.

/**
 * 练习4:将DataOnly代码段改写成一个程序,然后编译、运行。
 * @author admin11
 * @date 2018年2月27日
 */

class DataOnly {
    int i;
    double d;
    boolean b;
}

public class Exercise204 {

    public static void main(String[] args) {
        DataOnly d = new DataOnly();
        d.i = 1;
        d.d = 2.786;
        d.b = false;
    }
}

5.

/**
 * 练习5:修改前一个练习,将DataOnly中的数据在main()方法中赋值并打印出来。
 * @author admin11
 * @date 2018年2月27日
 */

class DataOnly2 {
    int i;
    double d;
    boolean b;
}

public class Exercise205 {

    public static void main(String[] args) {
        DataOnly2 d = new DataOnly2();
        d.i = 1;
        System.out.println("d.i = " + d.i);
        d.d = 2.786;
        System.out.println("d.d = " + d.d);
        d.b = false;
        System.out.println("d.b = " + d.b);
    }
}

运行结果:
这里写图片描述

6.

/**
 * 练习6:编写一个程序,让它含有本章所定义的storage()方法的代码段,并调用之。
 * @author admin11
 * @date 2018年2月27日
 */

public class Exercise206 {

    public int storage(String s) {
        return s.length() * 2;
    }

    public static void main(String[] args) {
        Exercise206 e = new Exercise206();
        int len = e.storage("Hello");
        System.out.println("storage(s) = " + len);
    }
}

运行结果:
这里写图片描述

7.

/**
 * 练习7:将Incrementable的代码段改写成一个完整的可运行程序。
 * @author admin11
 * @date 2018年2月27日
 */

class StaticTest {
    static int i = 47;
}

class Incrementable {
    public static void increment() {
        StaticTest.i++;
    }
}

public class Exercise207 {

    public static void main(String[] args) {
        Incrementable in = new Incrementable();
        in.increment();
        System.out.println(StaticTest.i);
        Incrementable.increment();
        System.out.println(StaticTest.i);
    }
}

运行结果:
这里写图片描述

8.

/**
 * 练习8:编写一个程序,展示无论你创建了某个特定类的多少个对象,这个类中的某个特定的static域
 * 只有一个实例。
 * @author admin11
 * @date 2018年2月27日
 */
public class Exercise208 {

    static int i = 47;

    public static void main(String[] args) {
        Exercise208 e1 = new Exercise208();
        Exercise208 e2 = new Exercise208();
        System.out.println(e1.i + " == " + e2.i);
        e1.i++;
        System.out.println(e1.i + " == " + e2.i);
    }
}

运行结果:
这里写图片描述

9.

/**
 * 练习9:编写一个程序,展示自动包装功能对所有的基本类型和包装类型都起作用。
 * @author admin11
 * @date 2018年2月27日
 */
public class Exercise209 {

    public static void main(String[] args) {
        Byte by = 1;
        byte bt = by;
        System.out.println("byte = " + bt);

        Short sh = 1;
        short s = sh;
        System.out.println("short = " + s);

        Integer in = 1;
        int i = in;
        System.out.println("int = " + i);

        Long lo = 1l; // 1L
        long l = lo;
        System.out.println("long = " + l);

        Boolean bool = true;
        boolean b = bool;
        System.out.println("boolean = " + b);

        Character ch = 'x';
        char c = ch;
        System.out.println("char = " + c);

        Float fl = 1.0f;
        float f = fl;
        System.out.println("float = " + f);

        Double db = 1.0d;
        double d = db;
        System.out.println("double = " + d);
    }
}

运行结果:
这里写图片描述

10.

/**
 * 练习10:编写一个程序,打印出从命令行获得的三个参数。为此,需要确定命令行数组中String的下标。
 * @author admin11
 * @date 2018年2月27日
 */
public class Exercise210 {

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(i + " : " + args[i]);
        }
    }
}

运行结果:
这里写图片描述

11.

/**
 * 练习11:将AllTheColorsOfTheRainbow这个示例改成一个程序,然后编译、运行。
 * @author admin11
 * @date 2018年2月27日
 */

class AllTheColorsOfTheRainbow {
    int anIntegerRepresentingColors;
    void changeTheHueOfTheColor(int newHue) {
        anIntegerRepresentingColors = newHue;
    }
}

public class Exercise211 {

    public static void main(String[] args) {
        AllTheColorsOfTheRainbow color = new AllTheColorsOfTheRainbow();
        System.out.println(color.anIntegerRepresentingColors);
        color.changeTheHueOfTheColor(1024);
        System.out.println(color.anIntegerRepresentingColors);
    }
}

运行结果:
这里写图片描述

12.找出HelloDate.java的第二个版本,也就是那个简单注释文档的示例。对该文件执行javadoc,然后通过web浏览器观看运行结果。

//:HelloDate.java
import java.util.*;

/**
 * The first Thinking in Java example program.
 * Displays a string and today's date.
 * @author Bruce Eckel
 * @author www.MindView.net
 * @version 4.0
 */
public class HelloDate {

    /**
     * Entrv Doint to class & application
     * @param args array of string arguments
     * @throws exceptions No exceptions thrown
     */
    public static void main(String[] args) {
        System.out.println("Hello, it's: ");
        System.out.println(new Date());
    }
}
/* Output: (55% match)
Hello, it's:
Wed Oct 05 14:39:36 MDT 2005
 *///:~

运行结果:
这里写图片描述

13.通过Javadoc运行Documentation1.java,Documentation2.java和Documentation3.java,然后通过web浏览器观看运行结果。

//:Documentation1.java
/** A class comment */
public class Documentation1 {
    /** A field comment */
    public int i;
    /** A method comment */
    public void f() {}
}
///:~
import java.util.Date;
//: Documentation2.java
/**
 * <pre>
 * Uses
 * System.out.println(new Date());
 * </pre>
 */
public class Documentation2 {
    Date d = new Date();
    void showDate() {
        System.out.println("Date = " + d);
    }
}
///:~
import java.util.Date;
//:Documentation3.java
/**
 * You can even insert a list:
 * <ol>
 * <li> Item one
 * <li> Item two
 * <li> Item three
 * </ol>
 */
public class Documentation3 {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println("d = " + d);
    }
}
///:~

运行结果:
这里写图片描述

14.在前一个练习的文档中加入各项的HTML列表。

import java.util.Date;
//: Documentation4.java
/**
* You can even insert a list:
* <ol>
* <li> Item one
* <li> Item two
* <li> Item three
* </ol>
* Another test list
* <ol>
* <li> One
* <li> Two
* <li> Three
* </ol>
*/
public class Documentation4 {

    /**
     * Let's try a public field list
     * <ol>
     * <li> One
     * <li> Two
     * <li> Three
     * </ol>
     */
    public int i = 2;

    /**
     * A private field list (-private to see) 
     * <ol>
     * <li> One
     * <li> Two
     * <li> Three
     * </ol>
     */
    public int j = 3;

    /**
     * Another list can be inserted here to help explain the
     * following method call
     * <ol>
     * <li> One
     * <li> Two
     * <li> Three
     * </ol><br>
     * but may be formatted differently in Method Summary
     */
    public static void main(String[] args) {
        /**
         * Let's try another test list here
         * <ol>
         * <li> One
         * <li> Two
         * <li> Three
         * </ol>
         */
        Date d = new Date();
        System.out.println("d = " + d);
    }
}
///:~

运行结果:
这里写图片描述

15.使用练习2的程序,加入注释文档。用javadoc提取此注释文档,并产生一个HTML文件,然后通过Web浏览器查看结果。

//: Exercise215.java
/**
 * Public class contained in file of the same name that includes main()
 */
public class Exercise215 {

    /**
     * main method executed by java 
     */
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
///:~

运行结果:
这里写图片描述

16.找到第5章中的Overloading.java示例,并为它加入javadoc文档。然后用javadoc提取此注释文档,并产生一个HTML文件,最后,通过Web浏览器查看结果。

//: Overloading.java

/** Model of a single arboreal unit. */
class Tree {
    /** Current vertical aspect to the tip. */
    int height; // 0 by default

    /** Plant a seedling. Assume height can be considered as zero. */
    Tree() {
        System.out.println("Planting a seedling");
    }

    /** Transplant an existing tree with a given height. */
    Tree(int i) {
        System.out.println("Creating new Tree that is " + i + " feet tall");
        height = i;
    }

    /** Produce information about this unit. */
    void info() {
        System.out.println("Tree is " + height + " feet tall");
    }

    /** Produce information with optional message. */
    void info(String s) {
        System.out.println(s + ": Tree is " + height + " feet tall");
    }
}
/** Simple test code for Tree class */
public class Overloading {

    /** Creates <b>Tree</b> objects and exercises the two
    different <code>info()</code> methods. */
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++) {
            Tree t = new Tree(i);
            t.info();
            t.info("overloaded method");
        }
        // Overloaded constructor:
        new Tree();
    }
}
///:~

运行结果:
这里写图片描述

写在前面的话 引言 1. 前提 2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 实现方案的隐藏 1.4 方案的重复使用 1.5 继承:重新使用接口 1.5.1 改善基础类 1.5.2 等价和类似关系 1.6 多形对象的互换使用 1.6.1 动态绑定 1.6.2 抽象的基础类和接口 1.7 对象的创建和存在时间 1.7.1 集合与继承器 1.7.2 单根结构 1.7.3 集合库与方便使用集合 1.7.4 清除时的困境:由谁负责清除? 1.8 违例控制:解决错误 1.9 多线程 1.10 永久性 1.11 Java和因特网 1.11.1 什么是Web? 1.11.2 客户端编程 1.11.3 服务器端编程 1.11.4 一个独立的领域:应用程序 1.12 分析和设计 1.12.1 不要迷失 1.12.2 阶段0:拟出一个计划 1.12.3 阶段1:要制作什么? 1.12.4 阶段2:开始构建? 1.12.5 阶段3:正式创建 1.12.6 阶段4:校订 1.12.7 计划的回报 1.13 Java还是C++? 第2章 一切都是对象 2.1 用句柄操纵对象 2.2 必须创建所有对象 2.2.1 保存在什么地方 2.2.2 特殊情况:主类型 2.2.3 Java中的数组 2.3 绝对不要清除对象 2.3.1 作用域 2.3.2 对象的作用域 2.4 新建数据类型:类 2.4.1 字段和方法 2.5 方法、自变量和返回值 2.5.1 自变量列表 2.6 构建Java程序 2.6.1 名字的可见性 2.6.2 使用其他组件 2.6.3 static关键字 2.7 我们的第一个Java程序 2.8 注释和嵌入文档 2.8.1 注释文档 2.8.2 具体语法 2.8.3 嵌入 2.8.4 @see:引用其他类 2.8.5 类文档标记 2.8.6 变量文档标记 2.8.7 方法文档标记 2.8.8 文档示例 2.9 编码样式 2.10 总结 2.11 练习 第3章 控制程序流程 3.1 使用Java运算符 3.1.1 优先级 3.1.2 赋值 3.1.3 算术运算符 3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14 Java没有“sizeof” 3.1.15 复习计算顺序 3.1.16 运算符总结 3.2 执行控制 3.2.1 真和假 3.2.3 反复 3.2.6 中断和继续 3.2.7 切换 3.3 总结 3.4 练习 第4章 初始化和清除 4.1 由构建器保证初始化 4.2 方法过载 4.2.1 区分过载方法 4.2.2 主类型的过载 4.2.3 返回值过载 4.2.4 默认构建器 4.2.5 this关键字 4.3 清除:收尾和垃圾收集 4.3.1 finalize()用途何在 4.3.2 必须执行清除 4.4 成员初始化 4.4.1 规定初始化 4.4.2 构建器初始化 4.5 数组初始化 4.5.1 多维数组 4.6 总结 4.7 练习 第5章 隐藏实施过程 5.1 包:库单元 5.1.1 创建独一无二的包名 5.1.2 自定义工具库 5.1.3 利用导入改变行为 5.1.4 包的停用 5.2 Java访问指示符 5.2.1 “友好的” 5.2.2 public:接口访问 5.2.3 private:不能接触 5.2.4 protected:“友好的一种” 5.3 接口与实现 5.4 类访问 5.5 总结 5.6 练习 第6章 类再生 6.1 合成的语法 6.2 继承的语法 6.2.1 初始化基础类 6.3 合成与继承的结合 6.3.1 确保正确的清除 6.3.2 名字的隐藏 6.4 到底选择合成还是继承 6.6 递增开发 6.7 上溯造型 6.7.1 何谓“上溯造型”? 6.8 final关键字 6.8.1 final数据 6.8.2 final方法 6.8.3 final类 6.8.4 final的注意事项 6.9 初始化和类装载 6.9.1 继承初始化 6.10 总结 6.11 练习 第7章 多形性 7.1 上溯造型 7.1.1 为什么要上溯造型 7.2 深入理解 7.2.1 方法调用的绑定 7.2.2 产生正确的行为 7.2.3 扩展性 7.3 覆盖与过载 7.4 抽象类和
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值