Java编程思想第四版第三章练习
来源:互联网 发布:东莞知美医院可靠吗 编辑:程序博客网 时间:2024/05/17 21:55
练习1:使用“简短的”和 正常的打印语句来编写一个程序
import java.util.Date;import net.mindview.util.Print.*;/*要想使用这个类库,必须从www.MindView.net或其镜像之一下载本书的代码包,然后解压代码目录树,并在你的计算机的CLASSPATH环境变量中添加该代码目录树的根目录。(你最终会获得有关类路径的完整介绍,但是你也应该尽早习惯它带来的麻烦。唉,它是你在使用Java时最常见的问题之一。)*/public class Exercise3_1 { public static void main(String[] args) { Print(new Date()); System.out.println(new Date());}}
练习2:创建一个包含一个float域的类。并用这个类来展示别名机制。
public class Exercise3_2 { public static void main(String[] args) { Integral integral1 = new Integral(); Integral integral2 = new Integral(); integral1.f=9f; integral2.f=47f; System.out.println("1:integral1.f="+integral1.f+",integral2.f="+integral2.f); integral1=integral2; System.out.println("2:integral1.f="+integral1.f+",integral2.f="+integral2.f); integral1.f=27f; System.out.println("3:integral1.f="+integral1.f+",integral2.f="+integral2.f); }}class Integral{ float f;}/* Output 1:integral1.f=9.0,integral2.f=47.0 2:integral1.f=47.0,integral2.f=47.0 3:integral1.f=27.0,integral2.f=27.0 */
练习3:创建一个包含一个float域的类。并用这个类来展示方法调用时的别名机制。
public class Exercise3_3 { static void f(Integral y) { y.f=1.0f; } public static void main(String[] args) { Integral x = new Integral(); x.f = 2.0f; System.out.println("1:x.f="+x.f); f(x); System.out.println("2:x.f="+x.f);}}/* Output 1:x.f=2.0 2:x.f=1.0*/
练习4 :编写一个计算速度的程序,它所使用的距离和时间都是常量。
public class Exercise3_4 { public static void main(String[] args) { float distance = 100; float time=20; float velocity =distance/time; System.out.println("Velocity = "+velocity+" m/s"); }}
练习5:创建一个名为dog的类,它包含两个string域:name和says。在main()方法中,创建两个Dogs对象,一个名为spot(它的叫声为“ Ruff!”),另一个名为scruffy(它的叫声为”Wuff!”)。然后显示它们的名字和叫声。
public class Exercise3_5 { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.name = "spot"; dog1.says = "Ruff!"; System.out.println(dog1.name+" says "+dog1.says); Dog dog2 = new Dog(); dog2.name = "scruffy"; dog2.says = "Wurf!"; System.out.println(dog2.name+" says "+dog2.says);}}class Dog{ String name; String says;}
练习6:在练习(5)的基础上,创建一个新的Dog索引,并对其赋值为spot对象。测试用==和equals()方法来比较所有引用的结果
public class Exercise3_6 { static void compare(Dog dog1,Dog dog2) { System.out.println("== on top references: "+(dog1==dog2)); System.out.println(".equals() on top references: "+dog1.equals(dog2)); System.out.println("== on name: "+(dog1.name==dog2.name)); System.out.println(".equals() on name: "+dog1.name.equals(dog2.name)); System.out.println("== on Says: "+(dog1.says==dog2.says)); System.out.println(".equals() on Says: "+dog1.says.equals(dog2.says)); } public static void main(String[] args) { Dog dog1 = new Dog(); dog1.name = "spot"; dog1.says = "Ruff!"; Dog dog2 = new Dog(); dog2.name = "scruffy"; dog2.says = "Wurf!"; Dog dog3 = dog1; System.out.println("Compareing dog1 and dog2 objects"); compare(dog1, dog2); System.out.println("Compareing dog1 and dog3 objects"); compare(dog1, dog3); System.out.println("Compareing dog2 and dog3 objects"); compare(dog2, dog3); } } class Dog{ String name; String says; }
练习7:编写一个程序,模拟扔硬币的结果。
public class Exercise3_7 { public static void main(String[] args) { Random random = new Random(47); boolean flip = random.nextBoolean(); System.out.println(flip==true?"Head":"Tail");}}
练习8:展示用十六进制和八进制计数法来操作long值,用Long.toBinaryString()来显示结果。
public class Exercise3_8 { public static void main(String[] args) { long l1 = 0x2f; System.out.println("l1: "+Long.toBinaryString(l1)); long l2 = 0X2F; System.out.println("l2: "+Long.toBinaryString(l2)); long l3 = 0177; System.out.println("l3: "+Long.toBinaryString(l3));}}/* Outputl1: 101111l2: 101111l3: 1111111*/
练习9:分别显示用float和double指数记数法所能表示的最大和最小的数字。
public class Exercise3_9 { public static void main(String[] args) { System.out.println("Float.MAX_VALUE = "+Float.MAX_VALUE); System.out.println("Float.MIN_VALUE = "+Float.MIN_VALUE); System.out.println("Double.MAX_VALUE = "+Double.MAX_VALUE); System.out.println("Double.MIN_VALUE = "+Double.MIN_VALUE);}}/* Output Float.MAX_VALUE = 3.4028235E38Float.MIN_VALUE = 1.4E-45Double.MAX_VALUE = 1.7976931348623157E308Double.MIN_VALUE = 4.9E-324 */
练习10: 编写一个具有两个常量值的程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,但其最低有效位为1(提示:使用十六进制常量来表示最简单的方法).取两个值,用按位操作符以所有可能方式结合运算它们,然后anteger.toBinaryString()显示
public class Exercise3_10 { public static void main(String[] args) { int i1 = 0Xaaaaaaaa; int i2 = 0x55555555; System.out.println("i1 = "+Integer.toBinaryString(i1)); System.out.println("i2 = "+Integer.toBinaryString(i2)); System.out.println("~i1 = "+Long.toBinaryString(~i1)); System.out.println("~i2 = "+Long.toBinaryString(~i2)); System.out.println("i1&i1 = "+Long.toBinaryString(i1&i1)); System.out.println("i1|i1 = "+Long.toBinaryString(i1|i1)); System.out.println("i1^i1 = "+Long.toBinaryString(i1^i1)); System.out.println("i1&i2 = "+Long.toBinaryString(i1&i2)); System.out.println("i1|i2 = "+Long.toBinaryString(i1|i2)); System.out.println("i1^i2 = "+Long.toBinaryString(i1^i2));}}
练习11:以一个最高有效位为1的二进制数字开始(提示:使用十六进制常量),用有符号右移操作符对其进行右移,直至所有的二进制位都被移除为止,每移一位都要使用Integer.toBinaryString()显示结果
public class Exercise3_11 {public static void main(String[] args) { int i = 0X80808080; System.out.println(Integer.toBinaryString(i)); for (int j = 0; j < 32; j++) { i>>=1; System.out.println(Integer.toBinaryString(i)); }}}
练习12:以一个所有位为1的二进制数字开始,先左移它,然后用无符号右移操作符对其右移,直至所有的二进制位都被移除为止,每移一位都要使用Integer.toBinaryString()显示结果。
public class Exercise3_12 { public static void main(String[] args) { int i = -1<<1; System.out.println(Integer.toBinaryString(i)); for (int j = 0; j < 32; j++) { i>>>=1; System.out.println(Integer.toBinaryString(i));}}}
练习13:编写一个方法,它以二进制形式显示char类型的值,使用多个不同的字符显示它。
public class Exercise3_13 { static void f(char c) { System.out.println(c+": "+Integer.toBinaryString(c)); } public static void main(String[] args) { f('A');}}
练习14 :编写一个接收两个字符串参数的方法,用各种布尔值的比较关系来比较这两个字符串,然后把结果打印出来。做==和!=的同时,用equals()作测试。在main()里面用几个不同的字符串对象调用这个方法。
public class Exercise3_14 { static void compare(String s1,String s2) { System.out.println("s1 ="+s1+",s1 ="+s1); p(s1+"=="+s2,s1==s2); p(s1+"!="+s2,s1!=s2); p(s1+".equals("+s2+")",s1.equals(s2)); } static void p(String s,boolean b) { System.out.println(s+" "+b); } public static void main(String[] args) { compare("hello", "hello"); String s = new String("hello"); compare("hello", s); compare("hello", "World");}}
- Java编程思想第四版 第三章个人练习
- Java编程思想第四版第三章练习
- Java编程思想第四版 第四章个人练习
- Java编程思想第四版第四章练习
- java编程思想 第四章 练习4
- Java编程思想(第四版)*第二章 个人练习
- Java编程思想第四版 *第五章 个人练习
- java编程思想 第四版 第六章 个人练习
- Java编程思想第四版*第七章*个人练习
- Java编程思想第四版第8章练习
- Java编程思想第四版13章 个人练习
- java编程思想 第四版 第六章 个人练习
- Java编程思想第四版第二章练习
- Java编程思想第四版第五章练习
- Java编程思想第四版第六章练习
- Java编程思想第四版第七章练习
- Java编程思想第四版第八章练习
- java编程思想(第四版)第三章练习题-10
- Tree形结构
- python基础-笔记
- Tarjan算法小结1——SCC
- Error: need a single repository as argument.(已解决)
- 20171103周测题
- Java编程思想第四版第三章练习
- oprofile性能分析优化程序
- Scala——基于Akka的并发编程和分布式应用程序开发
- MapReduce数据类型及自定义MapReduce数据类型
- physx 3.3.4中的 Revolute Joint
- 接口回调基础理解
- QVTKOpenGLWidget
- 路漫漫其修远兮,吾将上下而求索
- log4j过滤掉不必要的日志