Java - Thinking in Java 第2章 练习

标签: MystraJavaJava编程思想第2章练习
1266人阅读 评论(0) 收藏 举报
分类:

Thinking in Java

1

//: object/HelloDate.java
/**
 * Default initialize class.
 * @author C.L.Wang
 * @author Caroline Wendy
 * @version 1.0
 */
public class Main extends ShowProperties {
    /**
     * Entrv point to class & application.
     * @param args array of string arguments
     * @throws java.lang.Exception No exceptions thrown
     */
    public static void main(String[] args) {
        DefaultInit di = new DefaultInit();
        System.out.println("default int = " + di.i + ", default char = " + di.c);
    }
    /**
     * Output:
     * default int = 0, default char =
     *///:~
}

2

//: object/HelloDate.java
/**
 * Say Hello World
 * @author C.L.Wang
 * @author Caroline Wendy
 * @version 1.0
 */
public class Main extends ShowProperties {
    /**
     * Entrv point to class & application.
     * @param args array of string arguments
     * @throws java.lang.Exception No exceptions thrown
     */
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
    /**
     * Output:
     * Hello, World
     *///:~
}

3

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.7;
      a.b = false;
      a.show();
   }
}

4&5

//: object/DataOnlyTest.java
// TIJ4 Chapter Object Exercise 4 page 90
// Turn the DataOnly code fragments into a program that compiles and runs

class DataOnly {
  int i;
  double d;
  boolean b;
  void show() {
      System.out.println(i);
      System.out.println(d);
      System.out.println(b);
  }
}

public class DataOnlyTest {
  public static void main(String[] args) {
      DataOnly data = new DataOnly();
      data.i = 47;
      data.d = 1.1;
      data.b = false;
      data.show();
  }
}
/**
* Output:
* 47
* 1.1
* false
*///:~

6

//: StorageTest.java
/**
 * 存储字节数
 * Created by wangchenlong on 15/7/6.
 */
public class StorageTest {
    /**
     * 字符串的字节数
     * @param s 字符串
     * @return 字节数
     */
    public static int storage(String s) {
        return s.length()*2;
    }

    public static void main(String[] args) {
        String s = "Caroline";
        System.out.println(s + "字节数占用: " + storage(s));
    }
    /**
     * Output:
     * Caroline字节数占用: 16
     *///:~
}

7

//: IncrementableTest.java
/**
 * 静态变量测试
 * Created by wangchenlong on 15/7/6.
 */
class StaticTest {
    static int i = 47;
}

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

public class IncrementableTest {
    public static void main(String[] args) {
        Incrementable.increment();
        System.out.println("数字变为" + StaticTest.i);
    }
}
/**
 * output:
 * 数字变为48
 *///:~

8

//: OneStaticTest.java
/**
 * static变量单实例测试
 * Created by C.L.Wang on 15/7/6.
 */
public class OneStaticTest {
    public static void main(String[] args) {
        StaticTest s1 = new StaticTest();
        StaticTest s2 = new StaticTest();

        //noinspection AccessStaticViaInstance
        System.out.println("s1.i = " + s1.i + ", s2.i = " + s2.i);
        Incrementable.increment();

        //noinspection AccessStaticViaInstance
        System.out.println("after increment: " + "s1.i = " + s1.i + ", s2.i = " + s2.i);
    }
}
/**
 * output:
 * s1.i = 47, s2.i = 47
 * after increment: s1.i = 48, s2.i = 48
 *///:~

9

//: AutoBoxTest.java
/**
 * 基本类型和包装器类型
 * Created by C.L.Wang on 15/7/6.
 */
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);
    }
}
/**
 * Output:
 * ...
 *///:~

10

//: CommentLineTest.java
/**
 * 命令行参数
 * Created by C.L.Wang on 15/7/6.
 */
public class CommandLineTest {
    public static void main(String[] args) {
        if (args.length != 3) {
            System.out.println("参数不足3个");
            return;
        }

        String result = "";
        for (String s : args) {
            result += s + " ";
        }
        System.out.println("参数: " + result);
    }
}
/**
 * Output:
 * 参数: caroline wendy girl
 *///:~

11

//:AllTheColorsOfTheRainbow
/**
 * 编码风格测试
 * Created by C.L.Wang on 15/7/6.
 */
public class RainbowTest {
    public static void main(String[] args) {
        AllTheColorOfTheRainbow atc = new AllTheColorOfTheRainbow();
        atc.changeTheHusOfTheColor(16);
        System.out.println("theHueOfTheColor = " + atc.theHueOfTheColor);
        System.out.println("anIntegerRepresentingColors = " + atc.changeAnIntegerRepresentingColors(32));
    }

}

class AllTheColorOfTheRainbow {
    int anIntegerRepresentingColors;
    int theHueOfTheColor;
    void changeTheHusOfTheColor(int newHue) {
        theHueOfTheColor = newHue;
    }
    int changeAnIntegerRepresentingColors(int theHueOfTheColor) {
        return anIntegerRepresentingColors = theHueOfTheColor;
    }
}
/**
 * Output:
 * theHueOfTheColor = 16
 * anIntegerRepresentingColors = 32
 *///:~

12&13&14&15

参考: http://blog.csdn.net/caroline_wendy/article/details/46779719


娱乐

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2473648次
    • 积分:32372
    • 等级:
    • 排名:第152名
    • 原创:709篇
    • 转载:1篇
    • 译文:0篇
    • 评论:385条
    自我简介

    本人代码娴熟,涉猎广泛。

    目前任职于春雨医生的大数据部,数据挖掘工程师。

    曾经任职于春雨医生的移动端部,高级Android开发工程师。

    欢迎Follow我的GitHub:.

    https://github.com/SpikeKing

    文章分类
    最新评论