Thinking in Java-Object

话说:

各位读者,好久不见,寡人甚是惭愧。最近精力不济,下午好好打场羽毛球恢复下。
这篇博客开启《Thinking in Java》新篇章!这边博客记录Chapter1 -2的练习题。

难度系数:★★★★☆
建议用时:5H

目录


一、讲了什么?
二、习题
三、总结


一、讲了什么?

这本书适合有基础的看,什么基础水平呢?就是自己完整写过类似论坛、商城项目这样的水平,看这本书很好。

前2章讲得很好,反正笔者看完后感触有以下几点:
1)你知道为什么选择你当下的语言;为什么是Java而不是其他语言;
2)你知道了Java和C C++ Python等的异同;
3)你对Java的优点类似:简单易学、跨平台、垃圾回收机制、强大的类库、健壮性、安全性会有非常生动具体的理解,而不仅仅只是泛泛而谈。
4)一句话:有点基础看这些会极大的夯实基础。

还是那句话:在这个风云变幻的时代,什么都在变;如果有机会,一定要多花精力去把握那些不变的东西,比如基础。无论你学什么:计算机基础、网络基础、数据库基础、编程基础都是雷打不动的基本功。

二、习题
开发工具:Eclipse 2018.3 Oxygen
以下展示习题:

package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:01:22
*/
public class P1 {
    /**
     * Exercise 1 
     * p1:Create a class containing an int and a char that are not intitialized
    and print their values to verify that Java performs default initialization.
     */
    int a;
    char b;
    public static void main(String[]  args) {
        P1 p1  = new P1();
        System.out.println("a  "+p1.a);
        System.out.println("b" + p1.b);
        /**
         * a  0
            b
         */
    }

}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:13:56
Exercese 2
*/
public class HelloWorld {
    /**
     * p2:Following the HelloDate.java example in this chapter,
     * create a "Hello World" program that simply displays that statement
     */
    public static void main(String[] args) {
        System.out.println("Hello World!");
        //javac HelloWorld.java
        //java HelloWorld
    }




}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:26:31
Exercise 3
*/
public class ATNTest {
    /**
     * Find the code framents invoking ATypeName and turn them into that compiles and run.
     */

    public static void main(String[] args) {
        class ATypeName{
            int a;
            double b;
            boolean c;
            void show() {
                System.out.println(a);
                System.out.println(b);
                System.out.println(c);
            }
        }
        ATypeName  a = new ATypeName();
        a.a = 3;
        a.b = 3.2345;
        a.c = false;
        a.show();
    }

}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:32:56
Exercise 4
*/
public class DateOnlyTest {
    /**
     * Turn the DataOnly code fragments into a program that compiles and run .
     */

    public static void main(String[] args) {
        class DataOnly{
            int d;
            double e;
            boolean f;
            void show() {
                System.out.println(d);
                System.out.println(e);
                System.out.println(f);
            }
        }
        DataOnly data = new DataOnly();
        data.d = 15;
        data.e = 3.45667;
        data.f = false;
        data.show();

    }
}

Exercise5类似,不在赘述

package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:41:38
Exerxise 6
*/
public class StorageTest {
    /**
     * Write a program that includes and calls the storage() method defined as a code 
     * fragment in this chapter.
     */

    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"));//4
    }
}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午10:50:22

 * Exercise 7
 * Trun 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);
        System.out.println(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);
    }


}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月1日下午11:07:17

Exercise 8
Write a program that  demostrates that,no mater how many objects you create of a particular
class, there is  only on instance of  a particular static field of that class.
*/

class StaticTest2{
    static int i = 47;
}

class Incrementable2{
    static void increment() {
        StaticTest.i++;
    }
}
public class OneStaticTest {
    public static void main(String[] args) {
        System.out.println("StaticTest.i  "+StaticTest.i  );
        StaticTest2 st1 = new StaticTest2();
        StaticTest2 st2 = new StaticTest2();
        System.out.println("st1.i "+st1.i);
        System.out.println("st2.i "+st2.i);

        Incrementable2.increment();
        System.out.println("After Incrementable.increment() called:");
        System.out.println("st1.i "+st1.i);
        System.out.println("st2.i  "+st2.i);

        Incrementable2.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");
        StaticTest2 st3 = new StaticTest2();
        System.out.println("st3.i  "+st3.i);
        //这个感觉没啥可混淆的啊......
    }

}
package com.hmc.day01;


/**
Author:meice Huang
Time:2018年7月1日下午11:26:51

Exercise 9 
Write a program that demostrates 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);



    }
}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月2日上午12:37:57

Exercise 10 
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 {
    public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);
        System.out.println(args[2]);
    }
}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月2日上午12:42:43

Exercise 11
Turn the AllTheColorsOfTheRainbow 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;
    }
    //感觉这个案例处理变量名字长,考验你打字速度之外,没有啥特别意义。

}
package com.hmc.day01;

import java.util.Date;

/*
Author:meice Huang
Time:2018年7月2日上午1:02:50
*/

/*
 * Exercise 12
 * 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.
 * @author hmc
 *
 */

/**
 * The first Thinking  in Java example program.
 * Display a string and today's date.
 * @author hmc
 * @author dukegray.com
 * @version 0.1
 *
 */
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()) ;
    }
}
package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月3日上午1:32:35
*/

//Exercise 13 -1
/*
 * Run Documentation1.java ,Documentation2.java,Documentation3.java through javadoc.
 * Vertify 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() {}

}





package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月3日上午1:40:52
*/

import java.util.Date;

//Exercise 13 -2 
//object /Documentation2.java
/**
 * 
 * <pre>
 * Users
 * System.out.pringln(new Date());
 * </pre>
 *
 */
public class Documentation2 {
    Date d = new Date();
    void showDate() {
        System.out.println("Date= " + d);
    }
}





package com.hmc.day01;

import java.util.Date;

/**
Author:meice Huang
Time:2018年7月5日上午12:32:21
*/
/**
 * Exercise 13-3 
 * Run Documentation1.java,Documentation2.java and Document3.java
 *
 */
//Object/Documentation3.java
/**
 * You can even insert a list
 * <ol> 
 *      <li>Item one</li>
 *      <li>Item two </li>
 *      <li>Item three </li>
 * </ol>
 * <ul>
 *  <li>111</li>
 * <li>22222</li>
 * 
 * </ul>
 */
public class Documentation3 {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println("d =" + d);
    }

}
package com.hmc.day01;

import java.util.Date;

/**
Author:meice Huang
Time:2018年7月7日上午9:22:16
*/
/**
 * Exercise -14 
 * Add an HTML list of items to the documentation in the previous exercise.
 *
 */
/**
 * You can even insert a list
 * <ol>
     *  <li>Item one </li>
     * <li>Item two </li>
     * <li>Item three</li>
 * </ol>
 *
 *
 *Anotehr test list 
 *<ol>
 *      <li>One</li>
 *      <Li>Tow</li>
 *      <li>Three</li>
 *</ol>
 */
public class Documentation4 {
    /**
     * Let's try a pulic field list
     * <ol>
     *      <li>One</li>
     *      <li>Two</li>
     *      <li>Three</li>
     *      <li>Four</li>
     * </ol>
     * 
     */

    public  int i = 2;

    /**
     *A private field list 
     *<ol>
     *  <li>One</li>
     *  <li>Two</li>
     *  <li>Three</li>
     *</ol>
     */

    private int j  = 3;
    /**
     * Another list can be inserted here to help explain the following method all
     * <ol>
     *      <li>One </li>
     *      <li>Two </li>
     *      <li>Three </li>
     * </ol></br>
     * but may be formatted diffrently in Method Summary.
     */

    public static void main(String[] args) {
        /**
         * Let's try another test list here.
         * <ol>
         *      <li>One</li>
         *      <li>Tow</li>
         *      <li>Three</li>
         * </ol>
         */
        Date d = new Date();
        System.out.println("d"+d);

    }



}

Exercise 15过于简单,过。

package com.hmc.day01;
/**
Author:meice Huang
Time:2018年7月7日上午9:41:42

Exercise 16 
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.
*/
class Tree{
    int height;
    /**
     * no-argument constructor 
     * assigns height = 0 
     */
    Tree(){
        System.out.println("Planting a seeding ");
        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 height is "+height +"feet tall");
    }
    /**
     * overloaded method to print String argument and height of a tree object
     */
    void info(String s) {
        System.out.println("Tree is "+height +"feet tall");
    }
}
/**
 * class to test constructor of tree object.
 */
public class Overloading {
    public static void main(String[] args) {
        for(int i=0;i<5;i++) {
            Tree tree = new Tree(i);
            tree.info();
            tree.info("Overloading method");
        }
        new Tree();
    }
}

其中生成文档过程是这样的:

dzjdeMacBook-Air:~ dzj$ javadoc -d Document5/ Overloading.java 
正在加载源文件Overloading.java...
正在构造 Javadoc 信息...
标准 Doclet 版本 1.8.0_161
正在构建所有程序包和类的树...
正在生成Document5/com/hmc/day01/Overloading.html...
正在生成Document5/com/hmc/day01/package-frame.html...
正在生成Document5/com/hmc/day01/package-summary.html...
正在生成Document5/com/hmc/day01/package-tree.html...
正在生成Document5/constant-values.html...
正在构建所有程序包和类的索引...
正在生成Document5/overview-tree.html...
正在生成Document5/index-all.html...
正在生成Document5/deprecated-list.html...
正在构建所有类的索引...
正在生成Document5/allclasses-frame.html...
正在生成Document5/allclasses-noframe.html...
正在生成Document5/index.html...
正在生成Document5/help-doc.html...


最终会生成这么多文档:
dzjdeMacBook-Air:Document1 dzj$ ls
allclasses-frame.html   constant-values.html    index-all.html          package-list
allclasses-noframe.html deprecated-list.html    index.html              script.js
com                     help-doc.html           overview-tree.html      stylesheet.css
dzjdeMacBook-Air:Document1 dzj$ 

效果图:
这里写图片描述

三、总结*


1、基础得不能基础得东西就快速的过;
2、不要因为基础就忽略掉,恰恰因为基础中蕴含着大量重要的信息。
3、对文档注释了解更加深刻:javadoc -d 生成文档目录 目标文档


猜你喜欢

转载自blog.csdn.net/meiceatcsdn/article/details/80949596