java编程思想(第四版)练习题答案:一切都是对象

2.11 一切都是对象

练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化,将它们的值值打印出来,以验证java执行了默认初始化。

// TIJ4 Chapter Object, Exericise 1, page 89

// object/PrimitiveTest.java

// Create a class containing an int and a char that are not intitialized

// and print their values to verify that Java performs default initialization.

 

public class PrimitiveTest {

    static int i;

    static char c;   

    public static void main(String[] args) {     

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

       System.out.println("char = " + c);

    }

}

练习2:参照本章HelloDate.java这个例子,创建一个“Hello World”程序,该程序只要输出这句话即可。

// TIJ4 Chapter Object, Exericise 2, page 89

// object/HelloWorld.java

// Following the HelloDate.java example in this chapter, create a "hello, world"

// program that simply displays that statement.

 

public class HelloWorld {

    public static void main(String[] args) {     

       System.out.println("Hello World!");

    }

}

练习3:找到含有AtypeName的代码段,将其改写成完整的程序,然后编译、运行。

// TIJ4 Chapter Object, Exericise 3, page 90

// object/ATNTest.java

// Find the code fragments involving ATypeName and turn them into a program

// that compiles and runs.

 

public class ATNTest {     

    public static void main(String[] args) {

       class ATypeName {

           int i;

           double d;

           boolean b;

           void show() {

              System.out.println(i);

              System.out.println(d);

              System.out.println(b);  

           }

       }  

       ATypeName a = new ATypeName();

       a.i = 3;

       a.d = 2.71828;

       a.b = false;     

       a.show();

    }

}

练习4:将DataOnly代码段,将其改写成完整的程序,然后编译、运行。

// object/DataOnlyTest.java

// TIJ4 Chapter Object Exercise 4 page 90

// Turn the DataOnly code fragments into a program that compiles and runs

 

public class DataOnlyTest {

    public static void main(String[] args) {

       class DataOnly {

           int i;

           double d;

           boolean b;

           void show() {

              System.out.println(i);

              System.out.println(d);

              System.out.println(b);  

           }

       }  

       DataOnly data = new DataOnly();

       data.i = 3;

       data.d = 2.71828;

       data.b = false;     

       data.show();

    }

}

练习5:修改前一个练习,将DataOnly中的数据在main()方法中赋值并打印出来。

// object/DOTest2.java

// TIJ4 Chapter Object, Exercise 5, page 90

// Modify the previous exercise so that the values of the data in DataOnly are

// assigned to and printed in main().

 

public class DOTest2 {     

    public static void main(String[] args) {

       class DataOnly {

           int i;

           double d;

           boolean b;

           void show() {

              System.out.println(i);

              System.out.println(d);

              System.out.println(b);  

           }

       }  

       DataOnly data = new DataOnly();

       data.i = 234;

       data.d = 2.1234545;

       data.b = true;      

       data.show();

    }

}

练习6:编写一个程序,让它含有本章所定义的storage()方法的代码段,并调用之。

// object/StorageTest.java

// TIJ4 Chapter Object, Exercise 6, page 90

// Write a program that includes and calls the storage() method defined as a

// code fragment in this chapter.

 

public class StorageTest { 

    public static void main(String[] args) {

       class StoreStuff {

           int storage(String s) {

              return s.length() * 2;

           }  

       }

       StoreStuff x = new StoreStuff();

       System.out.println(x.storage("hi"));     

    }

}

练习7:将Incrementtable的代码段改成一个完整的可运行程序:

// object/ITest.java

// TIJ4 Chapter Object, Exercise 7, page 90

// Turn the Incrementable code fragments into a working program.

 

class StaticTest {

    static int i = 47;

}

class Incrementable {

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

}

public class ITest {

    public static void main(String[] args) {

    System.out.println("StaticTest.i= " + StaticTest.i);

    StaticTest st1 = new StaticTest();

    StaticTest st2 = new StaticTest();

    System.out.println("st1.i= " + st1.i);

    System.out.println("st2.i= " + st2.i);

    Incrementable sf = new Incrementable();

    sf.increment();

    System.out.println("After sf.increment() called: ");

    System.out.println("st1.i = " + st1.i);

    System.out.println("st2.i = " + st2.i);

    Incrementable.increment();

    System.out.println("After Incrementable.increment called: ");

    System.out.println("st1.i = " + st1.i);

    System.out.println("st2.i = " + st2.i);

    }

}

练习8:编写一个程序,展示无论你创建了某个特定类的多少个对象,这个类中的某个特定的static域只有一个实例。

// object/OneStaticTest.java

// TIJ4 Chapter Object, Exercise 8, page 90

/* Write a program that demonstrates that, no matter how many objects you

* create of a particular class, there is only one instance of a particular

* static field of that class.

*/

 

class StaticTest {

    static int i = 47;

}

 

class Incrementable {

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

}

 

public class OneStaticTest {

    public static void main(String[] args) {

       System.out.println("StaticTest.i= " + StaticTest.i);

       StaticTest st1 = new StaticTest();

       StaticTest st2 = new StaticTest();

       System.out.println("st1.i= " + st1.i);

       System.out.println("st2.i= " + st2.i);

       Incrementable.increment();

       System.out.println("After Incrementable.increment() called: ");

       System.out.println("st1.i = " + st1.i);

       System.out.println("st2.i = " + st2.i);

       Incrementable.increment();

       System.out.println("After Incrementable.increment called: ");

       System.out.println("st1.i = " + st1.i);

       System.out.println("st2.i = " + st2.i);

       st1.i = 3;

       System.out.println("After st1.i = 3, ");

       System.out.println("st1.i = " + st1.i);

       System.out.println("st2.i = " + st2.i);

       System.out.println("Create another StaticTest, st3.");

       StaticTest st3 = new StaticTest();

       System.out.println("st3.i = " + st3.i);

    }

}

练习9:编写一个程序,展示自动包装功能对所有的基本类型和包装器类型都起作用

// TIJ4 Chapter Object, Exercise 9, page 90

// Write a program that demonstrates that autoboxing works for all the primitive // types and their wrappers.

 

public class AutoboxTest {

    public static void main(String[] args) {

       boolean b = false;

       char c = 'x';

       byte t = 8;

       short s = 16;

       int i = 32;

       long l = 64;

       float f = 0.32f;

       double d = 0.64;

       Boolean B = b;

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

       System.out.println("Boolean B = " + B);

       Character C = c;

       System.out.println("char c = " + c);

       System.out.println("Character C = " + C);

       Byte T = t;

       System.out.println("byte t = " + t);

       System.out.println("Byte T = " + T);

       Short S = s;

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

       System.out.println("Short S = " + S);

       Integer I = i;

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

       System.out.println("Integer I = " + I);

       Long L = l;

       System.out.println("long l = " + l);

       System.out.println("Long L = " + L);

       Float F = f;

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

       System.out.println("Float F = " + F);

       Double D = d;

       System.out.println("double d = " + d);

       System.out.println("Double D = " + D);   

    }

}

练习10:编写一个程序,打印出从命令行获得的三个参数。为此,需要确定命令行数组中String的下标。

// TIJ4 Chapter Object, Exercise 10, page 90

// 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 CommandArgTest_10 {

    public static void main(String[] args) {

        int[] a = {1, 2, 3};

        System.out.println("a[0] = " + a[0]);

        System.out.println("a[1] = " + a[1]);

        System.out.println("a[2] = " + a[2]);

    }

}

练习11:将AllTheColorsOfTheRainbow这个示例改写成一个程序,然后编译、运行。

// object/Rainbow.java

// TIJ4 Chapter Object, Exercise 11, page 90

// Turn the AllColorsOfTheRainbow into a program that compiles and runs.

 

public class Rainbow {

    public static void main(String[] args) {

       AllTheColorsOfTheRainbow atc = new AllTheColorsOfTheRainbow();

       System.out.println("atc.anIntegerRepresentingColors = " + atc.anIntegerRepresentingColors);

       atc.changeColor(7);

       atc.changeTheHueOfTheColor(77);

       System.out.println("After color change, atc.anIntegerRepresentingColors = " + atc.anIntegerRepresentingColors);

       System.out.println("atc.hue = " + atc.hue);  

    }

}

 

class AllTheColorsOfTheRainbow {

    int anIntegerRepresentingColors = 0;

    int hue = 0;

    void changeTheHueOfTheColor(int newHue) {

       hue = newHue;

    }

    int changeColor(int newColor) {

       return anIntegerRepresentingColors = newColor;      

    }

}

练习12:找出HelloDate.java第二版本,也就是简单注释文档的示例。对该文档执行javadoc,然后通过web浏览器验证所产生的文档。

// object.DocTest.java

// TIJ4 Chapter Object, Exercise 12, page 90

/* 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 Burce Eckel

* @author www.MindView.net

* @version 4.0

*/

 

public class DocTest {

    /** Entry poing 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)

*/

练习13:通过javadoc运行Documentation1.java、Documentation2.java、Documentation3.java,然后通过web浏览器观看运行结果。

13-1

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 1

/* Run Documentation1.java, Documentation2.java and Documentation3.java

* through Javadoc. Verify the resulting documentation with your Web browser.

*/

 

/** A class comment */

public class Documentation1 {

    /** A field comment */

    public int i;

    /** A method comment */

    public void f() {}

}

13-2

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 2

/* Run Documentation1.java, Documentation2.java and Documentation3.java

* through Javadoc. Verify the resulting documentation with your Web browser.

*/

import java.util.*;

 

// object/Documentation2.java

/**

*

* Uses

* System.out.println(new Date());

*

*/

 

public class Documentation2 {

    Date d = new Date();

    void showDate() {

     System.out.println("Date = " + d);

    }

}

13-3

// object.Documentation1.java

// TIJ4 Chapter Object, Exercise 13 - 3

/* Run Documentation1.java, Documentation2.java and Documentation3.java

* through Javadoc. Verify the resulting documentation with your Web browser.

*/

import java.util.*;

 

// object/Documentation3.java

/**

* You can even insert a list:

*

    *

  1. Item one

    *

  2. Item two

    *

  3. Item three

    *

*/

 

public class Documentation3 {

    public static void main(String[] args) {

       Date d = new Date();

       System.out.println("d = " + d);

    }

}

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

// object/Documentation4.java

// TIJ4 Chapter Object, Exercise 14, page 90

// Add an HTML list of items to the documentation in the previous exercise.

import java.util.*;

 

// object/Documentation4.java

/**

* You can even insert a list:

*

    *

  1. Item one

    *

  2. Item two

    *

  3. Item three

    *

 

 

      

       * Another test list

       *

           *

  1. One

           *

  2. Two

           *

  3. Three

           *

       */ 

 

public class Documentation4 {

 

       /** Let's try a public field list

       *

           *

  1. One

           *

  2. Two

           *

  3. Three

           *

       */ 

      

       public int i = 2;

 

       /**

       * A private field list (-private to see)

       *

           *

  1. One

           *

  2. Two

           *

  3. Three

           *

       */ 

 

       private int j = 3;

 

       /**

       * Another list can be inserted here to help explain the

       * following method call

       *

           *

  1. One

           *

  2. Two

           *

  3. Three

           *


       * but may be formatted differently in Method Summary

       */ 

 

    public static void main(String[] args) {

 

       /**

       * Let's try another test list here

       *

           *

  1. One

           *

  2. Two

           *

  3. Three

           *

       */ 

 

       Date d = new Date();

       System.out.println("d = " + d);

    }

}

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

// object/HelloDocTest.java

// TIJ4 Chapter Object, Exercies 15, page 91

/* Take the program in Exercise 2 and add comment documentation to it. Extract

* this comment documentation into an HTML file using Javadoc and view it with

* your Web browser.

*/

 

 

/**

* Public class contained in file of the same name that includes main()

*/

 

public class HelloDocTest {

 

    /** main method executed by java

    */ 

 

    public static void main(String[] args) {

    System.out.println("Hello World!");

    }

}

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

// object/Overloading.java

// TIJ4 Chapter Object, Exercise 16, page 91

/* In the Initialization and Cleanup chapter, locate the Overloading.java

* example and add Javadoc documentation. Extract this comment documentation

* into and HTML file using Javadoc and view it with your Web browser.

*/

 

// initialization/Overloading.java

// Demonstration of both constructor

// and ordinary method overloading.

 

/** creates type Tree wth two constructors and one info method

*/

 

class Tree {

    int height;

   

    /** no-argument constructor

    * assigns height = 0

    */

 

    Tree() {

       System.out.println("Planting a seedling");

       height = 0;

    }

 

    /** constructor taking an int argument,

    * assigns height that int argument

    */

 

    Tree(int initialHeight) {

       height = initialHeight;

       System.out.println("Creating new tree that is " + height + " feet tall");

    }

 

    /** method to print height of tree object

    */

   

    void info() {

       System.out.println("Tree is " + height + " feet tall");

    }

 

    /** overloaded method to print string argument

    * and height of a tree object

    */

 

    void info(String s) {

       System.out.println(s + ": Tree is " + height + " feet tall");

    }

}

 

/** class to test construction of tree objects

*/

 

public class Overloading {

    public static void main(String[] args) {

       for(int i = 0; i < 5; i++) {

           Tree t = new Tree(i);

           t.info();

           t.info("overloading method");

       }

       // Overloaded constructor:

       new Tree();

    }

}

 

你可能感兴趣的:(java菜鸟)