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

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

 

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


static net.util.Print.*:

package net.util;

public class Print {
	public static void print() {
		System.out.println();
	}
	public static void print(Object o) {
		System.out.println(o);
	}	
}


练习1:

package lib.third;

import java.util.Date;
import static net.util.Print.*;

public class Exercise_1 {
	public static void main(String[] args) {
		System.out.println("Hello, it's: ");
		System.out.println(new Date());
		print("Hello, it's: ");
		print(new Date());
	}
} /* Output: (55% match)
Hello, it's: 
Mon Jul 18 12:03:34 CST 2011
Hello, it's: 
Mon Jul 18 12:03:34 CST 2011
*///:~

 

练习2:

package lib.third;

import static net.util.Print.*;

public class Exercise_2 {
	float level;
	public static void main(String[] args) {
		Exercise_2 e1 = new Exercise_2();
		Exercise_2 e2 = new Exercise_2();
		e1.level = 99.0f;
		e2.level = 360.0f;
		print("1: e1.level: " + e1.level +
				", e2.level: " + e2.level);
		e1.level = e2.level;
		print("2: e1.level: " + e1.level +
				", e2.level: " + e2.level);
		e1.level = 110;
		print("3: e1.level: " + e1.level +
				", e2.level: " + e2.level);
		e1 = e2;
		print("4: e1.level: " + e1.level +
				", e2.level: " + e2.level);
		e1.level = 99;
		print("5: e1.level: " + e1.level +
				", e2.level: " + e2.level);
	}
} /* Output:
1: e1.level: 99.0, e2.level: 360.0
2: e1.level: 360.0, e2.level: 360.0
3: e1.level: 110.0, e2.level: 360.0
4: e1.level: 360.0, e2.level: 360.0
5: e1.level: 99.0, e2.level: 99.0
*///:~


练习3:

package lib.third;

import static net.util.Print.*;

class Latter {
	float f;
}

public class Exercise_3 {
	static void c(Latter y) {
		y.f = 1.0f;
	}
	public static void main(String[] args) {
		Latter x = new Latter();
		x.f = 2.0f;
		print("1: x.f: " + x.f);
		c(x);
		print("2: x.f: " + x.f);
	}
} /* Output:
1: x.f: 2.0
2: x.f: 1.0
*///:~


练习4:

package lib.third;

import java.util.Random;
import static net.util.Print.*;

public class Exercise_4 {	
	float distance;
	float time;
	
	float velocity() {
		return distance / time;
	}
	public static void main(String[] args) {
		Exercise_4 e = new Exercise_4();
		if(args.length == 0) {
			Random rand = new Random(47);
			e.distance = rand.nextFloat();
			e.time = rand.nextFloat();
			print("distance: " + e.distance);
			print("time: " + e.time);
			print("velocity: " + e.velocity());
		}
		else {
			e.distance = Float.parseFloat(args[0]);
			e.time = Float.parseFloat(args[1]);			
			print("distance: " + e.distance);
			print("time: " + e.time);
			print("velocity: " + e.velocity());
		}
	}
} /* Output: (Sample)
distance: 0.72711575
time: 0.39982635
velocity: 1.8185788
*///:~


练习5:

package lib.third;

import static net.util.Print.*;

public class Dog {
	String name;
	String says;
	
	void newDog(String name, String says) {
		this.name = name;
		this.says = says;
	}
	public static void main(String[] args) {
		Dog d1 = new Dog();
		Dog d2 = new Dog();
		d1.newDog("spot", "Ruff!");
		d2.newDog("scruffy", "Wurf!");
		print(d1.name + " says: \"" + d1.says + "\"");
		print(d2.name + " says: \"" + d2.says + "\"");
	}
}/* Output:
spot says: "Ruff!"
scruffy says: "Wurf!"
*///:~


练习6:

package lib.third;

import static net.util.Print.*;

public class Dog2 {
	public static void main(String[] args) {
		Dog2 spot = new Dog2();
		Dog2 scruffy = new Dog2();
		Dog2 wolffy = new Dog2();
		wolffy = spot;
		print("spot == scruffy: " + (spot == scruffy));
		print("spot == wolffy: " + (spot == wolffy));
		print("scruffy == wolffy: " + (scruffy == wolffy));
		print("spot.equals(scruffy): " + spot.equals(scruffy));
		print("spot.equals(wolffy): " + spot.equals(wolffy));
		print("scruffy.equals(wolffy): " + scruffy.equals(wolffy));
	}
} /* Output:
spot == scruffy: false
spot == wolffy: true
scruffy == wolffy: false
spot.equals(scruffy): false
spot.equals(wolffy): true
scruffy.equals(wolffy): false
*///:~


练习7:

package lib.third;

import java.util.Random;
import static net.util.Print.*;

public class Exercise_7 {
	int x;
	int y;
	public static void main(String[] args) {
		Exercise_7 e = new Exercise_7();
		Random rand = new Random();
		e.x = rand.nextInt(2);
		e.y = rand.nextInt(2);
		print((e.x == e.y) ? "Positive!" : "Negative!");
	}
} /* OutPut: (Sample)
Positive!
*///:~


练习8:

package lib.third;

import static net.util.Print.*;

public class Exercise_8 {
	long hex = 0x2f;
	long octal = 0177;
	public static void main(String[] args) {
		Exercise_8 e = new Exercise_8();
		print("Binary 0x2f is " + Long.toBinaryString(e.hex));
		print("Binary 0177 is " + Long.toBinaryString(e.octal));
	}
} /* Output:
Binary 0x2f is 101111
Binary 0177 is 1111111
*///:~


练习9:

package lib.third;

import static net.util.Print.*;

public class Exercise_9 {
	public static void main(String[] args) {
		print("float MAX: " + Float.MAX_VALUE + ", MIN: " + Float.MIN_VALUE);
		print("double MAX: " + Double.MAX_VALUE + ", MIN: " + Double.MIN_VALUE);
	}
} /* Output:
float MAX: 3.4028235E38, MIN: 1.4E-45
double MAX: 1.7976931348623157E308, MIN: 4.9E-324
*///:~


练习10:

package lib.third;

import static net.util.Print.*;

public class Exercise_10 {
	int i = 0xA; //0xA = 1010;
	int j = 0x5; //0x5 = 0101;
	public static void main(String[] args) {
		Exercise_10 e = new Exercise_10();
		print("i & j is " + Integer.toBinaryString(e.i & e.j));
		print("i | j is " + Integer.toBinaryString(e.i | e.j));
		print("i ^ j is " + Integer.toBinaryString(e.i ^ e.j));
	}
} /* Output:
i & j: 0
i | j: 1111
i ^ j: 1111
*///:~


练习11:

package lib.third;

import static net.util.Print.*;

public class Exercise_11 {
	public static void main(String[] args) {
		int a = 0x81234567;
		for(int i = 0; i <= 32; i++)
			print(Integer.toBinaryString(a >>= 1));
	}
} /* Output:
11000000100100011010001010110011
11100000010010001101000101011001
...
11111111111111111111111111111110
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
*///:~


练习12:

package lib.third;

import static net.util.Print.print;

public class Exercise_12 {
	public static void main(String[] args) {
		int a = 0xffffffff;
		print(Integer.toBinaryString(a <<= 1));
		for(int i = 0; i < 32; i++)
			print(Integer.toBinaryString(a >>>= 1));
	}
} /* Output:
11111111111111111111111111111110
1111111111111111111111111111111
...
11
1
0
*///:~


练习13:

package lib.third;

import static net.util.Print.print;

public class Exercise_13 {
	static void showChar(char c) {
		print(Integer.toBinaryString(c));
	}
	public static void main(String[] args) {
		showChar('a');
		showChar('A');
		showChar('z');
	}
} /* Output:
1100001
1000001
1111010
*///:~


练习14:

package lib.third;

import static net.util.Print.*;

public class Exercise_14 {
	static void compare(String s1, String s2) {
		print(s1 + " == " + s2 + " is " + (s1 == s2));
		print(s1 + " != " + s2 + " is " + (s1 != s2));
		print(s1 + ".equals(" + s2 + ") is " + s1.equals(s2));
	}
	public static void main(String[] args) {
		compare("abc", "abc");
		compare("abc", "abcd");
		compare("π", "PI");
	}
} /* Output:
abc == abc is true
abc != abc is false
abc.equals(abc) is true
abc == abcd is false
abc != abcd is true
abc.equals(abcd) is false
π == PI is false
π != PI is true
π.equals(PI) is false
*///:~


-END-

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值