JAVA编程思想课后习题答案-.pdf

上传人:索**** 文档编号:76246943 上传时间:2023-03-08 格式:PDF 页数:10 大小:11.41KB
返回 下载 相关 举报
JAVA编程思想课后习题答案-.pdf_第1页
第1页 / 共10页
JAVA编程思想课后习题答案-.pdf_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《JAVA编程思想课后习题答案-.pdf》由会员分享,可在线阅读,更多相关《JAVA编程思想课后习题答案-.pdf(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、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 class ATNTest public

2、 static void main(String args)class ATypeName int i;double d;boolean b;void show()System.out.println(i);System.out.println(d);System.out.println(b);ATypeName a=new ATypeName();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

3、;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.p

4、rintln(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.

5、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.printl

6、n(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:);S

7、ystem.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(

8、);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(Afte

9、r 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.

10、out.println(st3.i=+st3.i);练习 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;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

11、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);Syste

12、m.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)System.out.println(args0=+args0);System.out.println(args1=

13、+args1);System.out.println(args2=+args2);练习 11:public class Rainbow public static void main(String args)AllTheColorsOfTheRainbow atc=new AllTheColorsOfTheRainbow();System.out.println(atc.anIntegerRepresentingColors=+atc.anIntegerRepresentingColors);atc.changeColor(7);atc.changeTheHueOfTheColor(77);S

14、ystem.out.println(After color change,atc.anIntegerRepresentingColors=+atc.anIntegerRepresentingColors);System.out.println(atc.hue=+atc.hue);class AllTheColorsOfTheRainbow int anIntegerRepresentingColors=0;int hue=0;void changeTheHueOfTheColor(int newHue)hue=newHue;int changeColor(int newColor)return

15、 anIntegerRepresentingColors=newColor;练习 12:public class DocTest /*Entry poing to class&application.*param args array of string arguments*throws exceptions No exceptions thrown*/public static void main(String args)System.out.println(Hello,its:);System.out.println(new Date();练习 13-1:public class Docu

16、mentation1 /*A field comment*/public int i;/*A method 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 Doc

17、umentation4 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);hei

18、ght=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)

19、for(int i=0;i 5;i+)Tree t=new Tree(i);t.info();t.info(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 Tub

20、e float level;public class Assign 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:

21、+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;pu

22、blic 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=V elocityCalculator.velocity(d,t);System.out.println(V elocity:+v);练习 5:class Dog String name;String says;void setName(String n)name=n;void se

23、tSays(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

24、.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 void main(String args)Dog spot=new Dog();spot.setName(Spot);spot.setSays(Ruff!);

25、Dog scruffy=new Dog();scruffy.setName(Scruffy);scruffy.setSays(Wurf!);spot.showName();spot.speak();scruffy.showName();scruffy.speak();Dog butch=new Dog();butch.setName(Butch);butch.setSays(Hello!);butch.showName();butch.speak();P.rintln(Comparison:);P.rintln(spot=butch:+(spot=butch);P.rintln(spot.eq

26、uals(butch):+spot.equals(butch);P.rintln(butch.equals(spot):+butch.equals(spot);P.rintln(Now assign:spot=butch);spot=butch;P.rintln(Compare again:);P.rintln(spot=butch:+(spot=butch);P.rintln(spot.equals(butch):+spot.equals(butch);P.rintln(butch.equals(spot):+butch.equals(spot);P.rintln(Spot:);spot.showName();spot.speak();P.rintln(Butch:);butch.showName();butch.speak();练习 7:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 实施方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁