不知道楼主要干嘛,
从Collection开始往后的几章倒是可以做一下子!

解决方案 »

  1.   

    公布答案
    Exercise 1
    //: c02:E01_HelloWorld.java
    //+M java E01_HelloWorld
    /****************** Exercise 1 ******************
     * Following the HelloDate.java example in this
     * chapter, create a "hello, world" program that
     * simply prints out that statement. You need
     * only a single method in your class (the "main"
     * one that gets executed when the program
     * starts). Remember to make it static and to
     * include the argument list, even though you
     * don't use the argument list. Compile the
     * program with javac and run it using java. If
     * you are using a different development
     * environment than the JDK, learn how to compile
     * and run programs in that environment.
     ***********************************************/
    public class E01_HelloWorld {
      public static void main(String args[]) {
        System.out.println("Hello, world!");
      }
    } ///:~
    The +M directive tells my makefile builder tool to add this command (java HelloWorld) to the commands that it automatically runs while building the program. This way, if the execution fails for any reason then the make will abort and show that something is wrong for a particular program. This is a very simple form of built-in testing: automatically running the program to ensure no exceptions are thrown. You’ll see this on most of the programs in this solution guide, and the makefiles that come with the source code (that is packaged with this guide) will automatically run the programs.
    Exercise 2
    //: c02:E02_ATypeName.java
    //+M java E02_ATypeName
    /****************** Exercise 2 ******************
     * Find the code fragments involving ATypeName
     * and turn them into a program that compiles and
     * runs.
     ***********************************************/
    public class E02_ATypeName { 
      public static void main(String args[]) {
        E02_ATypeName a = new E02_ATypeName();
      }
    } ///:~
    Exercise 3
    //: c02:E03_DataOnly.java
    //+M java E03_DataOnly
    /****************** Exercise 3 ******************
     * Turn the DataOnly code fragments into a
     * program that compiles and runs.
     ***********************************************/
    public class E03_DataOnly {
      int i;
      float f;
      boolean b;
      public static void main(String[] args) {
        E03_DataOnly d = new E03_DataOnly();
        d.i = 47;
        d.f = 1.1f;
        d.b = false;
      }
    } ///:~
    Exercise 4
    //: c02:E04_DataOnly2.java
    //+M java E04_DataOnly2
    /****************** Exercise 4 ******************
     * Modify Exercise 3 so that the values of the
     * data in DataOnly are assigned to and printed
     * in main().
     ***********************************************/
    public class E04_DataOnly2 {
      int i;
      float f;
      boolean b;
      public static void main(String[] args) {
        E04_DataOnly2 d = new E04_DataOnly2();
        d.i = 47;
        System.out.println("d.i = " + d.i);
        d.f = 1.1f;
        System.out.println("d.f = " + d.f);
        d.b = false;
        System.out.println("d.b = " + d.b);
      }
    } ///:~
    Exercise 5
    //: c02:E05_Storage.java
    //+M java E05_Storage
    /****************** Exercise 5 ******************
     * Write a program that includes and calls the
     * storage() method defined as a code fragment in
     * this chapter.
     ***********************************************/
    public class E05_Storage {
      String s = "Hello, World!";
      int storage(String s) {
        return s.length() * 2;
      }
      void print() {
        System.out.println(
          "storage(s) = " + storage(s));
      }
      public static void main(String[] args) {
        E05_Storage st = new E05_Storage();
        st.print();
      }
    } ///:~
    You could have also made storage( ) a static method and just called it from main( ).
    Exercise 6
    //: c02:E06_StaticFun.java
    //+M java E06_StaticFun
    /****************** Exercise 6 ******************
     * Turn the StaticFun code fragments into a
     * working program.
     ***********************************************/
    class StaticTest {
        static int i = 47;
    }public class E06_StaticFun {
      static void incr() { StaticTest.i++; }
      public static void main(String[] args) {
        E06_StaticFun sf = new E06_StaticFun();
        sf.incr();
        E06_StaticFun.incr();
      }
    } ///:~
    Exercise 7
    //: c02:E07_ShowArgs.java
    //+M java E07_ShowArgs A B C
    /****************** Exercise 7 ******************
     * Write a program that prints three arguments
     * taken from the command line. To do this,
     * you'll need to index into the command-line
     * array of Strings.
     ***********************************************/
    public class E07_ShowArgs {
      public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);
        System.out.println(args[2]);
      }
    } ///:~
    Note that you’ll get a run-time exception if you run the program without enough arguments. You should actually test for the length of the array first, like this:
    //: c02:E07_ShowArgs2.java
    //+M java E07_ShowArgs2 A B C
    /****************** Exercise 7 ******************
     * Testing for the length of the array first.
     ***********************************************/
    public class E07_ShowArgs2 {
      public static void main(String[] args) {
        if(args.length < 3) {
          System.out.println("Need 3 arguments");
          System.exit(1);
        }
        System.out.println(args[0]);
        System.out.println(args[1]);
        System.out.println(args[2]);
      }
    } ///:~
    The System.exit( ) call terminates the program and passes its argument back to the operating system as a result (with most operating systems, a non-zero result indicates that the program execution failed).
      

  2.   

    Exercise 8
    //: c02:E08_AllTheColorsOfTheRainbow.java
    //+M java E08_AllTheColorsOfTheRainbow
    /****************** Exercise 8 ******************
     * Turn the AllTheColorsOfTheRainbow example into
     * a program that compiles and runs.
     ***********************************************/
    public class E08_AllTheColorsOfTheRainbow {
      int anIntegerRepresentingColors;
      void changeTheHueOfTheColor(int newHue) {
        anIntegerRepresentingColors = newHue;
      }
      public static void main(String[] args) {
        E08_AllTheColorsOfTheRainbow all = 
          new E08_AllTheColorsOfTheRainbow();
        all.changeTheHueOfTheColor(27);
      }
    } ///:~
    Exercise 9
    //: c02:E09_HelloDate.java
    //+M java E09_HelloDate
    //+M mkdir HelloDate
    //+M javadoc E09_HelloDate.java –d HelloDate
    /****************** Exercise 9 ******************
     * Find the code for the second version of
     * HelloDate.java, which is the simple comment
     * documentation example. Execute javadoc on the
     * file and view the results with your Web
     * browser.
     ***********************************************/
    import java.util.*;/** The first Thinking in Java example program.
     * Displays a string and today's date.
     * @author Bruce Eckel
     * @author www.BruceEckel.com
     * @version 2.0 
    */
    public class E09_HelloDate {
      /** Sole entry point to class & application
       * @param args array of string arguments
       * @return No return value
       * @exception exceptions No exceptions thrown
      */
      public static void main(String[] args) {
        System.out.println("Hello, it's: ");
        System.out.println(new Date());
      }
    } ///:~
    The command to run javadoc on the file and to place the results in a separate subdirectory called HelloDate is embedded as the third +M comment directive above. Note that javadoc doesn’t automatically create the destination directory.
    Exercise 10
    //: c02:E10_docTest.java
    //+M mkdir docTest
    //+M javadoc E10_docTest.java –d docTest
    /****************** Exercise 10 *****************
     * Turn docTest into a file that compiles and
     * then run it through javadoc. Verify the
     * resulting documentation with your Web browser.
     ***********************************************/
    /** A class comment */
    public class E10_docTest {
      /** A variable comment */
      public int i;
      /** A method comment */
      public void f() {}
    } ///:~
    You must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +M comment directives. Note that we’re only concerned that the file compiles, so there’s no makefile directive to run it.
    Exercise 11
    //: c02:E11_docTest2.java
    //+M mkdir docTest2
    //+M javadoc E11_docTest2.java –d docTest2
    /****************** Exercise 11 *****************
     * Add an HTML list of items to the documentation
     * in Exercise 10.
     ***********************************************/
    /** A class comment
    * <pre>
    * System.out.println(new Date());
    * </pre>
    */
    public class E11_docTest2 {
      /** A variable comment */
      public int i;
      /** A method comment
      * You can <em>even</em> insert a list:
      * <ol>
      * <li> Item one
      * <li> Item two
      * <li> Item three
      * </ol>
      */
      public void f() {}
    } ///:~
    I simply added the HTML code fragments from the chapter examples.
    You must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +M comment directives.
    Exercise 12
    //: c02:E12_HelloWorldDoc.java
    //+M java E12_HelloWorldDoc
    //+M mkdir HelloWorldDoc
    //+M javadoc E12_HelloWorldDoc.java –d HelloWorldDoc
    /****************** Exercise 12 *****************
     * Take the program in Exercise 1 and add comment
     * documentation to it. Extract this comment
     * documentation into an HTML file using javadoc
     * and view it with your Web browser.
     ***********************************************/
    /** A first example from <I>Thinking in Java,
    * 2nd edition</I>. Demonstrates the basic class
    * structure and the creation of a 
    * <code>main()</code> function.
    */
    public class E12_HelloWorldDoc {
      /** The <code>main()</code> function which is
      called when the program is executed by saying
      <code>java E12_HelloWorldDoc</code>.
      @param args array passed from the command-line
      */
      public static void main(String args[]) {
        System.out.println("Hello, world!");
      }
    } ///:~
      

  3.   

    我也看过thinking in java 但是后面的习题没有仔细的做过支持楼主 打好基础是必要的也很感谢楼上的贴代码出来
      

  4.   

    大家还有注意一下第三章(P133)中的第七题中说要用if-then-else语句,可是好些jdk不能编译此语句吧,大家想想看是不是?
      

  5.   

    java里没有if-than-else。
    书上肯定是弄错了。
      

  6.   

    以下是我写的答案,不保证全对.1,
    public class HelloWorld{
      public static void main(String[] args){
       System.out.println("Hello World");
      }
    }2,
    public class ATypeName
    {    public ATypeName()
        {
        }    public static void main(String args[])
        {
            ATypeName atypename = new ATypeName();
        }
    }
    3,
    public class DataOnly{
      int i;
      float f;
      boolean b;
      
        public static void main(String[] args){
        DataOnly d = new DataOnly();
        
        d.i=47;
        d.f=1.1f;
        d.b=false;
        
        System.out.println(d.i);
        System.out.println(d.f);
        System.out.println(d.b);
      }
    }
    4,第3题和第4题差不多,以上的第3题已经包含了第4题,不过我觉得打印那个函数,我写的比较丑陋,看了一下 catblue(limiaomiao) 公布的答案,这样写比较好:System.out.println("d.i = " + d.i);
    5,
    public class Storage{
      static int storage(String s){
       return s.length()*2;
      }
      public static void main(String[] args){
       System.out.println(storage("xuduo"));
      }
    }
    /**
    第5题目有个要注意的地方,就是non-static函数不能引用来自static context..
    所以我在int前加上了static 编译通过了。..汗.. 不过作者的做法是写一个打印函数print()*/
    6,
    public class StaticFun{
      static int i = 47;
      static void incr() {StaticFun.i++;}
      
       public static void main(String[] args){
         StaticFun sf = new StaticFun();
         sf.incr();
         StaticFun.incr();
        // System.out.println(sf.incr());这里注解掉了,编译的时候发现return void的不能放在System.out.println里.
       }
    }
      
      

  7.   

    7,
    我没读懂题目的意思,所以直接看了答案:
    public class E07_ShowArgs {
      public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);
        System.out.println(args[2]);
      }
    } ///:~以上编译通过的,java的时候出现:Exception in thread: "main" java.lang......0: at E07_ShowArgs.main(ShowArgs.java:3)不清楚是什么意思,谁能帮助解释一下.?
      

  8.   

    8,
    class AllTheColorsOfTheRainbow{
      int anIntegerRepresentingColors;
      void changeTheHueofTheColor(int newHue){ 
       //anIntegerRepresentingColors = newHue;
       }
        
        public static void main(String[] args){
         AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
          a.changeTheHueofTheColor(10);
        }
    }