JAVA编程思想(第四版)练习题答案 unit 2 --- Think in JAVA
- 格式:doc
- 大小:57.50 KB
- 文档页数:11
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 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);}}A TypeName 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;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 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 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) {int[]args1={1,2,3};System.out.println("args[0] = " + args1[0]);System.out.println("args[1] = " + args1[1]);System.out.println("args[2] = " + args1[2]);}}练习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);System.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 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, it's: ");System.out.println(new Date());}}练习13-1:public class Documentation1 {/** 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 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 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: " + 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 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();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.equals(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:。
这是第2章的答案:Exercise 1//: c02:E01_HelloWorld.java//+M java E01_HelloWorld/****************** Exercise 1 ******************* Following the HelloDate.java example in this* chapter, create a "hello, world" program that* simply prints out that statement. You need* only a single method in your class (the "main"* one that gets executed when the program* starts). Remember to make it static and to* include the argument list, even though you* don't use the argument list. Compile the* program with javac and run it using java. If* you are using a different development* environment than the JDK, learn how to compile* and run programs in that environment.***********************************************/public class E01_HelloWorld {public static void main(String args[]) {System.out.println("Hello, world!");}} ///:~The +M directive tells my makefile builder tool to add this command (java HelloWorld) to the commands that it automatically runs while building the program. This way, if the execution fails for any reason then the make will abort and show that something is wrong for a particular program. This is a very simple form of built-in testing: automatically running the program to ensure no exceptions are thrown. You’ll see this on most of the programs in this solution guide, and the makefiles that come with the source code (that is packaged with this guide) will automatically run the programs.Exercise 2//: c02:E02_ATypeName.java//+M java E02_ATypeName/****************** Exercise 2 ******************* Find the code fragments involving ATypeName* and turn them into a program that compiles and * runs.***********************************************/ public class E02_ATypeName {public static void main(String args[]) {E02_ATypeName a = new E02_ATypeName();}} ///:~Exercise 3//: c02:E03_DataOnly.java//+M java E03_DataOnly/****************** Exercise 3 ****************** * Turn the DataOnly code fragments into a* program that compiles and runs.***********************************************/ public class E03_DataOnly {int i;float f;boolean b;public static void main(String[] args) {E03_DataOnly d = new E03_DataOnly();d.i = 47;d.f = 1.1f;d.b = false;}} ///:~Exercise 4//: c02:E04_DataOnly2.java//+M java E04_DataOnly2/****************** Exercise 4 ****************** * Modify Exercise 3 so that the values of the* data in DataOnly are assigned to and printed * in main().***********************************************/ public class E04_DataOnly2 {int i;float f;boolean b;public static void main(String[] args) {E04_DataOnly2 d = new E04_DataOnly2();d.i = 47;System.out.println("d.i = " + d.i);d.f = 1.1f;System.out.println("d.f = " + d.f);d.b = false;System.out.println("d.b = " + d.b);}} ///:~Exercise 5//: c02:E05_Storage.java//+M java E05_Storage/****************** Exercise 5 ******************* Write a program that includes and calls the* storage() method defined as a code fragment in* this chapter.***********************************************/public class E05_Storage {String s = "Hello, World!";int storage(String s) {return s.length() * 2;}void print() {System.out.println("storage(s) = " + storage(s));}public static void main(String[] args) {E05_Storage st = new E05_Storage();st.print();}} ///:~You could have also made storage( ) a static method and just called it from main( ).Exercise 6//: c02:E06_StaticFun.java//+M java E06_StaticFun/****************** Exercise 6 ******************* Turn the StaticFun code fragments into a* working program.***********************************************/class StaticTest {static int i = 47;}public class E06_StaticFun {static void incr() { StaticTest.i++; }public static void main(String[] args) {E06_StaticFun sf = new E06_StaticFun();sf.incr();E06_StaticFun.incr();}} ///:~Exercise 7//: c02:E07_ShowArgs.java//+M java E07_ShowArgs A B C/****************** Exercise 7 ******************* Write a program that prints three arguments* taken from the command line. To do this,* you'll need to index into the command-line* array of Strings.***********************************************/public class E07_ShowArgs {public static void main(String[] args) {System.out.println(args[0]);System.out.println(args[1]);System.out.println(args[2]);}} ///:~Note that you’ll get a run-time exception if you run the program without enough arguments. You should actually test for the length of the array first, like this://: c02:E07_ShowArgs2.java//+M java E07_ShowArgs2 A B C/****************** Exercise 7 ******************* Testing for the length of the array first.***********************************************/public class E07_ShowArgs2 {public static void main(String[] args) {if(args.length < 3) {System.out.println("Need 3 arguments");System.exit(1);}System.out.println(args[0]);System.out.println(args[1]);System.out.println(args[2]);}} ///:~The System.exit( ) call terminates the program and passes its argument back to the operating system as a result (with most operating systems, a non-zero result indicates that the program execution failed).Exercise 8//: c02:E08_AllTheColorsOfTheRainbow.java//+M java E08_AllTheColorsOfTheRainbow/****************** Exercise 8 ******************* Turn the AllTheColorsOfTheRainbow example into* a program that compiles and runs.***********************************************/public class E08_AllTheColorsOfTheRainbow {int anIntegerRepresentingColors;void changeTheHueOfTheColor(int newHue) {anIntegerRepresentingColors = newHue;}public static void main(String[] args) {E08_AllTheColorsOfTheRainbow all =new E08_AllTheColorsOfTheRainbow();all.changeTheHueOfTheColor(27);}} ///:~Exercise 9//: c02:E09_HelloDate.java//+M java E09_HelloDate//+M mkdir HelloDate//+M javadoc E09_HelloDate.java –d HelloDate/****************** Exercise 9 ******************* Find the code for the second version of* HelloDate.java, which is the simple comment* documentation example. Execute javadoc on the* file and view the results with your Web* browser.***********************************************/import java.util.*;/** The first Thinking in Java example program.* Displays a string and today's date.* @author Bruce Eckel* @author * @version 2.0*/public class E09_HelloDate {/** Sole entry point to class & application* @param args array of string arguments* @return No return value* @exception exceptions No exceptions thrown*/public static void main(String[] args) {System.out.println("Hello, it's: ");System.out.println(new Date());}} ///:~The command to run javadoc on the file and to place the results in a separate subdirectory called HelloDate is embedded as the third +M comment directive above. Note that javadoc doesn’t automatically create the destination directory.Exercise 10//: c02:E10_docTest.java//+M mkdir docTest//+M javadoc E10_docTest.java –d docTest/****************** Exercise 10 ****************** Turn docTest into a file that compiles and* then run it through javadoc. Verify the* resulting documentation with your Web browser.***********************************************//** A class comment */public class E10_docTest {/** A variable comment */public int i;/** A method comment */public void f() {}} ///:~You must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +M comment directives. Note that we’re only concerned that the file compiles, so there’s no makefile directive to run it.Exercise 11//: c02:E11_docTest2.java//+M mkdir docTest2//+M javadoc E11_docTest2.java –d docTest2/****************** Exercise 11 ****************** Add an HTML list of items to the documentation* in Exercise 10.***********************************************//** A class comment* <pre>* System.out.println(new Date());* </pre>*/public class E11_docTest2 {/** A variable comment */public int i;/** A method comment* You can <em>even</em> insert a list:* <ol>* <li> Item one* <li> Item two* <li> Item three* </ol>*/public void f() {}} ///:~I simply added the HTML code fragments from the chapter examples. You must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +M comment directives.Exercise 12//: c02:E12_HelloWorldDoc.java//+M java E12_HelloWorldDoc//+M mkdir HelloWorldDoc//+M javadoc E12_HelloWorldDoc.java –d HelloWorldDoc/****************** Exercise 12 ****************** Take the program in Exercise 1 and add comment* documentation to it. Extract this comment* documentation into an HTML file using javadoc* and view it with your Web browser.***********************************************//** A first example from <I>Thinking in Java,* 2nd edition</I>. Demonstrates the basic class* structure and the creation of a* <code>main()</code> function.*/public class E12_HelloWorldDoc {/** The <code>main()</code> function which iscalled when the program is executed by saying<code>java E12_HelloWorldDoc</code>.@param args array passed from the command-line*/public static void main(String args[]) {System.out.println("Hello, world!");}} ///:~You must still execute javadoc and verify the results, but you can see the commands that are added to the makefile to do this, as +M comment directives.。
上机实践1初识Java实验1一个简单的应用程序1.相关知识点Java语言的出现是源于对独立于平台语言的需要,即这种语言编写的程序不会因为芯片的变化而发生无法运行或出现运行错误的情况。
目前,随着网络的迅速发展,Java语言的优势越发明显,Java已经成为网络时代最重要的语言之一。
Sun公司要实现“编写一次,到处运行”(Write once,run anywhere的目标,就必须提供相应的Java运行平台,目前Java运行平台主要分为下列3个版本。
(1Java SE:称为Java标准版或Java 标准平台。
Java SE 提供了标准的JDK开发平台。
利用该平台可以开发Java桌面应用程序和低端的服务器应用程序,也可以开发Java Applet 程序。
当前成熟的新的JDK版本为JDK 1.6。
(2Java EE:称为Java企业版或Java企业平台。
使用J2EE可以构建企业级的服务应用,Java EE平台包含了Java SE平台,并增加了附加类库,以便支持目录管理、交易管理和企业级消息处理等功能。
(3Java ME:称为Java微型版或Java小型平台。
Java ME是一种很小的Java运行环境,用于嵌入式的消费产品中,如移动电话、掌上电脑或其他无线设备等。
无论上述哪种Java运行平台都包括了相应的Java虚拟机(Java Virtual Machine,虚拟机负责将字节码文件(包括程序使用的类库中的字节码加载到内存,然后采用解释方式来执行字节码文件,即根据相应硬件的机器指令翻译一句执行一句。
J2SE平台是学习掌握Java语言的最佳平台,而掌握J2SE又是进一步学习J2EE和J2ME所必需的。
2.实验目的本实验的目的是让学生掌握开发Java应用程序的三个步骤:编写源文件、编译源文件和运行应用程序。
3.实验要求编写一个简单的Java应用程序,该程序在命令行窗口输出两行文字:“你好,欢迎学习Java”和“We are students”。
thinking in java习题答案Thinking in Java习题答案在学习编程语言Java的过程中,练习习题是非常重要的一部分。
通过解答习题,我们可以加深对Java语言的理解,巩固所学知识,并且提高编程能力。
本文将为大家提供一些Thinking in Java习题的答案,希望能够帮助大家更好地学习和掌握Java编程。
1. 以下是一个简单的Java程序,输出结果是什么?```javapublic class Test {public static void main(String[] args) {int a = 5;int b = 10;System.out.println("a + b = " + a + b);}}```答案:输出结果是"a + b = 510"。
这是因为在Java中,"+"操作符在遇到字符串时会进行字符串连接操作,而不是数值相加。
因此,a和b会被当做字符串连接,而不是数值相加。
2. 以下是一个Java程序,输出结果是什么?```javapublic class Test {public static void main(String[] args) {int a = 5;int b = 10;System.out.println("a + b = " + (a + b));}}```答案:输出结果是"a + b = 15"。
这是因为在这个程序中,我们使用了小括号将a + b括起来,这样就会先进行数值相加操作,然后再进行字符串连接操作。
3. 以下是一个Java程序,输出结果是什么?```javapublic class Test {public static void main(String[] args) {String s = "Hello";s = s + " World";System.out.println(s);}}```答案:输出结果是"Hello World"。
java编程思想第四版习题答案Java编程思想是一本经典的Java编程教材,它深入浅出地介绍了Java编程的基本原理和思想。
为了更好地理解和掌握这本书,我特意整理了第四版习题的答案,并在实践中验证了这些答案的正确性。
在第一章中,书中提到了Java编程的基本思想和概念。
习题一要求我们解释Java程序的编译和执行过程。
编译过程是将Java源代码转换成字节码的过程,而执行过程是虚拟机将字节码转换成机器码并执行的过程。
这两个过程是相互独立的,编译过程只需要一次,而执行过程可以多次进行。
习题二要求我们解释Java中的关键字和标识符的概念。
关键字是Java语言中预先定义的具有特殊含义的单词,如class、public、static等。
而标识符是由程序员自定义的用来表示变量、方法、类等的名称,标识符要符合一定的命名规则,如不能以数字开头,不能包含特殊字符等。
在第二章中,书中介绍了Java中的数据类型和运算符。
习题一要求我们解释Java中的基本数据类型和引用数据类型的区别。
基本数据类型是Java语言中预先定义的数据类型,如int、float、boolean等,它们的值直接存储在变量中。
而引用数据类型是由程序员自定义的数据类型,如类、接口等,它们的值存储在堆内存中,变量中只存储引用地址。
习题二要求我们解释Java中的赋值运算符和逻辑运算符。
赋值运算符用于将一个值赋给一个变量,如a = 10;逻辑运算符用于对多个条件进行逻辑判断,如&&表示与运算,||表示或运算。
在第三章中,书中介绍了Java中的控制流程和数组。
习题一要求我们解释Java中的if语句和switch语句的用法。
if语句用于根据条件判断是否执行某段代码,而switch语句用于根据不同的值选择执行不同的代码块。
习题二要求我们解释Java中的for循环和while循环的用法。
for循环用于重复执行一段代码,可以指定循环的初始条件、循环条件和循环步长;而while循环用于当满足某个条件时重复执行一段代码,循环条件在循环开始前判断。
java第四版课后习题答案Java第四版课后习题答案Java是一种广泛应用于软件开发领域的编程语言,具有跨平台、面向对象等特点。
对于学习Java的人来说,课后习题是巩固知识、提高编程能力的重要方式。
本文将为读者提供Java第四版课后习题的答案,帮助读者更好地理解和掌握Java编程。
一、基础知识1. 什么是Java虚拟机(JVM)?它的作用是什么?答:Java虚拟机(JVM)是Java程序运行的环境,它负责将Java源代码编译成字节码,并在不同的操作系统上运行。
JVM的作用是实现Java的跨平台特性,使得Java程序可以在不同的操作系统上运行。
2. Java中的八种基本数据类型是什么?答:Java中的八种基本数据类型分别是byte、short、int、long、float、double、char和boolean。
3. 什么是面向对象编程(OOP)?Java是一种面向对象的编程语言吗?答:面向对象编程(OOP)是一种编程范式,通过将数据和方法封装在对象中,以对象为中心进行程序设计和开发。
Java是一种面向对象的编程语言,它支持封装、继承和多态等面向对象的特性。
二、控制流程1. Java中的条件语句有哪些?答:Java中的条件语句包括if语句、switch语句和三元运算符。
2. Java中的循环语句有哪些?答:Java中的循环语句包括for循环、while循环和do-while循环。
3. 如何在循环中使用break和continue语句?答:break语句用于立即终止循环,跳出循环体。
continue语句用于跳过当前循环的剩余代码,继续下一次循环。
三、数组和集合1. 如何声明和初始化一个一维数组?答:可以使用以下方式声明和初始化一个一维数组:```int[] array = new int[5]; //声明一个长度为5的整型数组int[] array = {1, 2, 3, 4, 5}; //声明并初始化一个整型数组```2. 如何声明和初始化一个二维数组?答:可以使用以下方式声明和初始化一个二维数组:```int[][] array = new int[3][2]; //声明一个3行2列的整型二维数组int[][] array = {{1, 2}, {3, 4}, {5, 6}}; //声明并初始化一个整型二维数组```3. Java中常用的集合类有哪些?答:Java中常用的集合类有ArrayList、LinkedList、HashSet、TreeSet、HashMap和TreeMap等。
java编程思想第四版习题答案【篇一:java编程思想第四版_读书笔记】面向对象程序设计(object-oriented programming oop),uml(unitied modelling language 统一建模语言)。
将对象想像成“服务提供者” ,它们看起来像什么?能够提供哪些服务?需要哪些对象?2.java中动态绑定是默认行为。
java采用动态内存分配方式,通过new操作在堆(heap)的内存池中动态创建对象。
java存储结构类型:1)寄存器2)堆栈,主要存储对象引用3)堆,主要用于存放所有的java对象4)常量存储,也就是程序代码区5)非ram存储,如流对象和持久化对象。
基本类型不用new来创建变量,而且这个变量直接存储”值”,并置于堆栈中。
3.biginteger和bigdecimal的使用。
当变量作为类的成员使用时当变量作为类的成员使用时,java才确保给定其默认初当变量作为类的成员使用时始值,但是在方法中定义的变量,它有可能是任意值。
面向对象的程序设计可以归纳为“向对象发送消息” 。
关键字static。
4.javadoc只能为public和protected成员进行文档注释,但是也可以通过-private进行标记注释。
javadoc常用方法: @see 引用其他类,link package.class#member label}, {@ {@docroot},{@inheritdoc},@version,@ author,@since,@param,@return,@throws,@deprecated。
5.整数除法会直接去掉结果的小数位。
基本类型的对象如果直接对它们赋值,对象指向同一个常量存储区,但是如果通过对象来初始化则会指向不同的堆的存储区。
如:string st1 = new string(a);string st2 = new string(a); st1==st2 false string st1 = a; string st2 = a; st1==st2 true6.逻辑操作符:与()、或(||)、非(!),其中与()、或(||)会产生短路现象。
JAVA 2实用教程习题解答习题一(第1章)一、问答题1.James Gosling2.需3个步骤:用文本编辑器编写源文件。
使用javac编译源文件,得到字节码文件。
使用解释器运行程序。
3.由类所构成,应用程序必须有一个类含有public static void main(String args[])方法,含有该方法的类称为应用程序的主类。
不一定,但最多有一个public类。
4.set classpath=D:\jdk\jre\lib\rt.jar;.;5. java和class6. java Bird7. 独行风格(大括号独占行)和行尾风格(左大扩号在上一行行尾,右大括号独占行)二、选择题1.B2.D。
三、阅读程序1.(a)Person.java。
(b)两个字节码,分别是Person.class和Xiti.class。
(c)得到“NoSuchMethodError”,得到“NoClassDefFoundError: Xiti/class”,得到“您好,很高兴认识您nice to meet you”习题二(第2章)一、问答题1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。
标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。
false不是标识符。
2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。
不是关键字。
class implements interface enum extends abstract。
3.boolean,char,byte,short,int,long,float,double。
4.float常量必须用F或f为后缀。
double常量用D或d为后缀,但允许省略后缀。
5.一维数组名.length。
二维数组名.length。
二、选择题1.C2.ADF3.B4.BE5.【代码2】【代码3】【代码4】【代码5】6.B。
Java编程思想(第四版)习题答案第二章练习1:public class PrimitiveTest {}练习2:public class HelloWorld {}练习3:public class ATNTest {}练习4:public class DataOnlyTest {class DataOnly {int i;double d;boolean b;void show() {System、out、println(i);System、out、println(d);public 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);public static void main(String[] args) {public static void main(String[] args) {}System、out、println("Hello World!");static int i; static char c;}public static void main(String[] args) { System、out、println("int = " + i);System、out、println("char = " + c); ATypeName a = new ATypeName();a、i = 3;a、d = 2、71828;a、b = false;a、show();}}}}System、out、println(b);DataOnly data = new DataOnly();data、i = 3;data、d = 2、71828;data、b = false;data、show();练习5:public class DOTest2 {}练习6:public class StorageTest { }练习7:class StaticTest {}static int i = 47;}class StoreStuff {}int storage(String s) {}}class DataOnly {}int i;double d;boolean b;void show() {}public static void main(String[] args) { 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();public static void main(String[] args) {return s、length() * 2;StoreStuff x = new StoreStuff();System、out、println(x、storage("hi"));class Incrementable {}public class ITest {}练习8:class StaticTest {}class Incrementable {}public class OneStaticTest {public static void main(String[] args) {System、out、println("StaticTest、i= " + StaticTest、i);迁终樹邬锾羆餾。
// TIJ4 Chapter Object, Exericise 3, page 90// object/ATNTest.java// Find the code fragments involving ATypeName and turn them into a program// that compiles and runs.public class ATNTest {public 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();}}-------------------------------------------------// TIJ4 Chapter Object, Exercise 9, page 90// Write a program that demonstrates that autoboxing works for all the primitive // types and their wrappers.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 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);}}---------------------------------------------------------// TIJ4 Chapter Object, Exercise 10, page 90// Write a program that prints three arguments taken from the command line. To do // this you'll need to index into the command-line array of Strings.public class CommandArgTest {public static void main(String[] args) {System.out.println("args[0] = " + args[0]);System.out.println("args[1] = " + args[1]);System.out.println("args[2] = " + args[2]);}}--------------------------------------------------// object/DataOnlyTest.java// TIJ4 Chapter Object Exercise 4 page 90// Turn the DataOnly code fragments into a program that compiles and runspublic 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();}}--------------------------------------------------// object.DocTest.java// TIJ4 Chapter Object, Exercise 12, page 90/* Find the code for the second version of HelloDate.java, which is the simple * comment documentation example. Execute Javadoc on the file and view the * results with your Web browser.*/import java.util.*;/** The first Thinking in Java example program.* Displays a string and today's date.* @author Burce Eckel* @author * @version 4.0*/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, it's: ");System.out.println(new Date());}} /* Output: (55% match)*/----------------------------------------------------// object.Documentation1.java// TIJ4 Chapter Object, Exercise 13 - 1/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. *//** A class comment */public class Documentation1 {/** A field comment */public int i;/** A method comment */public void f() {}}---------------------------------------------------// object.Documentation1.java// TIJ4 Chapter Object, Exercise 13 - 2/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. */import java.util.*;// object/Documentation2.java/*** <pre>* Uses* System.out.println(new Date());* </pre>*/public class Documentation2 {Date d = new Date();void showDate() {System.out.println("Date = " + d);}}---------------------------------------------------// object.Documentation1.java// TIJ4 Chapter Object, Exercise 13 - 3/* Run Documentation1.java, Documentation2.java and Documentation3.java * through Javadoc. Verify the resulting documentation with your Web browser. */import java.util.*;// object/Documentation3.java/*** You can even insert a list:* <ol>* <li> Item one* <li> Item two* <li> Item three* </ol>*/public class Documentation3 {public static void main(String[] args) {Date d = new Date();System.out.println("d = " + d);}}---------------------------------------------------// object/Documentation4.java// TIJ4 Chapter Object, Exercise 14, page 90// Add an HTML list of items to the documentation in the previous exercise. import java.util.*;// object/Documentation4.java/*** You can even insert a list:* <ol>* <li> Item one* <li> Item two* <li> Item three* </ol>* Another test list* <ol>* <li> One* <li> Two* <li> Three* </ol>*/public class Documentation4 {/** Let's try a public field list* <ol>* <li> One* <li> Two* <li> Three* </ol>*/public int i = 2;/*** A private field list (-private to see)* <ol>* <li> One* <li> Two* <li> Three* </ol>*/private int j = 3;/*** Another list can be inserted here to help explain the* following method call* <ol>* <li> One* <li> Two* <li> Three* </ol><br>* but may be formatted differently in Method Summary*/public static void main(String[] args) {/*** Let's try another test list here* <ol>* <li> One* <li> Two* <li> Three* </ol>*/Date d = new Date();System.out.println("d = " + d);}}--------------------------------------------------// object/DOTest2.java// TIJ4 Chapter Object, Exercise 5, page 90// Modify the previous exercise so that the values of the data in DataOnly are// assigned to and printed in main().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();}}----------------------------------------------------// object/HelloDocTest.java// TIJ4 Chapter Object, Exercies 15, page 91/* Take the program in Exercise 2 and add comment documentation to it. Extract * this comment documentation into an HTML file using Javadoc and view it with * your Web browser.*//*** Public class contained in file of the same name that includes main()*/public class HelloDocTest {/** main method executed by java*/public static void main(String[] args) {System.out.println("Hello World!");}}---------------------------------------------------// TIJ4 Chapter Object, Exericise 2, page 89// object/HelloWorld.java// Following the HelloDate.java example in this chapter, create a "hello, world" // program that simply displays that statement.public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");}}---------------------------------------------------// object/ITest.java// TIJ4 Chapter Object, Exercise 7, page 90// Turn the Incrementable code fragments into a working program.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);}}----------------------------------------------------// object/OneStaticTest.java// TIJ4 Chapter Object, Exercise 8, page 90/* Write a program that demonstrates that, no matter how many objects you * create of a particular class, there is only one instance of a particular* static field of that class.*/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);}}-----------------------------------------------------// object/Overloading.java// TIJ4 Chapter Object, Exercise 16, page 91/* In the Initialization and Cleanup chapter, locate the Overloading.java* example and add Javadoc documentation. Extract this comment documentation * into and HTML file using Javadoc and view it with your Web browser.*/// initialization/Overloading.java// Demonstration of both constructor// and ordinary method overloading./** creates type Tree wth two constructors and one info method*/class Tree {int height;/** no-argument constructor* assigns height = 0*/Tree() {System.out.println("Planting a seedling");height = 0;}/** constructor taking an int argument,* assigns height that int argument*/Tree(int initialHeight) {height = initialHeight;System.out.println("Creating new tree that is " + height + " feet tall");}/** method to print height of tree object*/void info() {System.out.println("Tree is " + height + " feet tall");}/** overloaded method to print string argument* and height of a tree object*/void info(String s) {System.out.println(s + ": Tree is " + height + " feet tall");}}/** class to test construction of tree objects*/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();}}----------------------------------------------------。