练习1:(1)使用“简短的”和正常的打印语句来编写一个程序。
答案:这里使用“简短的”打印语句需要导thinking in Java作者个人写的包。笔者没有找到资源,就忽略这一步。
package thinkinginjava.chapterthree;public class ChapterThreeExerciseOne {public static void main(String[] args) {System.out.println("This is a normal statement:");System.out.println("Hello World");System.out.print("This is a short statement: ");System.out.print("Hello again! ");System.out.print("Still On the same line.");System.out.print("\nUse in short sentence\nManual line。wrap\n");}}
练习2: (1) 创建一个包含float域的类,并用这个类来展示别名机制。
package thinkinginjava.chapterthree;class FloatAlias{float value;
}
public class ChapterThreeExerciseTwo {public static void main(String[] args) {FloatAlias objOne = new FloatAlias();FloatAlias objTwo = new FloatAlias();objOne = objTwo;objOne.value = 3.14f;System.out.println("1: objOne.value:" + objOne.value+" objTwo.value:" + objTwo.value);objTwo.value = 1.618f;System.out.println("After modifying objTwo.value:");System.out.println("2: objOne.value:" + objOne.value+" objTwo.value:" + objTwo.value);}}
练习3: (1) 创建一个包含一个float域的类,并用这个类来展示方法调用时的别名机制。
package thinkinginjava.chapterthree;class FloatHoler{float value;
}
public class ChapterThreeExerciseThree {static void modifyValue(FloatHoler floatHoler){floatHoler.value = 9.81f;}public static void main(String[] args) {FloatHoler floatHoler = new FloatHoler();floatHoler.value = 3.14f;System.out.println("Before modify, floatHoler.value="+floatHoler.value);modifyValue(floatHoler);System.out.println("After modify, floatHoler.value="+floatHoler.value);}}
练习4: (2) 编写一个计算速度的程序,它所使用的距离和时间都是常量。
package thinkinginjava.chapterthree;import java.util.Random;public class ChapterThreeExerciseFour {public static void main(String[] args) {Random rand = new Random(47);double distance = rand.nextDouble() + 1;double time = rand.nextInt(10) + 1;double velocity = distance / time;System.out.println("distance = " + String.format("%.2f", distance));System.out.println("time = " + String.format("%.2f", time));System.out.println("velocity = " + String.format("%.2f", velocity));}}
练习5: (2) 创建一个名为Dog的类,它包含两个String域:name和says。在main()方法中,创建两个Dog对象,一个名为spot(它的叫声为"Ruff!"),另一个名为scruffy(它的叫声为“Wurf!")。然后显示它们的名字和叫声。
答案:
package thinkinginjava.chapterthree;class Dog{String name;String says;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSays() {return says;}public void setSays(String says) {this.says = says;}
}
public class ChapterThreeExerciseFive {public static void main(String[] args) {Dog dogOne = new Dog();dogOne.setName("spot");dogOne.setSays("Ruff!");Dog dogTwo = new Dog();dogTwo.setName("scruffy");dogTwo.setSays("Wurf!");System.out.println(dogOne.getName() + " " + dogOne.getSays());System.out.println(dogTwo.getName() + " " + dogTwo.getSays());}
}
练习6:(3)在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象。测试用==和equals()方法来比较所有引用的结果。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseSix {public static void main(String[] args) {Dog dogOne = new Dog();dogOne.setName("spot");dogOne.setSays("Ruff!");Dog dogTwo = new Dog();dogTwo.setName("scruffy");dogTwo.setSays("Wurf!");Dog dogThree = dogOne;System.out.println("=========="+dogOne.getName()+" compare with "+dogTwo.getName()+" ===========");compare(dogOne, dogTwo);System.out.println("=========="+dogOne.getName()+" compare with "+dogThree.getName()+" ===========");compare(dogOne, dogThree);System.out.println("=========="+dogTwo.getName()+" compare with "+dogThree.getName()+" ===========");compare(dogTwo, dogThree);}public static void compare(Dog dogOne, Dog dogTwo) {System.out.println("with==:"+(dogOne == dogTwo));System.out.println("with equals():"+(dogOne.equals(dogTwo)));System.out.println("==on name:"+(dogOne.getName()==dogTwo.getName()));System.out.println("==on says:"+(dogOne.getSays()==dogTwo.getSays()));System.out.println("equals() on name:"+(dogOne.getName().equals(dogTwo.getName())));System.out.println("equals() on says:"+(dogOne.getSays()==dogTwo.getSays()));}}
练习7:(3)编写一个程序,模拟扔硬币的结果。
答案:
package thinkinginjava.chapterthree;import java.util.Random;public class ChapterThreeExerciseSeven {public static void main(String[] args) {for (int i = 0; i < 10000; i++) {Random rand = new Random();String result = rand.nextBoolean()? "HEAD" : "TAIL";System.out.println(result);}}}
练习8: (2)展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseEight {public static void main(String[] args) {long hexValue = 0x1A3F; //decimal 6719long octalValue = 0757; //decimal 495System.out.println("Hexdecimal value: 0x"+Long.toHexString(hexValue));System.out.println("Binary representation :"+Long.toBinaryString(hexValue));System.out.println("Octaldecimal value: 0"+Long.toOctalString(octalValue));System.out.println("Binary representation :"+Long.toBinaryString(octalValue));}}
练习9:(1) 分别显示用float和double指数计数法所能表示的最大和最小的数字。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseNine {public static void main(String[] args) {System.out.println("Float Maximum Value: "+Float.MAX_VALUE);System.out.println("Float Minimum Value: "+Float.MIN_VALUE);System.out.println("Double Maximum Value: "+Double.MAX_VALUE);System.out.println("Double Minimum Value: "+Double.MIN_VALUE);System.out.println("\nFloat Maximum Value in secientific nontion:" + String.format("%e", Float.MAX_VALUE));System.out.println("Float Minimum Value in secientific nontion:" + String.format("%e", Float.MIN_VALUE));System.out.println("Double Maximum Value in secientific nontion:" + String.format("%e", Double.MAX_VALUE));System.out.println("Double Minimum Value in secientific nontion:" + String.format("%e", Double.MIN_VALUE));}}
练习10:(3) 编写一个具有两个常量值的程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,但是其最低有效位为1(提示:使用十六进制常量来表示是最简单的方法)。取这两个值,按位操作符以所有可能的方式结合运算它们,然后用Integer.toBinaryString()显示。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseTen {public static void main(String[] args) {int valueOne = 0x55555555; // binary 01010101010101010101010101010101int valueTwo = 0xAAAAAAAA; // binary 10101010101010101010101010101010System.out.println("valueOne in binary: " + Integer.toBinaryString(valueOne));System.out.println("valueTwo in binary: " + Integer.toBinaryString(valueTwo));int andResult = valueOne & valueTwo;System.out.println("andResult in binary: " + Integer.toBinaryString(andResult));int orResult = valueOne | valueTwo;System.out.println("orResult in binary: " + Integer.toBinaryString(orResult));int xorResult = valueOne ^ valueTwo;System.out.println("xorResult in binary: " + Integer.toBinaryString(xorResult));int notResultOne = ~valueOne;int notResultTwo = ~valueTwo;System.out.println("notResultOne in binary: " + Integer.toBinaryString(notResultOne));System.out.println("notResultTwo in binary: " + Integer.toBinaryString(notResultTwo));System.out.println("andResult in hexdecimal: 0x" + Integer.toHexString(andResult));System.out.println("orResult in hexdecimal: 0x" + Integer.toHexString(orResult));System.out.println("xorResult in hexdecimal: 0x" + Integer.toHexString(xorResult));System.out.println("notResultOne in hexdecimal: 0x " + Integer.toHexString(notResultOne));System.out.println("notResultTwo in hexdecimal: 0x" + Integer.toHexString(notResultTwo));}}
练习11:(3) 以一个最高有效位为1的二进制数字开始(提示:使用十六进制常量),用有符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都要使用Integer.toBinaryString()显示结果。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseEleven {public static void main(String[] args) {Integer i = Integer.MAX_VALUE;System.out.println(Integer.toBinaryString(i));System.out.println(Integer.toHexString(i));while(i != 0){System.out.println(Integer.toBinaryString(i >>= 1));}}}
练习12:(2) 以一个所有位都为1的二进制的数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都要使用Integer.toBinaryStirng()显示结果。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseTwelve {public static void main(String[] args) {Integer i = Integer.MAX_VALUE;System.out.println(Integer.toBinaryString(i <<= 1));while(i != 0){System.out.println(Integer.toBinaryString(i >>>= 1));}}}
练习13:(1) 编写一个方法,它以二进制形式显示char类型的值。使用多个不同的字符来展示它。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseThirteen {public static void main(String[] args) {printCharInBinary('A');printCharInBinary('B');printCharInBinary('1');}public static void printCharInBinary(char c){String binaryString = Integer.toBinaryString(c);System.out.println("Character: " + binaryString + "-> Binary: "+ String.format("%16s", binaryString).replace(' ','0'));}}
练习14:(3) 编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结果打印出来。做==和!=比较的同时,用equals()作测试。在main()里面用几个不同的字符串对象调用这个方法。
答案:
package thinkinginjava.chapterthree;public class ChapterThreeExerciseFourteen {public static void main(String[] args) {String strOne = "hello";String strTwo = "hello";String strThree = new String("hello");String strFour = "world";String strFive = "Hello";compareString(strOne, strTwo);compareString(strOne, strThree);compareString(strOne, strFour);compareString(strOne, strFive);}public static void compareString(String strOne, String strTwo) {System.out.println(strOne + "&" + strTwo);System.out.println("strOne == strTwo : " + (strOne == strTwo));System.out.println("strOne != strTwo : " + (strOne != strTwo));System.out.println("strOne.equals(strTwo) : " + (strOne.equals(strTwo)));}}