第二章练习题(Chapter2)

*本文章仅供参考,习题的答案可以从www.mindviewinc.com处购买名为“The Thinking in Java Annotated Solution Guide”的电子文档中查找。

  • 练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化,将他们的值打印出来,以验证Java执行了默认初始化。
class Initialization {
    int a;
    char b;
}

public class testInitialization {
    public static void main(String[] args) {
        Initialization ini = new Initialization();
        System.out.println("1." + ini.a);
        System.out.println("2." + ini.b);
    }
}
  • 练习2:参照本章的HelloDate.java这个例子,创建一个“Hello, World”程序,该程序只要输出这句话即可,你所编写的类里只需一个方法(即“main”方法,在程序启动时被执行)。只要记住把它设为static形式,并指定参数列表-即使根本不会用到这个列表。用javac进行编译,再用java运行它。如果你使用的是不同于JDK的开发环境,请了解如何在你的环境中进行编译和运行。
  • /**The first Thinking in Java example program.
     * Display a string and today's date.
     *@author zhangjian
     *@author Bruce Eckel
     *@version 4.0
     */
    
    /**
     * Entrv point to class & application.
     * 
     * @param args
     *            array of string arguments
     * @throws exceptions
     *             No exceptions thrown
     *
     *             <pre>
     *             System.out.println(new Date());
     *             </pre>
     */
    public class testHello {
    
        public static void main(String[] args) {
            System.out.println("Hello wrold! It is:");
            System.out.println(new Date());
        }
    }
    
  • 练习3:找出含有ATypeName的代码段,将其改写成完整的程序,然后编译、运行。
  • public class testATypeName {
        public static void main(String[] args) {
            testATypeName atn = new testATypeName();
            atn.getClass();
        }
    }
    
  • 练习4:将DataOnly代码段改写成一个程序,然后编译、运行。
  • class DataOnly {
        int i;
        double d;
        boolean b;
    }
    
    public class testDataOnly {
        public static void main(String[] args) {
            DataOnly d = new DataOnly();
            System.out.println(d.i);
            System.out.println(d.d);
            System.out.println(d.b);
        }
    }
  • 练习5:修改前一个练习,将DataOnly中的数据在main()方法中赋值并打印出来。
  • class DataOnly {
        int i;
        float f;
        boolean b;
    }
    
    public class testDataOnly2 {
        public static void main(String[] args) {
            DataOnly d1 = new DataOnly();
            d1.i = 12;
            System.out.println(d1.i);
            d1.f = 12.0f;
            System.out.println(d1.f);
            d1.b = true;
            System.out.println(d1.b);
        }
    }
  • 练习6:编写一个程序,让它含有本章所定义的storage()方法的代码段,并调用之。
  • class testStorage {
        String s = "Hello world !";
    
        private int storage(String s) {
            return s.length() * 2;
        }
    
        public void print() {
            System.out.println("storage(s) is:" + storage(s));
        }
    }
    
    public class testStorageMethod {
        public static void main(String[] args) {
            testStorage ts = new testStorage();
            ts.print();
        }
    }
  • 练习7:将Incrementable的代码段改写成一个完整的可运行程序。
  • class StaticTest{
        static int i = 47;
    }
    
    class Incrementable{
        static void incrementable(){
            StaticTest.i++;
        }
    }
    
    public class testIncrementable {
        public static void main(String[] args) {
            Incrementable.incrementable();
            System.out.println(StaticTest.i);
        }
    }
    
  • 练习8:编写一个程序,展示无论你创建了某个特定类的多少个对象,这个类中的某个特定的static域只有一个实例。
  • class TestStatic1 {
        static int i = 12;
    
        static void addi() {
            i++;
        }
    }
    
    public class testStatic {
        public static void main(String[] args) {
            TestStatic1 ts0 = new TestStatic1();
            System.out.println(ts0.i);
            TestStatic1 ts1 = new TestStatic1();
            System.out.println(ts1.i);
            ts0.addi();
            ts1.addi();
            TestStatic1 ts2 = new TestStatic1();
            System.out.println(ts2.i);
        }
    }
  • 练习9:编写一个程序,展示自动包装功能对所有的基本类型和包装器类型都起作用。
  • public class AutoboxingTest {
        public static void main(String[] args) {
            boolean bn = true;
            byte bt = 8;
            char c = 'c';
            short s = 16;
            int i = 32;
            long l = 64;
            float f = 32.32f;
            double d = 64.64;
            Boolean BN = bn;
            Byte BT = bt;
            Character C = c;
            Short S = s;
            Integer I = i;
            Long L = l;
            Float F = f;
            Double D = d;
            System.out.println("boolean bn = " + bn);
            System.out.println("Boolean BN = " + BN);
            System.out.println();
            System.out.println("byte bt = " + bt);
            System.out.println("Byte BT = " + BT);
            System.out.println();
            System.out.println("char c = " + c);
            System.out.println("Character C = " + C);
            System.out.println();
            System.out.println("short s = " + s);
            System.out.println("Short S = " + S);
            System.out.println();
            System.out.println("int i = " + i);
            System.out.println("Integer I = " + I);
            System.out.println();
            System.out.println("long l = " + l);
            System.out.println("Long L = " + L);
            System.out.println();
            System.out.println("float f = " + f);
            System.out.println("Float F = " + F);
            System.out.println();
            System.out.println("double d = " + d);
            System.out.println("Double D = " + D);
            System.out.println();
            System.out.println("--------------------");
            /* 装箱 */
            Integer a = new Integer(12);
            Character ch = new Character('a');
            System.out.println(ch + " = " + a);
    
            System.out.println("--------------------");
    
            /* 拆箱 */
            int b = new Integer(12);
            char chr = new Character('b');
            System.out.println(chr + " = " + b);
        }
    }
  • 练习10:编写一个程序,打印从命令行获取的三个参数。为此,需要确定命令行数组中String的下标。
  • public class ArgsTest {
        public static void main(String[] args) {
            if (args.length < 3) {
                System.out.println("No 3 arguments.");
                System.exit(1);
            }
            System.out.println(args[0]);
            System.out.println(args[1]);
            System.out.println(args[2]);
        }
    }
  • 练习11:将AllTheColorsOfTheRainbow这个示例改写成一个程序,然后编译、运行。
  • public class AllTheColorsOfTheRainbow {
        int anIntegerRepresentingColors;
    
        void changeTheHueOfTheColor(int newHue) {
            anIntegerRepresentingColors = newHue;
        }
    
        public static void main(String[] args) {
            AllTheColorsOfTheRainbow all = new AllTheColorsOfTheRainbow();
            all.changeTheHueOfTheColor(12);
            System.out.println(all.anIntegerRepresentingColors);
        }
    }
  • 练习12:找出HelloDate.java的第二版本,也就是那个简单注释文档的示例。对该文件执行javadoc,然后通过Web浏览器观看运行结果。
  • 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 javadocHelloDate {
        /**
         * Entrv point 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());
        }
    }
    
    /* 可以用命令行形式实现 javadoc -help 
     * javadoc -d [输出文件保存目录] nameOfPackage 
     * 可以用Eclipse实现
     * Project->Generate javadoc...
     */
    
  • 练习13:通过Javadoc运行Documentation1.javaDocumentation2.javaDocumentation3.java,然后通过Web浏览器验证所产生的文档。
  • /** A class comment */
    public class Documentation1 {
        /** A field comment */
        public int i;
    
        /** A method comment */
        public void f() {
        }
    }
    
    /**
     * <pre>
     * System.out.println(new Date());
     * </pre>
     */
    
    public class Documentation2 {
    
    }
    
    /* 生成javadoc文档,必须有可以文档化的公共或受保护的类   */
    /**
     * You can <em>even</em> insert a list:
     * <ol>
     * <li>Item one</li>
     * <li>Item two</li>
     * <li>Item three</li>
     * </ol>
     */
    
    public class Documentation3 {
    
    }
    
    /* 生成javadoc文档,必须有可以文档化的公共或受保护的类   */
  • 练习14:在前一个练习的文档中加入各项的HTML列表。
  • /* javadoc只能为public和protected成员进行文档注释。
     * 但是在测试过程中,只有public成员可以进行文档注释。WHY?
     * Eclipse中有权限选择,选择private则会显示所有类型成员。默认public。
     * 命令行形式可以在要进行文档注释的类名前加 -private ,则显示所拥有类型成员。默认public和protected。
     */
    
    
    /**
     * Menu <menu>
     * <li>class one
     * <ul>
     * <li>method one</li>
     * <li>method two</li>
     * <li>method three</li>
     * </ul>
     * </li>
     * <li>class two</li>
     * <li>class three</li></menu>
     */
    public class HtmlListTest {
        /** MethodOne */
        public void f1() {
        }
    
        /** MethodTwo */
        public void f2() {
        }
    
        /** MethodThree */
        void f3() {
        }
    }
    
    /** ClassOne */
    class One {
    }
    
    /** ClassTwo */
    class Two {
    }
    /**
     * List1(ol):
     * <ol>
     * <li>filed one</li>
     * <li>filed two</li>
     * <li>filed three</li>
     * </ol>
     */
    
    public class List1 {
        /** FiledOne */
        public int i;
        /** FiledTwo */
        protected int j;
        /** FiledThree */
        private int k;
    }
    /**
     * List2(ul):
     * <ul>
     * <li>bananas</li>
     * <li>apples</li>
     * <li>plums</li>
     * </ul>
     */
    
    public class List2 {
    
    }
    /**
     * List3(dl)
     * <dl>
     * <dt>Apple</dt>
     * <dd>The apple is the pomaceous fruit of the apple tree</dd>
     * <dd><i>Musa domestica</i></dd>
     * <dt>Banana</dt>
     * <dd>The banana is the parthenocarpic fruit of the banana tree</dd>
     * <dd><i>Musa acuminata</i></dd>
     * <dt>Cherry</dt>
     * <dd>The cherry is the stone fruit of the genus <i>Prunus</i></dd>
     * </dl>
     */
    
    public class List3 {
    
    }
  • 练习15:使用练习2的程序,加入注释文档。用javadoc提取此注释文档,并产生一个HTML文件,然后通过Web浏览器查看结果。
  • import java.util.Date;
    
    /**
     * 这是一个显示系统时间的类。
     * 
     * @author Bruce Eckel 
     * @author 这是一个链接<a>http://www.MindView.net</a>
     * @version 4.0
     */
    
    public class JavadocExercise2HelloDate {
        /**
         * 主函数
         * 
         * @param args
         *            字符串型数组参数
         * @return 返回一个整数0
         */
        public static int main(String[] args) {
            System.out.println("Hello, it's: ");
            System.out.println(new Date());
            return 0;
        }
    }
  • 练习16:找到第5章中的Overloading.java示例,并为它加入javadoc文档。然后用javadoc提取此注释文档,并产生一个HTML文件,最后,通过Web浏览器查看结果。
  • //Demonstration of both constructor
    //and ordinary method overloading
    
    /** 方法重载示例类 */
    class Tree {
        /** A filed comment */
        int height;
    
        /** 默认构造器:用标准方式初始化 */
        Tree() {
            System.out.println("Planting a seedling");
            height = 0;
        }
    
        /**
         * 方法重载后的构造器:读取信来初始化
         * 
         * @param initialHeight
         *            取int型initialHeight作为形式参数,该整型数表示height的初始值
         */
        Tree(int initialHeight) {
            height = initialHeight;
            System.out.println("Creating new Tree that is " + height + " feet tall");
        }
    
        /** 信息显示方法 */
        void info() {
            System.out.println("Tree is " + height + " feet tall");
        }
    
        /**
         * 信息显示方法重载
         * 
         * @param s
         *            取字符串s作为形式参数,该字符串表示重载后的方法进行的信息显示
         */
        void info(String s) {
            System.out.println(s + ": Tree is " + height + " feet tall");
        }
    }
    
    /**
     * 方法重载
     * 
     * @author Bruce Eckel
     * @author www.MindView.net
     * @version 4.0
     */
    public class Overloading {
        /**
         * The <code>main()</code> function which is called when the program is
         * executed by saying <code>java Overloading</code>.
         * 
         * @param args
         *            array passed from the command-line
         */
        public static void main(String[] args) {
            for (int i = 0; i < 5; i++) {
                Tree t = new Tree(i);
                t.info();
                t.info("overloaded method");
            }
        }
    }

    猜你喜欢

    转载自blog.csdn.net/sinat_32469267/article/details/73612437