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();
    }
}
///:~

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

猜你喜欢

转载自blog.csdn.net/summerSunStart/article/details/79409200