Java编程思想 练习题(一)

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

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));
}

返回值:

0

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

书中HelloDate源码:

import java.util.*;

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

修改后HelloWorld源码:

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

返回值:Hello, World

三、找出含有ATypeName的代码段,将其改为完整的程序。

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()); } }

返回:ATypeName{id='1', name='张三'}

四、将DataOnly代码段改写成一个程序。略。

五、修改前一个联系,将DataOnly中的数据改写成mian()方法中赋值并打印出来。参照(三)

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

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

返回:20

七、将Incrementtable的代码改写成一个完整的可运行程序。 

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); } }

返回:

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

八、编写一个程序,展示无论你创建了某个特定类的多少个对象,这个类中的某个特定的static域只有一个对象。

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)); } }

返回:

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

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

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; } }

 十、编写一个程序,打印出从命令行获取的三个参数。为此,要确定命令行数组中String的下标。

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); } }

输入:

hello
world
!

返回:

hello
world
!

 十一、将AllTheColorsOfTheRainbow这个示例改写成一个程序。

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); } }

返回:2

十二、找出HelloDate.java的第二版本,也就是那个简单注释文档的示例。对该文档执行javadoc,然后通过Web浏览器观看运行结果。

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

页面:

 十三、通过javadoc运行Documentation1.java、Documentation2.java、Documentation3.java,然后通过Web浏览器验证所产生的文档。

懒得弄了,盗图~

 十四、在前一个练习的文档中加入各项的HTML列表

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

//: 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");
    }
}
//:~

页面:

十六、找到第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:

猜你喜欢

转载自www.cnblogs.com/jojo-wang/p/11972920.html