源代码地址:https://github.com/yangxian1229/ThinkingInJava
练习4:编写一个计算速度的程序,它所使用的距离和时间都是常量。
//: operators/E04_Velocity.java
// {Args: 30.5 3.2}
package operators;
public class E04_Velocity {
public static void main(String[] args) {
if(args.length < 2) {
System.err.println(
"Usage: java E04_Velocity distance time");
System.exit(1);
}
float distance = Float.parseFloat(args[0]);
float time = Float.parseFloat(args[1]);
System.out.print("Velocity = ");
System.out.print(distance / time);
// Change the next line if you want to use a different
// unit for 'distance'
System.out.println(" m/s");
}
} /* Output:
Velocity = 9.53125 m/s
*///:
Here we take the distance and time values from the command line. Arguments come in as a String array; if you need a float instead, use the static parseFloat( ) method of class Float.
练习5:创建一个名为Dog的类,它包含两个String域:name和says。创建两个对象,spot和scruffy。
练习6:在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象,测试用==和**equals()**方法来比较所有引用的结果。
//: ch3/E05_Dogs.java
package ch3;
import static net.mindview.util.Print.*;
class Dog{
String name,says;
void shuchu(){
System.out.println(name + " says:"+says);
}
}
public class E5_Dogs {
static void compare(Dog dog1, Dog dog2) {
print("== on top references: " + (dog1 == dog2));
print(
"equals() on top references: " + dog1.equals(dog2)
);
print("== on names: " + (dog1.name == dog2.name));
print(
"equals() on names: " + dog1.name.equals(dog2.name)
);
print("== on says: " + (dog1.says == dog2.says));
print(
"equals() on says: " + dog1.says.equals(dog2.says)
);
}
public static void main(String[] args) {
Dog aDog = new Dog();
aDog.name = "spot";
aDog.says = "Ruff";
Dog bDog = new Dog();
bDog.name = "scruffy";
bDog.says = "Wurf";
aDog.shuchu();
bDog.shuchu();
//compare the sopt's object and scruffy's object
print("\n compare aDog and bDog:");
compare(aDog,bDog);
//a new Dog reference to spot's object.
Dog newDog = aDog;
print("\n compare aDog and newDog:");
compare(aDog, newDog);
print("\n compare bDog and newDog:");
compare(bDog, newDog);
}
}/* Output:
spot says:Ruff
scruffy says:Wurf
compare aDog and bDog:
== on top references: false
equals() on top references: false
== on names: false
equals() on names: false
== on says: false
equals() on says: false
compare aDog and newDog:
== on top references: true
equals() on top references: true
== on names: true
equals() on names: true
== on says: true
equals() on says: true
compare bDog and newDog:
== on top references: false
equals() on top references: false
== on names: false
equals() on names: false
== on says: false
equals() on says: false
*///:~
练习7:编写一个程序,模拟扔硬币的结果。
package ch3;
import java.util.Random;
public class E07_CoinFlipping {
public static void main(String[] args) {
Random rand = new Random();
boolean flip = rand.nextBoolean();
System.out.print("Outcome:");
System.out.println(flip ? "HEAD":"TAIL");
}
}
练习8:展示用十六进制和八进制记数法操作long值,用**Long.toBinaryString()**来显示结果。
package ch3;
import static net.mindview.util.Print.*;
public class E08_LongLiterals {
public static void main(String[] args) {
long l1 = 0x22al;
print("l1:"+Long.toBinaryString(l1));
long l2 = 0X22AL;
print("l2:"+Long.toBinaryString(l2));
long l3 = 077l;
print("l3:"+Long.toBinaryString(l3));
}
}/* Output:
l1:1000101010
l2:1000101010
l3:111111
*/
练习9:分别显示用float和double指数记数法所能表示的最大和最小的数字。
package ch3;
import static net.mindview.util.Print.*;
public class E09_MinMaxExponets {
public static void main(String[] args) {
print("Min Float:"+Float.MIN_VALUE);
print("Max Float:"+Float.MAX_VALUE);
print("Min Double:"+Double.MIN_VALUE);
print("Max Double:"+Double.MAX_VALUE);
}
}/* Output:
Min Float:1.4E-45
Max Float:3.4028235E38
Min Double:4.9E-324
Max Double:1.7976931348623157E308
*///:~
练习10:编写一个具有两个常量值的程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,但是其最低有效位为1(提示:使用十六进制常量来表示是最简单的方式)。取这两个值,用按位操作符以所有可能的方式结合运算它们,然后用**Integer.toBinaryString()**显示。
package ch3;
import static net.mindview.util.Print.*;
public class E10 {
public static void main(String[] args) {
int a = 0xaaaaaaaa;
int b = 0x55555555;
print(" a :"+Integer.toBinaryString(a));
print(" b :"+Integer.toBinaryString(b));
print("~a :"+Integer.toBinaryString(~a));
print("~b :"+Integer.toBinaryString(~b));
print("a&a:"+Integer.toBinaryString(a&a));
print("a|a:"+Integer.toBinaryString(a|a));
print("a^a:"+Integer.toBinaryString(a^a));
print("a&b:"+Integer.toBinaryString(a&b));
print("a|b:"+Integer.toBinaryString(a|b));
print("a^b:"+Integer.toBinaryString(a^b));
}
}/* Output:
a :10101010101010101010101010101010
b :1010101010101010101010101010101
~a :1010101010101010101010101010101
~b :10101010101010101010101010101010
a&a:10101010101010101010101010101010
a|a:10101010101010101010101010101010
a^a:0
a&b:0
a|b:11111111111111111111111111111111
a^b:11111111111111111111111111111111
*///:~
练习11:以一个最高有效位为1的二进制数字开始(提示:使用十六进制常量),用有符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,每移一位都要使用Integer.toBinaryString()显示结果。
练习12:以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有的二进制都被移出为止,每移一位都要使 用Integer.toBinaryString()显示结果。
练习13:编写一个方法,它以二进制形式显示char类型的值。使用多个不同的字符来展示它。
package ch3;
public class ch11 {
static void charToBinary(char c){
System.out.println(Integer.toBinaryString((int)c));
}
public static void main(String[] args) {
//11
int a = 0x80;
while(a != 0){
System.out.println(Integer.toBinaryString(a));
a >>= 1;
}
//12
int b = 0xff;
System.out.println(Integer.toBinaryString(b));
b <<= 1;
while(b != 0){
System.out.println(Integer.toBinaryString(b));
b >>>=1;
}
//13
charToBinary('a');
charToBinary('b');
charToBinary('c');
}
}
练习14:编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结果打印出来。做**==和!=比较的同时,用equals()做测试。在main()**里面用几个不同的字符串对象调用这个方法。
package ch3;
import static net.mindview.util.Print.*;
public class E14 {
public static void p(String s, boolean b) {
System.out.println(s + ": " + b);
}
static void compare(String l,String r ){
print("l:"+l+" r:"+r);
p("l == r" , l==r);
p("l != r" , l!=r);
p("l.equals(r)" , l.equals(r));
}
public static void main(String[] args) {
compare("Hello","Hello");
String s = new String("Hello");
compare("Hello", s);
compare("Hello", "Goodbye");
}
}/* Output:
l:Hello r:Hello
l == r: true
l != r: false
l.equals(r): true
l:Hello r:Hello
l == r: false
l != r: true
l.equals(r): true
l:Hello r:Goodbye
l == r: false
l != r: true
l.equals(r): false
*///:~
The only comparisons that actually compile are = = and !=. This (slightly tricky) exercise highlights the critical difference between the = = and != operators,which compare references, and equals( ), which actually compares content.
请记住,引用的字符数组也会生成对String对象的引用。 在第一种情况下,编译器识别出两个字符串实际上包含相同的值。 因为String对象是不可变的(你不能改变它们的内容),所以编译器可以将两个String对象合并为一个,所以在这种情况下= =返回true。
但是,当您创建单独的String时,您还会创建具有相同内容的不同对象,因此 = =返回false。 比较对象的唯一可靠方法是使用equals()。 警惕任何使用==的比较,它总是只比较两个引用以查看它们是否相同(即它们指向同一个对象)。