Java programming thinking exercises (a)

First, create a class that contains a domain int and a char field, they have not been initialized, print out their values ​​to verify the implementation of the Java default initialization. 

private static char c;
private static int i;

public static void main(String[] args){
    System.out.println(String.valueOf(c));
    System.out.println(String.valueOf(i));
}

return value:

 

0

Two, HelloDate.java this example with reference to this chapter, create a "Hello, World" program, which can be as long as the output of this sentence.

The book HelloDate Source:

import java.util.*;

public class HelloDate {
    public static void main(String[] args){
        System.out.println("Hello, it's:");
        System.out.println(new Date());
    }
}

After modifying HelloWorld Source:

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello, World");
    }
}

Return value: Hello, World

Third, find out the code segment containing ATypeName, to read the full program.

public class ATypeName {
    /* Class body goes here */
    private String id;
    private String name;
    public ATypeName() {
    }
    public ATypeName(String id, String name) {
        this.id = id; this.name = name; } @Override public String toString() { return "ATypeName{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } } public class Main{ public static void main(String[] args){ ATypeName a = new ATypeName(1,"张三"); System.out.println(a.toString()); } }

Returns: ATypeName {id = '1', name = 'John Doe'}

Fourth, the DataOnly rewritten into a program code segment. slightly.

Fifth, before modifying a contact, the data DataOnly the rewrite mian assignment and print method (). Referring to (c)

Sixth, write a program, it contains storage, as defined in this chapter () method of the code segment, and call it. 

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println(storage("HelloWorld"));
    }
    private static int storage(String s) {
        return s.length() * 2;
    }
}

Returns: 20

Seven, the Incrementtable code rewritten into a complete executable. 

public class StaticTest {
    static int i = 47;
}
public class Incrementtable {
   static void increment(){
       StaticTest.i++;
   }
}
public class HelloWorld { public static void main(String[] args){ System.out.println("before StaticTest.i:" + StaticTest.i); Incrementtable.increment(); System.out.println("after StaticTest.i:" + StaticTest.i); } }

return:

before StaticTest.i:47
after StaticTest.i:48

Eight, write a program that shows no matter how many you create objects of a particular class, a particular static field of this class is only one object.

public class StaticTest {
    static String s = new String("HelloWorld");
}
public class HelloWorld {
    public static void main(String[] args) {
        StaticTest st1 = new StaticTest();
        StaticTest st2 = new StaticTest(); StaticTest st3 = new StaticTest(); StaticTest st4 = new StaticTest(); System.out.println("st1.s == st2.s : "+(st1.s == st2.s)); System.out.println("st1.s == st3.s : "+(st1.s == st3.s)); System.out.println("st1.s == st4.s : "+(st1.s == st4.s)); } }

return:

st1.s == st2.s : true
st1.s == st3.s : true
st1.s == st4.s : true

Nine, write a program that shows automatic packaging functions for all basic types and package types are functional. 

public class HelloWorld {
    public static void main(String[] args) {
        int i1 = 127;
        Integer ii1 = i1;
        int i2 = ii1;
        int i3 = 128; Integer ii2 = i3; int i4 = ii2; } }

 X. Write a program that prints out three parameters obtained from the command line. To do this, make sure the command line in a String array index.

public class HelloWorld {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String s1 = s.next();
        String s2 = s.next();
        String s3 = s.next(); System.out.println(s1); System.out.println(s2); System.out.println(s3); } }

Input:

hello
world
!

return:

hello
world
!

 XI This example will AllTheColorsOfTheRainbow rewritten into a program.

public class AllTheColorsOfTheRainbow {
    int anIntegerRepresentingColors;
    void changeTheHueOfTheColor(int newValue){
        this.anIntegerRepresentingColors = newValue;
    }
}
public class HelloWorld {
    public static void main(String[] args) { AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow(); a.changeTheHueOfTheColor(2); System.out.println(a.anIntegerRepresentingColors); } }

Returns: 2

XII find HelloDate.java second version, which is simple comment that sample document. Javadoc perform the document, and then watch the results of running a Web browser.

//:HelloDate.java

import java.util.Date;

/**
 * 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
 *///:~

page:

 

 XIII, the document generated by the javadoc run Documentation1.java, Documentation2.java, Documentation3.java, and then through a Web browser to verify.

Bother to get, Pirates -

 Fourth, a document in the previous practice of adding the HTML list

slightly

 Fifth, use the program to practice 2, add comments to documents. This comment is extracted with javadoc documentation, and generate a HTML file, and then view the results through a Web browser.

//: HelloWorld.java
/**
 * HelloWorld class
 * @version 1.0
 * @see String
 * @author jojo
 */
public class HelloWorld {
    /**
     * HelloWorld method
     * @param args
     */
    public static void main(String[] args){
        System.out.println("Hello, World");
    }
}
//:~

page:

 

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

//: Overloading.java

/**
 * Tree class
 * @author jojo
 * @version 1.0
 */
class Tree {
    /**
     * height
     */
    int height;
    /**
     * Planting a seedling
     */
    Tree() {
        System.out.println("Planting a seedling");
    }
    /**
     * Creating new Tree that is 0 feet tall
     * @param i
     */
    Tree(int i) { System.out.println("Creating new Tree that is " + i + " feet tall"); height = i; } /** * Tree is 0 feet tall */ void info() { System.out.println("Tree is " + height + " feet tall"); } /** * s : Tree is 0 feet tall * @param s */ void info(String s) { System.out.println(s + ": Tree is " + height + " feet tall"); } } /** * Overloading */ 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("overloaded method"); } // Overloaded constructor: new Tree(); } } //:~

页面1:

Guess you like

Origin www.cnblogs.com/jojo-wang/p/11972920.html