Thinking in Java(第四版)习题--第三章

来源:互联网 发布:手机淘宝旺旺名在哪里 编辑:程序博客网 时间:2024/05/16 10:35

练习1

/*使用“简短的”和正常的打印语句来编写一个程序。*/import java.util.*;import  static net.mindview.util.Print.*;public class Main {    public static void main(String[] args) {        print("Hello World !");        System.out.println("Hello world !");    }}/*Output:Hello World !Hello world !*/
导入包时花了不少劲,util前面两个文件夹好像缺少文件编译不了,于是直接删了,然后编译才通过。

练习2

/*创建一个包含一个float域的类,并用这个类来展示别名机制*/class Integral{    float f;}class Main{    public static void main(String args[]){        Integral f1 = new Integral();        Integral f2 = new Integral();        f1.f = 2f;        f2.f = 1f;        System.out.println("f1.f="+f1.f);        System.out.println("f2.f="+f2.f);        System.out.println();        f1.f=f2.f;        System.out.println("f1.f="+f1.f);        System.out.println("f2.f="+f2.f);        System.out.println();        f2.f=3f;        System.out.println("f1.f="+f1.f);        System.out.println("f2.f="+f2.f);        System.out.println();        f1=f2;        System.out.println("f1.f="+f1.f);        System.out.println("f2.f="+f2.f);    }}/*Output:f1.f=2.0f2.f=1.0f1.f=1.0f2.f=1.0f1.f=1.0f2.f=3.0f1.f=3.0f2.f=3.0*/
这个程序应该可以说明别名机制了。

练习3

/*创建一个包含一个float域的类,并用这个类来展示方法调用是的别名机制* */class Letter{    float f;}public class Main{    static void f(Letter f){        f.f=1f;    }    public static void main (String args[]){        Letter x = new Letter();        x.f = 2f;        System.out.println("x.f="+x.f);        f(x);        System.out.println("x.f="+x.f);    }}/*Output:x.f=2.0x.f=1.0*/

练习4

/*编写一个计算速度的程序,他所使用的距离和时间都是常量。*/

练习5

/*创建一个名为Dog的类,它包含两个String域,name和says。在main()方法中,创建两个Dog对象,* 一个名为spot(它的叫声为“Ruff!”),另一个名为scruffy(它的叫声为“Wuff!”)。然后输出它们的名字和叫声*/class Dog{    String name;    String says;}public class Main{    public static void main (String [] args){        Dog dog1 = new Dog();        Dog dog2 = new Dog();        dog1.name = "spot";        dog2.name = "scruffy";        dog1.says = "Ruff!";        dog2.says = "Wurf!";        System.out.println(dog1.name + "says:" + dog1.says);        System.out.println(dog2.name + "says:" + dog2.says);    }}/*Output;spotsays:Ruff!scruffysays:Wurf!*/

练习6

/*在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象。测试用==和equals()方法来比较所有引用的结果。*/
个人理解:==用来比较两对象在对内存中的地址,equals()用来比较两个对象的内容是否相同。
/*在练习5的基础上,创建一个新的Dog索引,并对其赋值为spot对象。测试用==和equals()方法来比较所有引用的结果。*/import static net.mindview.util.Print.*;class Dog {    String name;    String says;}class Main {    public static void compare(Dog dog1,Dog dog2){        print("with==:"+(dog1==dog2));        print("with equals():"+dog1.equals(dog2));        print("==on name:"+(dog1.name==dog2.name));        print("==on says:"+(dog1.says==dog2.says));        print("equals on name:"+(dog1.name.equals(dog2.name)));        print("equals on says:"+(dog1.says.equals(dog2.says)));    }    public static void main (String args[]){        Dog dog1 = new Dog();        Dog dog2 = new Dog();        Dog dog3 = dog1;        dog1.name = "spot";        dog2.name = "scruffy";        dog1.says = "Ruff!";        dog2.says = "Wurf!";        compare(dog1,dog2);        compare(dog2,dog3);        compare(dog1,dog3);    }}/*Output:with==:falsewith equals():false==on name:false==on says:falseequals on name:falseequals on says:falsewith==:falsewith equals():false==on name:false==on says:falseequals on name:falseequals on says:falsewith==:truewith equals():true==on name:true==on says:trueequals on name:trueequals on says:true*/

练习7

/*编写一个程序,模拟扔硬币的结果。*//*布尔类型0代表硬币正面,1代表硬币反面*/import java.util.Random;class Main{    public static void main (String args[]){        Random rand = new Random();        boolean i = rand.nextBoolean(20);        if (i){            System.out.println("抛得的硬币为反面");        }        else {            System.out.println("抛得的硬币为正面");        }    }} /*Output:抛得的硬币为正面*/

练习8

/*展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果。*/import static net.mindview.util.Print.*;class Main {    public static void main (String args[]){        long i1 = 0x123L;        print("i1 ="+Long.toBinaryString(i1));        long i2 = 0123;        print("i2 ="+Long.toBinaryString(i2));    }}/*Output:i1 =100100011i2 =1010011*/

练习9

/*分别显示用float和double指数计数法所能表示的最大和最小的数字。*/class Main {    public static void main (String args[]){        System.out.println("float_MAX="+Float.MAX_VALUE);        System.out.println("float_MIN="+Float.MIN_VALUE);        System.out.println("double_MAX="+Double.MAX_VALUE);        System.out.println("double_MIN="+Double.MIN_VALUE);    }}/*Output:float_MAX=3.4028235E38float_MIN=1.4E-45double_MAX=1.7976931348623157E308double_MIN=4.9E-324*/

练习10

/*编写一个具有两个常量值得程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,* 但是最低有效位为1(提示:使用十六进制常量来表示是最简单的方法)。取这两个值,用按位操作符一所有可能的方式结合运算他们,* 然后用Integer.toBinaryString()显示。*/import static net.mindview.util.Print.*; class Main {    public static void main (String args[]){        int i = 0x2aa;        int j = 0x555;        print("i:"+Integer.toBinaryString(i));        print("j:"+Integer.toBinaryString(j));        print("~i:"+Integer.toBinaryString(~i));        print("~j:"+Integer.toBinaryString(~j));        print("(i|j):"+Integer.toBinaryString(i|j));        print("(i&j):"+Integer.toBinaryString(i&j));        print("(i^i):"+Integer.toBinaryString(i^i));        print("(i|j):"+Integer.toBinaryString(~(i|j)));        print("~(i&j):"+Integer.toBinaryString(~(i&j)));        print("(i^j):"+Integer.toBinaryString(i^j));    }}/*Output:i:1010101010j:10101010101~i:11111111111111111111110101010101~j:11111111111111111111101010101010(i|j):11111111111(i&j):0(i^i):0(i|j):11111111111111111111100000000000~(i&j):11111111111111111111111111111111(i^j):11111111111*/

练习11

/*以一个所有位都为1的二进制数字开始,(使用十六进制常量),用有符号右移操作符对其进行右移,直至所有的二进制位都被移出为止,* 每移一位都要使用Integer.toBinaryString()显示结果*/import static net.mindview.util.Print.*; class Main{    public static void main (String args[]){        int a = Integer.MAX_VALUE;        print(Integer.toBinaryString(a));        print(Integer.toHexString(a));        while(a!=0){            print(Integer.toBinaryString(a>>=1));        }    }}/*Output:11111111111111111111111111111117fffffff1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110*/   

练习12

/*以一个所有位都为1的二进制数字开始,先左移它,然后用无符号右移操作符对其进行右移,直至所有的二进制位都被移除为止,* 每移一位都要使用Integer.toBinaryString()显示结果*/import static net.mindview.util.Print.*;class Main {    public static void main (String args[]){        int a = Integer.MAX_VALUE;        a<<=1;        print(Integer.toBinaryString(a));        while(a!=0){            print(Integer.toBinaryString(a>>>=1));        }    }}/*Output:1111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110*/

练习13

/*编写一个方法,它以二进制形式显示char类型的值。使用多个不同的字符来展示它。*/import static net.mindview.util.Print.*;class Main {  public static void main (String args[]){      print("a:"+Integer.toBinaryString('a'));      print("b:"+Integer.toBinaryString('b'));      print("c:"+Integer.toBinaryString('c'));  }}/*Output:a:1100001b:1100010c:1100011*/

练习14

/*编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较两个字符串,* 然后把结果打印出来。做==和!=比较的同时,用equals()做测试,在main()里面用几个不同的字符串对象调用这个方法。*/class Main{    public static void p(String s,boolean b){        System.out.println(s +":"+b);    }    public static void compare(String A,String B){        System.out.println("A:"+A+" B:"+B);        p("A==B:" , A==B);        p("A!=B" , A !=B);        System.out.print("A.equals(B):");        System.out.println(A.equals(B));    }    public static void main(String args[]){        compare("hello","hello");        String s = new String("fine");        compare("hello",s);    }}/*Output:* A:hello B:helloA==B::trueA!=B:falseA.equals(B):trueA:hello B:fineA==B::falseA!=B:trueA.equals(B):false*/

第三章完结。

原创粉丝点击