Thinking in Java - Fourth Edition 章节练习个人解答——第5章

  注:本练习题答案系个人阅读后所作解答,非原书所配答案。(由于水平有限,文中出现错误还望谅解与指正)

 

文中所有源代码皆在Eclipse-jee-indigo-3.7,Java SE 6平台下测试运行。


 

练习1:

package lib.fifth;

public class Exercise_1 {
	String s;
	Exercise_1() {		
	}
	public static void main(String[] args) {
		Exercise_1 e = new Exercise_1();
		System.out.print(e.s);
	}
} /* Output:
null
*///:~


练习2:

package lib.fifth;

public class Exercise_2 {
	String s = "abc";
	Exercise_2() {
		System.out.println(s + " (No-arguments)");
	}
	Exercise_2(String s) {
		System.out.println(this.s + " (Arguments)");
		this.s = s;
	}
	public static void main(String[] args) {
		Exercise_2 e1 = new Exercise_2();
		Exercise_2 e2 = new Exercise_2("bcd");
		System.out.println(e1.s + " (No-arg created)");
		System.out.println(e2.s + " (Arguments created)");
	}
} /* Output:
abc (No-arguments)
abc (Arguments)
abc (No-arg created)
bcd (Arguments created)
*///:~


练习3:

package lib.fifth;

public class Exercise_3 {
	Exercise_3() {
		System.out.print("Hello!");
	}
	public static void main(String[] args) {
		new Exercise_3();
	}
} /* Output:
Hello!
*///:~


练习4:

package lib.fifth;

public class Exercise_4 {
	Exercise_4() {
		System.out.print("Hello!");
	}
	Exercise_4(String s) {
		System.out.print("Hello! " + s);
	}
	public static void main(String[] args) {
		new Exercise_4("Overloaded constructor");
	}
} /* Output:
Hello! Overloaded constructor
*///:~


练习5:

package lib.fifth;

public class Dog {
	void bark(int i) {
		System.out.println("barking!");
	}
	void bark(float f) {
		System.out.println("howling!");
	}
	public static void main(String[] args) {
		Dog dog = new Dog();
		dog.bark(1);
		dog.bark(1f);
	}
} /* Output:
barking!
howling!
*///:~


练习6:

package lib.fifth;

public class Dog2 {
	void bark(int i) {
		System.out.println("barking!");
	}
	void bark(float f) {
		System.out.println("howling!");
	}
	public static void main(String[] args) {
		Dog2 dog = new Dog2();
		dog.bark(1f);
		dog.bark(1);
	}
} /* Output:
howling!
barking!
*///:~


练习7:

package lib.fifth;

public class Exercise_7 {
	int a;
	public static void main(String[] args) {
		Exercise_7 e = new Exercise_7();
		System.out.print(e.a);
	}
} /* Output:
0
*///:~


练习8:

package lib.fifth;

public class Exercise_8 {
	void pick() {
		pit();
		this.pit();
	}
	void pit() {
		System.out.println("Hello!");
	}
	public static void main(String[] args) {
		Exercise_8 e = new Exercise_8();
		e.pick();
	}
} /* Output:
Hello!
Hello!
*///:~


练习9:

package lib.fifth;

public class Exercise_9 {
	Exercise_9() {
		System.out.print("abc");
	}
	Exercise_9(String s) {
		this();
	}
	public static void main(String[] args) {
		new Exercise_9("");
	}
} /* Output:
abc
*///:~


练习10:

package lib.fifth;

public class Exercise_10 {
	protected void finalize()	{
		System.out.print("Look out!");
	}
	public static void main(String[] args) {
		new Exercise_10();
	}
} /* (Execute to see output) *///:~

 

练习11:

package lib.fifth;

public class Exercise_11 {
	protected void finalize()	{
		System.out.print("Look out!");
	}
	public static void main(String[] args) {
		new Exercise_10();
		System.gc();
	}
} /* Output:
Look out!
*///:~


练习12:

package lib.fifth;

import java.util.Random;

public class Tank {
	int status;
	Tank() {		
	}
	Tank(int status) {
		this.status = status;
	}
	protected void finalize() {
		System.out.print(status + " ");
		System.out.println(status == 0);
	}
	public static void main(String[] args) {
		for(int i = 0; i < 100;) {
			Random rand = new Random();
			int j = rand.nextInt(2);
			if(j == 0)
				i++;
			new Tank(j);
		}
		System.gc();
	}
} /* (Execute to see output) *///:~


练习13:

package lib.fifth;

import static net.util.Print.print;

class Cup {
	Cup(int marker) {
		print("Cup(" + marker + ")");
	}
	void f(int marker) {
		print("f(" + marker + ")");
	}
}

class Cups {
	static Cup cup1;
	static Cup cup2;
	static {
		cup1 = new Cup(1);
		cup2 = new Cup(2);
	}
	Cups() {
		print("Cups()");
	}
}
public class Exercise_13 {
	public static void main(String[] args) {
		print("Inside main()");
		// Cup.cup1.f(99);  // (1)
	}
	static Cups cup1 = new Cups();  // (2)
	static Cups cup2 = new Cups();  // (2)
} /* Output:
Cup(1)
Cup(2)
Cups()
Cups()
Inside main()
*///:~


练习14:

package lib.fifth;

public class Exercise_14 {
	static String s1 = "abc";
	static String s2;
	static {
		s2 = "xyz";
	}
	static void print() {
		System.out.println(s1);
		System.out.println(s2);
	}
	public static void main(String[] args) {
		print();
	}
} /* Output:
abc
xyz
*///:~


练习15:

package lib.fifth;

public class Exercise_15 {
	String s;
	{
		s = "abc";
	}
	public static void main(String[] args) {
		Exercise_15 e = new Exercise_15();
		System.out.print(e.s);
	}
} /* Output:
abc
*///:~


练习16:

package lib.fifth;

public class Exercise_16 {
	public static void main(String[] args) {
		String[] s = new String[]{"fiddle", "de", "dum" };
		for(String str : s)
			System.out.print(str + " ");
	}
} /* Output:
fiddle de dum 
*///:~


练习17:

package lib.fifth;

public class Exercise_17 {
	Exercise_17(String s) {
		System.out.print(s);
	}
	public static void main(String[] args) {
		Exercise_17[] e;
	}
} ///:~


练习18:

package lib.fifth;

public class Exercise_18 {
	Exercise_18(String s) {
		System.out.println(s);
	}
	public static void main(String[] args) {
		Exercise_18[] e = new Exercise_18[]{
				new Exercise_18("fiddle"),
				new Exercise_18("de"),
				new Exercise_18("dum"),
		};
	}
} /* Output:
fiddle
de
dum
*///:~


练习19:

package lib.fifth;

import java.util.Arrays;

public class Exercise_19 {
	static void f(String...s) {
		System.out.println(Arrays.toString(s));
	}
	public static void main(String[] args) {
		f("fiddle", "de", "dum");
		f(new String[]{"fiddle", "de", "dum"});
	}
} /* Output:
[fiddle, de, dum]
[fiddle, de, dum]
*///:~


练习20:

package lib.fifth;

import java.util.Arrays;

public class Exercise_20 {
	public static void main(String...args) {
		System.out.print(Arrays.toString(args));
	}
} /* 
Input:
fiddle de dum
Output:
[fiddle, de, dum]
*///:~


练习21:

package lib.fifth;

public enum Value {
	$1, $2, $5, $10, $20, $50
} ///:~
package lib.fifth;

public class Exercise_21 {
	public static void main(String[] args) {
		for(Value v : Value.values())
			System.out.println(v + ", ordinal " + v.ordinal());
	}
} /* Output:
$1, ordinal 0
$2, ordinal 1
$5, ordinal 2
$10, ordinal 3
$20, ordinal 4
$50, ordinal 5
*///:~


练习22:

package lib.fifth;

public class Exercise_22 {
	Value v;
	Exercise_22(Value v) {
		this.v = v;
	}
	String describe() {
		switch(v) {
		case $1: return "$1: Front: the 1th US President, George Washington.";
		case $2: return "$2: Front: the 3th US President, Thomas Jefferson.";
		case $5: return "$5: Front: the 16th US President, Abraham Lincoln.";
		case $10: return "$10: Front: the 1th US Treasury Secretary, Alexander Hamilton.";
		case $20: return "$20: Front: the 7th US president, Andrew Jackson.";
		case $50: return "$50: Front: the 18th US president, Ulysses Simpson Grant.";
		default: return null;
		}
	}
	public static void main(String[] args) {
		System.out.println(new Exercise_22(Value.$1).describe());
		System.out.print(new Exercise_22(Value.$5).describe());
	}
} /* Output:
$1: Front: the 1th US President, George Washington.
$5: Front: the 16th US President, Abraham Lincoln.
*///:~


 

-END-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值