JAVA编程思想课后习习题答案_第1页
JAVA编程思想课后习习题答案_第2页
JAVA编程思想课后习习题答案_第3页
JAVA编程思想课后习习题答案_第4页
JAVA编程思想课后习习题答案_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java编程思想(第四版)习题答案第二章练习1:public class PrimitiveTest static int i;static char c;public static void main(String args) "int = " + i);"char = " + c);练习2:public class HelloWorld public static void main(String args) "Hello World!");练习3:public class ATNTest public static void m

2、ain(String args) class ATypeName int i;double d;boolean b;void show() ATypeName a = new ATypeName(); = 3; = ; = false;();练习4:public class DataOnlyTest public static void main(String args) class DataOnly int i;double d;boolean b;void show() DataOnly data = new DataOnly(); = 3; = ; = false;();练习5:publ

3、ic class DOTest2 public static void main(String args) class DataOnly int i;double d;boolean b;void show() DataOnly data = new DataOnly(); = 234; = ; = true;();练习6:public class StorageTest public static void main(String args) class StoreStuff int storage(String s) return () * 2;StoreStuff x = new Sto

4、reStuff();"hi");练习7:class StaticTest static int i = 47;class Incrementable static void increment() +; public class ITest public static void main(String args) "= " + ;StaticTest st1 = new StaticTest();StaticTest st2 = new StaticTest();"= " + ;"= " + ;Incrementa

5、ble sf = new Incrementable();();"After () called: ");" = " + ;" = " + ;();"After called: ");" = " + ;" = " + ;练习8:class StaticTest static int i = 47;class Incrementable static void increment() +; public class OneStaticTest public static voi

6、d main(String args) "= " + ;StaticTest st1 = new StaticTest();StaticTest st2 = new StaticTest();"= " + ;"= " + ;();"After () called: ");" = " + ;" = " + ;();"After called: ");" = " + ;" = " + ; = 3;"After

7、= 3, ");" = " + ;" = " + ;"Create another StaticTest, st3.");StaticTest st3 = new StaticTest();" = " + ;练习9:public class AutoboxTest public static void main(String args) boolean b = false;char c = 'x'byte t = 8;short s = 16;int i = 32;long l = 64;

8、float f = ;double d = ;Boolean B = b;"boolean b = " + b); "Boolean B = " + B); Character C = c;"char c = " + c);"Character C = " + C);Byte T = t;"byte t = " + t);"Byte T = " + T);Short S = s;"short s = " + s);"Short S = "

9、; + S);Integer I = i;"int i = " + i);"Integer I = " + I);Long L = l;"long l = " + l);"Long L = " + L);Float F = f;"float f = " + f);"Float F = " + F);Double D = d;"double d = " + d);"Double D = " + D);练习10:public class C

10、ommandArgTest public static void main(String args) "args0 = " + args0);"args1 = " + args1);"args2 = " + args2);练习11:public class Rainbow public static void main(String args) AllTheColorsOfTheRainbow atc = new AllTheColorsOfTheRainbow();" = " + ;(7);(77);"

11、After color change, = " + ;" = " + ;class AllTheColorsOfTheRainbow 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

12、to class & application.* param args array of string arguments* throws exceptions No exceptions thrown*/public static void main(String args) "Hello, it's: "); Date();练习13-1:public class Documentation1 /* A field comment */public int i;/* A method comment */public void f() 2:public c

13、lass Documentation2 Date d = new Date();void showDate() "Date = " + d);3:public class Documentation3 public static void main(String args) Date d = new Date();"d = " + d);练习14:public class Documentation4 public int i = 2;private int j = 3;public static void main(String args) Date

14、d = new Date();"d = " + d);练习15:public class HelloDocTest public static void main(String args) "Hello World!");练习16:class Tree int height;Tree() "Planting a seedling");height = 0;Tree(int initialHeight) height = initialHeight;"Creating new tree that is " + hei

15、ght + " feet tall");void info() "Tree is " + height + " feet tall");void info(String 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);();("

16、overloading method");("Hello from greggordon form.");"Hello from long form.");练习2:class Tube float level;public class Assign public static void main(String args) Tube t1 = new Tube();Tube t2 = new Tube(); = ; = ;("1: : " + + ", : " + ;t1 = t2;("2: :

17、" + + ", : " + ; = ; ("3: : " + + ", : " + ;练习3:class Box float a;public class PassObject2 static void f(Box y) = ;public static void main(String args) Box x = new Box(); = ;print("1: = " + ;f(x);print("2: = " + ;练习4:class VelocityCalculator sta

18、tic 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 = ;float t = ;"Distance: " + d);"Time: " + t);float v = (d, t);"Velocity: " + v);练习5:class Dog String name;String says;voi

19、d setName(String n) name = n;void setSays(String s) says = s;void showName() (name);void speak() (says);public class DogTest public static void main(String args) Dog spot = new Dog();("Spot");("Ruff!");Dog scruffy = new Dog();("Scruffy");("Wurf!");();();(); ();练习6:class Dog String name;String says;void setName(String n) name = n;void setSays(String s) says = s;void showName() (name);void speak() (says);public

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论