第二章练习题(Chapter2)
来源:互联网 发布:淘宝宝贝主图更换技巧 编辑:程序博客网 时间:2024/06/02 01:13
*本文章仅供参考,习题的答案可以从www.mindviewinc.com处购买名为“The Thinking in Java Annotated Solution Guide”的电子文档中查找。
- 练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化,将他们的值打印出来,以验证Java执行了默认初始化。
class Initialization { int a; char b;}public class testInitialization { public static void main(String[] args) { Initialization ini = new Initialization(); System.out.println("1." + ini.a); System.out.println("2." + ini.b); }}
/**The first Thinking in Java example program. * Display a string and today's date. *@author zhangjian *@author Bruce Eckel *@version 4.0 *//** * Entrv point to class & application. * * @param args * array of string arguments * @throws exceptions * No exceptions thrown * * <pre> * System.out.println(new Date()); * </pre> */public class testHello { public static void main(String[] args) { System.out.println("Hello wrold! It is:"); System.out.println(new Date()); }}
public class testATypeName { public static void main(String[] args) { testATypeName atn = new testATypeName(); atn.getClass(); }}
class DataOnly { int i; double d; boolean b;}public class testDataOnly { public static void main(String[] args) { DataOnly d = new DataOnly(); System.out.println(d.i); System.out.println(d.d); System.out.println(d.b); }}
class DataOnly { int i; float f; boolean b;}public class testDataOnly2 { public static void main(String[] args) { DataOnly d1 = new DataOnly(); d1.i = 12; System.out.println(d1.i); d1.f = 12.0f; System.out.println(d1.f); d1.b = true; System.out.println(d1.b); }}
class testStorage { String s = "Hello world !"; private int storage(String s) { return s.length() * 2; } public void print() { System.out.println("storage(s) is:" + storage(s)); }}public class testStorageMethod { public static void main(String[] args) { testStorage ts = new testStorage(); ts.print(); }}
class StaticTest{ static int i = 47;}class Incrementable{ static void incrementable(){ StaticTest.i++; }}public class testIncrementable { public static void main(String[] args) { Incrementable.incrementable(); System.out.println(StaticTest.i); }}
class TestStatic1 { static int i = 12; static void addi() { i++; }}public class testStatic { public static void main(String[] args) { TestStatic1 ts0 = new TestStatic1(); System.out.println(ts0.i); TestStatic1 ts1 = new TestStatic1(); System.out.println(ts1.i); ts0.addi(); ts1.addi(); TestStatic1 ts2 = new TestStatic1(); System.out.println(ts2.i); }}
public class AutoboxingTest { public static void main(String[] args) { boolean bn = true; byte bt = 8; char c = 'c'; short s = 16; int i = 32; long l = 64; float f = 32.32f; double d = 64.64; Boolean BN = bn; Byte BT = bt; Character C = c; Short S = s; Integer I = i; Long L = l; Float F = f; Double D = d; System.out.println("boolean bn = " + bn); System.out.println("Boolean BN = " + BN); System.out.println(); System.out.println("byte bt = " + bt); System.out.println("Byte BT = " + BT); System.out.println(); System.out.println("char c = " + c); System.out.println("Character C = " + C); System.out.println(); System.out.println("short s = " + s); System.out.println("Short S = " + S); System.out.println(); System.out.println("int i = " + i); System.out.println("Integer I = " + I); System.out.println(); System.out.println("long l = " + l); System.out.println("Long L = " + L); System.out.println(); System.out.println("float f = " + f); System.out.println("Float F = " + F); System.out.println(); System.out.println("double d = " + d); System.out.println("Double D = " + D); System.out.println(); System.out.println("--------------------"); /* 装箱 */ Integer a = new Integer(12); Character ch = new Character('a'); System.out.println(ch + " = " + a); System.out.println("--------------------"); /* 拆箱 */ int b = new Integer(12); char chr = new Character('b'); System.out.println(chr + " = " + b); }}
public class ArgsTest { public static void main(String[] args) { if (args.length < 3) { System.out.println("No 3 arguments."); System.exit(1); } System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); }}
public class AllTheColorsOfTheRainbow { int anIntegerRepresentingColors; void changeTheHueOfTheColor(int newHue) { anIntegerRepresentingColors = newHue; } public static void main(String[] args) { AllTheColorsOfTheRainbow all = new AllTheColorsOfTheRainbow(); all.changeTheHueOfTheColor(12); System.out.println(all.anIntegerRepresentingColors); }}
import java.util.*;/** * The first Thinking in java example program. Displays a string and today's * date. * * @author Bruce Eckel * @author www.MindView.net * @version 4.0 */public class javadocHelloDate { /** * Entrv point 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()); }}/* 可以用命令行形式实现 javadoc -help * javadoc -d [输出文件保存目录] nameOfPackage * 可以用Eclipse实现 * Project->Generate javadoc... */
/** A class comment */public class Documentation1 { /** A field comment */ public int i; /** A method comment */ public void f() { }}
/** * <pre> * System.out.println(new Date()); * </pre> */public class Documentation2 {}/* 生成javadoc文档,必须有可以文档化的公共或受保护的类 */
/** * You can <em>even</em> insert a list: * <ol> * <li>Item one</li> * <li>Item two</li> * <li>Item three</li> * </ol> */public class Documentation3 {}/* 生成javadoc文档,必须有可以文档化的公共或受保护的类 */
/* javadoc只能为public和protected成员进行文档注释。 * 但是在测试过程中,只有public成员可以进行文档注释。WHY? * Eclipse中有权限选择,选择private则会显示所有类型成员。默认public。 * 命令行形式可以在要进行文档注释的类名前加 -private ,则显示所拥有类型成员。默认public和protected。 *//** * Menu <menu> * <li>class one * <ul> * <li>method one</li> * <li>method two</li> * <li>method three</li> * </ul> * </li> * <li>class two</li> * <li>class three</li></menu> */public class HtmlListTest { /** MethodOne */ public void f1() { } /** MethodTwo */ public void f2() { } /** MethodThree */ void f3() { }}/** ClassOne */class One {}/** ClassTwo */class Two {}
/** * List1(ol): * <ol> * <li>filed one</li> * <li>filed two</li> * <li>filed three</li> * </ol> */public class List1 { /** FiledOne */ public int i; /** FiledTwo */ protected int j; /** FiledThree */ private int k;}
/** * List2(ul): * <ul> * <li>bananas</li> * <li>apples</li> * <li>plums</li> * </ul> */public class List2 {}
/** * List3(dl) * <dl> * <dt>Apple</dt> * <dd>The apple is the pomaceous fruit of the apple tree</dd> * <dd><i>Musa domestica</i></dd> * <dt>Banana</dt> * <dd>The banana is the parthenocarpic fruit of the banana tree</dd> * <dd><i>Musa acuminata</i></dd> * <dt>Cherry</dt> * <dd>The cherry is the stone fruit of the genus <i>Prunus</i></dd> * </dl> */public class List3 {}
import java.util.Date;/** * 这是一个显示系统时间的类。 * * @author Bruce Eckel * @author 这是一个链接<a>http://www.MindView.net</a> * @version 4.0 */public class JavadocExercise2HelloDate { /** * 主函数 * * @param args * 字符串型数组参数 * @return 返回一个整数0 */ public static int main(String[] args) { System.out.println("Hello, it's: "); System.out.println(new Date()); return 0; }}
//Demonstration of both constructor//and ordinary method overloading/** 方法重载示例类 */class Tree { /** A filed comment */ int height; /** 默认构造器:用标准方式初始化 */ Tree() { System.out.println("Planting a seedling"); height = 0; } /** * 方法重载后的构造器:读取信来初始化 * * @param initialHeight * 取int型initialHeight作为形式参数,该整型数表示height的初始值 */ 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"); } /** * 信息显示方法重载 * * @param s * 取字符串s作为形式参数,该字符串表示重载后的方法进行的信息显示 */ void info(String s) { System.out.println(s + ": Tree is " + height + " feet tall"); }}/** * 方法重载 * * @author Bruce Eckel * @author www.MindView.net * @version 4.0 */public class Overloading { /** * The <code>main()</code> function which is called when the program is * executed by saying <code>java Overloading</code>. * * @param args * array passed from the command-line */ public static void main(String[] args) { for (int i = 0; i < 5; i++) { Tree t = new Tree(i); t.info(); t.info("overloaded method"); } }}
阅读全文
1 0
- 第二章练习题(Chapter2)
- 课后练习题(第二章)
- 数据结构与算法分析(JAVA版)Chapter2练习题
- 《深入理解计算机系统》(第二版)第二章练习题3
- 《深入理解计算机系统》(第二版)第二章练习题4
- 《深入理解计算机系统》(第二版)第二章练习题5
- Python核心编程(第二版)第二章练习题
- 初学Python-第二章练习题
- java编程思想(第四版)第二章练习题-2
- java编程思想(第四版)第二章练习题-3
- java编程思想(第四版)第二章练习题-9
- java编程思想(第四版)第二章练习题-10
- java编程思想(第四版)第二章练习题-12
- 算法导论(第三版) 第二章练习题
- PythonNLP学习进阶:第二章练习题(Python自然语言处理)
- Python核心编程(第二版)第三章练习题
- Python核心编程(第二版)第四章练习题
- Python核心编程(第二版)第五章练习题
- Linux mint安装Qt5
- java多线程之内存可见性-synchronized、volatile
- java安全初始化
- PAT-A-1018. Public Bike Management (30)
- react-native Modal的学习与使用
- 第二章练习题(Chapter2)
- 单例模式实现在线用户列表
- KNN算法(有监督学习算法)
- yii cli模式 杂记
- 221.map3-百度地图图层
- 聊聊JDBC
- linux netstat命令
- UESTC 1607 大学生足球联赛
- ubuntu使用openvpn