已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java 编程思想(第四版)习题答案 第二章 练习 1:public class PrimitiveTest static int i; static char c; public static void main(String args) System.out.println(“int = “ + i); System.out.println(“char = “ + c); 练习 2:public class HelloWorld public static void main(String args) System.out.println(“Hello World!“); 练习 3:public classATNTest public static void main(String args) classATypeName int i; double d; boolean b; void show() System.out.println(i); System.out.println(d); System.out.println(b); ATypeName a = newATypeName(); a.i = 3; a.d = 2.71828; a.b = false; a.show(); 练习 4:public class DataOnlyTest public static void main(String args) class DataOnly int i; double d; boolean b; void show() System.out.println(i); System.out.println(d); System.out.println(b); DataOnly data = new DataOnly(); data.i = 3; data.d = 2.71828; data.b = false; data.show(); 练习 5:public class DOTest2 public static void main(String args) class DataOnly int i; double d; boolean b; void show() System.out.println(i); System.out.println(d); System.out.println(b); DataOnly data = new DataOnly(); data.i = 234; data.d = 2.1234545; data.b = true; data.show(); 练习 6:public class StorageTest public static void main(String args) class StoreStuff int storage(String s) return s.length() * 2; StoreStuff x = new StoreStuff(); System.out.println(x.storage(“hi“); 练习 7: class StaticTest static int i = 47; class Incrementable static void increment() StaticTest.i+; public class ITest public static void main(String args) System.out.println(“StaticTest.i= “ + StaticTest.i); StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); System.out.println(“st1.i= “ + st1.i); System.out.println(“st2.i= “ + st2.i); Incrementable sf = new Incrementable(); sf.increment(); System.out.println(“After sf.increment() called: “); System.out.println(“st1.i = “ + st1.i); System.out.println(“st2.i = “ + st2.i); Incrementable.increment(); System.out.println(“After Incrementable.increment called: “); System.out.println(“st1.i = “ + st1.i); System.out.println(“st2.i = “ + st2.i); 练习 8:class StaticTest static int i = 47; class Incrementable static void increment() StaticTest.i+; public class OneStaticTest public static void main(String args) System.out.println(“StaticTest.i= “ + StaticTest.i); StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); System.out.println(“st1.i= “ + st1.i); System.out.println(“st2.i= “ + st2.i); Incrementable.increment(); System.out.println(“After Incrementable.increment() called: “); System.out.println(“st1.i = “ + st1.i); System.out.println(“st2.i = “ + st2.i); Incrementable.increment(); System.out.println(“After Incrementable.increment called: “); System.out.println(“st1.i = “ + st1.i); System.out.println(“st2.i = “ + st2.i); st1.i = 3; System.out.println(“After st1.i = 3, “); System.out.println(“st1.i = “ + st1.i); System.out.println(“st2.i = “ + st2.i); System.out.println(“Create another StaticTest, st3.“); StaticTest st3 = new StaticTest(); System.out.println(“st3.i = “ + st3.i); 练习 9:public classAutoboxTest public static void main(String args) boolean b = false; char c = x; byte t = 8; short s = 16; int i = 32; long l = 64; float f = 0.32f; double d = 0.64; Boolean B = b; System.out.println(“boolean b = “ + b); System.out.println(“Boolean B = “ + B); Character C = c; System.out.println(“char c = “ + c); System.out.println(“Character C = “ + C); Byte T = t; System.out.println(“byte t = “ + t); System.out.println(“Byte T = “ + T); Short S = s; System.out.println(“short s = “ + s); System.out.println(“Short S = “ + S); Integer I = i; System.out.println(“int i = “ + i); System.out.println(“Integer I = “ + I); Long L = l; System.out.println(“long l = “ + l); System.out.println(“Long L = “ + L); Float F = f; System.out.println(“float f = “ + f); System.out.println(“Float F = “ + F); Double D = d; System.out.println(“double d = “ + d); System.out.println(“Double D = “ + D); 练习 10:public class CommandArgTest public static void main(String args) intargs1=1,2,3; System.out.println(“args0 = “ + args10); System.out.println(“args1 = “ + args11); System.out.println(“args2 = “ + args12); 练习 11: public class Rainbow public static void main(String args) AllTheColorsOfTheRainbow atc = newAllTheColorsOfTheRainbow(); System.out.println(“atc.anIntegerRepresentingColors = “ + atc.anIntegerRepresentingColors); atc.changeColor(7); atc.changeTheHueOfTheColor(77); System.out.println(“Aftercolorchange,atc.anIntegerRepresentingColors=“+ atc.anIntegerRepresentingColors); System.out.println(“atc.hue = “ + atc.hue); classAllTheColorsOfTheRainbow int anIntegerRepresentingColors = 0; int hue = 0; void changeTheHueOfTheColor(int newHue) hue = newHue; int changeColor(int newColor) return anIntegerRepresentingColors = newColor; 练习 12:public class DocTest /* Entry poing to class System.out.println(new Date(); 练习 13-1:public class Documentation1 /* Afield comment */ public int i; /* Amethod comment */ public void f() 2:public class Documentation2 Date d = new Date(); void showDate() System.out.println(“Date = “ + d); 3:public class Documentation3 public static void main(String args) Date d = new Date(); System.out.println(“d = “ + d); 练习 14:public class Documentation4 public int i = 2; private int j = 3; public static void main(String args) Date d = new Date(); System.out.println(“d = “ + d); 练习 15:public class HelloDocTest public static void main(String args) System.out.println(“Hello World!“); 练习 16: class Tree int height; Tree() System.out.println(“Planting a seedling“); height = 0; Tree(int initialHeight) height = initialHeight; System.out.println(“Creating new tree that is “ + height + “ feet tall“); void info() System.out.println(“Tree is “ + height + “ feet tall“); void info(String s) System.out.println(s + “: Tree is “ + height + “ feet tall“); public class Overloading public static void main(String args) for(int i = 0; i 5; i+) Tree t = new Tree(i); (); (“overloading method“); / Overloaded constructor: new Tree(); 第三章 练习 1:public class PrintTest public static void main(String args) print(“Hello, from short form.“); P.rintln(“Hello from greggordon form.“); System.out.println(“Hello from long form.“); 练习 2:class Tube float level; public classAssign public static void main(String args) Tube t1 = new Tube(); Tube t2 = new Tube(); t1.level = 0.9f; t2.level = 0.47f; P.rintln(“1: t1.level: “ + t1.level + “, t2.level: “ + t2.level); t1 = t2; P.rintln(“2: t1.level: “ + t1.level + “, t2.level: “ + t2.level); t1.level = 0.27f; P.rintln(“3: t1.level: “ + t1.level + “, t2.level: “ + t2.level); 练习 3:class Box float a; public class PassObject2 static void f(Box y) y.a = 2.71828f; public static void main(String args) Box x = new Box(); x.a = 3.1416f; print(“1: x.a = “ + x.a); f(x); print(“2: x.a = “ + x.a); 练习 4:class VelocityCalculator static float velocity (float d, float t) if(t = 0) return 0f; else return d/t; public class VelocityTester public static void main(String args) float d = 565.3f; float t = 3.6f; System.out.println(“Distance: “ + d); System.out.println(“Time: “ + t); float v = VelocityCalculator.velocity(d, t); System.out.println(“Velocity: “ + v); 练习 5:class Dog String name; String says; void setName(String n) name = n; void setSays(String s) says = s; void showName() P.rintln(name); void speak() P.rintln(says); public class DogTest public static void main(String args) Dog spot = new Dog(); spot.setName(“Spot“); spot.setSays(“Ruff!“); Dog scruffy = new Dog(); scruffy.setName(“Scruffy“); scruffy.setSays(“Wurf!“); spot.showName(); spot.speak(); scruffy.showName(); scruffy.speak(); 练习 6:class Dog String name; String says; void setName(String n) name = n; void setSays(String s) says = s; void showName() P.rintln(name); void speak() P.rintln(says); public class DogCompare public static
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 13508-2025聚乙烯吹塑容器
- 2025年11月广东深圳市龙华区招聘社区网格员72人备考题库及一套完整答案详解
- 2025年洛阳市中心医院招聘合同制工作人员93人参考模拟试题及答案解析
- 2025年富阳区总工会公开招聘工会社会工作者3人备考题库及答案详解(历年真题)
- 2025广东梅州市梅县区雁洋镇村级党群服务中心政务服务专职工作人员招聘备考题库含答案详解(满分必刷)
- 南充市消防救援支队2025年关于面向社会招聘消防文员的备考题库(二)(6人)含答案详解(轻巧夺冠)
- 2025年马鞍山宁马城际招聘招聘车站协理员70人参考笔试题库及答案解析
- 2025青海西宁市湟中区正丰现代农业科技服务有限公司招聘1人参考模拟试题及答案解析
- 2025万祥社区卫生服务中心卫生室招聘笔试考试备考试题及答案解析
- 2025年鄂州招聘警务辅助人员66人备考题库及答案详解(真题汇编)
- 2025年秋季湖南省港航水利集团有限公司社会招聘备考题库附答案详解
- 河南省青桐鸣2026届高三上学期第二次联考语文试卷及参考答案
- 维护文化安全课件
- 汽车制造行业年终述职
- 交通运输公司安全管理工作计划及措施
- 工程监理居间协议书
- GB/T 46621-2025机械式停车设备报废条件
- 西安科技大学高新学院《电气工程专业英语》2024-2025学年第一学期期末试卷
- 22北京音乐合格考题
- 2025年工会考试练习题及答案
- 水库文明施工方案
评论
0/150
提交评论