標籤:mystra java java編程思想 第2章 練習
Thinking in Java1
//: 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 runsclass 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
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Java - Thinking in Java 第2章練習