Java-Thinking in Java chapter 2nd exercises
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/*** Number of stored bytes * Created by wangchenlong on 15/7/6. */public class StorageTest {/*** number of bytes of the String * @ param s String * @ return number of bytes */public static int storage (String s) {return s. length () * 2;} public static void main (String [] args) {String s = Caroline; System. out. println (s + bytes occupied: + storage (s);}/*** Output: * Caroline bytes occupied: 16 *///:~}
7
//: IncrementableTest. java/*** static variable test * 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 (the number is changed to + StaticTest. i) ;}}/*** output: * The number is 48 *///:~
8
//: OneStaticTest. java/*** static variable single instance test * 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 inc Rement: + 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/*** basic type and package type * 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); Sys Tem. 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 (floa T 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/*** command line parameter * 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 (less than 3 parameters); return;} String result =; for (String s: args) {result + = s +;} System. out. println (parameter: + result) ;}}/*** Output: * parameter: caroline wendy girl *///:~
11
//: AllTheColorsOfTheRainbow/*** encoding style test * 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 Other {int response; int theHueOfTheColor; void response (int newHue) {theHueOfTheColor = newHue;} int response (int theHueOfTheColor) {return response = theHueOfTheColor;}/*** Output: * theHueOfTheColor = 16 * anIntegerRepresentingColors = 32 *///:~
12 & 13 & 14 & 15