1. 程式人生 > >Thinking in Java 第四版完整版 第二章練習題

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

執行結果:
這裡寫圖片描述