內容簡介
《Java編程基礎實驗指導與習題解答》是主教材《Java 編程基礎》(陳國君編著,清華大學出版社出版)的配套實驗指導與習題解答。 全書分為兩部分。第一部分是實驗指導,共有 17 章,內容與主教材中的 17 章相對應。每章都有相關內 容的知識點,並包含若干上機實驗,每個實驗由實驗目的、實驗要求、程式模板和實驗指導等部分組成。 實驗內容由淺入深、循序漸進,知識點全面,並針對學習 Java 語言過程中遇到的重點和難點進行講解與 指導,強調實用性和易學性,可以幫助讀者進一步熟悉和掌握 Java 語言的語法知識及程式設計的方法。 第二部分是主教材的習題解答,對主教材中練習題和上機實驗題給出了詳細的解題思路和參考答案,並 對編程題提供了完整的程式代碼。
本書既可以作為大專院校計算機及其相關專業程式設計課程的教學用書,也可以作為各學校程式設 計公共選修課用書,還可以用作職業教育的培訓用書和 Java 初學者的入門書。
圖書目錄
目錄
第一部分 實 驗 指 導
第1章 Java語言概述 ················································································3
實驗1.1 Java語言開發環境的配置 ··········································································3
實驗1.2 JDK參考文檔的使用 ··················································································6
實驗1.3 Eclipse集成開發環境 ···············································································11
第2章 基本數據類型 ··················································································19
實驗2.1 取模運算與自增、自減運算 ····································································19
實驗2.2 整數與浮點數的除法與強制類型轉換 ····················································20
實驗2.3 邏輯運算符 ································································································21
實驗2.4 字元串與數值型數據的轉換 ····································································22
實驗2.5 從鍵盤輸入數據 ························································································23
第2章實驗參考答案 ··································································································25
第3章 結構語句 ························································································26
實驗3.1 if條件語句套用 ·························································································26
實驗3.2 for循環語句與套用 ···················································································27
實驗3.3 while循環語句與字元比較 ······································································28
實驗3.4 do-while循環語句 ·····················································································29
第3章實驗參考答案··································································································30
第4章 數組與字元串 ··················································································31
實驗4.1 從鍵盤輸入數據給數組元素賦值····························································31
實驗4.2 二維數組····································································································32
實驗4.3 字元串相等的比較····················································································33
實驗4.4 字元串方法的調用····················································································36
實驗4.5 命令行參數································································································37
第4章實驗參考答案··································································································38
第5章 類與對象
實驗5.1
實驗5.2
實驗5.3
實驗5.4
························································································40
類定義與創建對象····················································································40
參數傳遞····································································································42
有返回值可變參數方法的套用································································43
無返回值可變參數方法的套用································································44
第5章實驗參考答案··································································································45
第6章 Java語言類的特性 ·········································································46
實驗6.1 類的私有成員與公共成員········································································46
實驗6.2 類構造方法重載與默認構造方法····························································48
實驗6.3 在構造方法內調用另一個構造方法························································49
實驗6.4 方法的重載································································································51
實驗6.5 類的靜態成員····························································································52
第6章實驗參考答案··································································································55
第7章 繼承、抽象類與接口 ········································································57
實驗7.1 類的繼承····································································································57
實驗7.2 子類調用父類的方法················································································59
實驗7.3 方法的覆蓋································································································60
實驗7.4 抽象類········································································································61
實驗7.5 接口的定義與類實現接口········································································63
實驗7.6 接口實現類多重繼承及名字衝突····························································65
第7章實驗參考答案 ··································································································66
第8章 異常處理 ························································································68
實驗8.1 Java語言中的常見異常類 ········································································68
實驗8.2 多異常處理 ································································································70
實驗8.3 由方法拋出異常 ························································································71
實驗8.4 主動拋出異常 ····························································································73
第8章實驗參考答案 ··································································································74
第9章 輸入輸出 ························································································75
實驗9.1 FileInputStream類的套用 ·········································································75
實驗9.2 FileOutputStream類的套用 ·······································································76
實驗9.3 FileReader與FileWriter類的套用 ···························································78
實驗9.4 標準輸入輸出與重定向 ············································································79
實驗9.5 對象的寫入與讀取 ····················································································80
實驗9.6 檔案屬性的操作 ························································································83
實驗9.7 對檔案的隨機訪問 ····················································································86
第9章實驗參考答案 ··································································································87
第10章 泛型與容器類 ················································································89
實驗10.1 泛型類定義與方法的調用 ....................................................................89
實驗10.2 類作為類型實參的泛型套用 ................................................................90
實驗10.3 鍊表LinkedList的套用 ........................................................................92
第10章實驗參考答案 ..............................................................................................94
第11章 內部類、匿名內部類與Lambda表達式 ···········································95
實驗11.1 內部類 ....................................................................................................95
實驗11.2 匿名內部類 ............................................................................................98
實驗11.3 Lambda表達式 .....................................................................................99
第11章實驗參考答案 ............................................................................................100
第12章 圖形界面設計 ··············································································101
實驗12.1 創建視窗 ..............................................................................................101
實驗12.2 格線面板與文本組件 ..........................................................................103
實驗12.3 單選按鈕組件 ......................................................................................105
第12章實驗參考答案 ............................................................................................106
第13章 事件處理 ····················································································107
實驗13.1 動作事件 ..............................................................................................107
實驗13.2 滑鼠事件及處理程式 ..........................................................................109
實驗13.3 鍵盤事件及處理程式 ..........................................................................110
實驗13.4 為綁定屬性註冊監聽者 ......................................................................111
第13章實驗參考答案 ............................................................................................115
第14章 繪圖與動畫程式設計····································································116
實驗14.1 繪製橢圓和八邊形 ..............................................................................116
實驗14.2 製作一個小球在弧上滾動 ..................................................................118
實驗14.3 利用時間軸動畫製作一個旋轉的風扇 ..............................................119
第14章實驗參考答案 ............................................................................................122
第15章 多執行緒 ·······················································································123
實驗15.1 用Thread類創建執行緒 ........................................................................123
實驗15.2 實現Runnable接口創建執行緒 ............................................................125
實驗15.3 鐵路售票程式 ......................................................................................126
第15章實驗參考答案 ............................................................................................127
第16章 Java網路編程 ···········································································128
實驗16.1 使用URL類訪問網路資源 ................................................................128
實驗16.2 InetAddress程式設計 .........................................................................130
實驗16.3 基於TCP的通信程式設計 ................................................................131
第16章實驗參考答案 ............................................................................................134
第17章 Java資料庫程式設計 ··································································135
實驗17.1 利用Docker部署MySQL、phpMyAdmin與JDBC驅動程式的安裝
......................................................................................................135
實驗17.2 查詢資料庫 ..........................................................................................139
實驗17.3 Statement接口 .....................................................................................140
實驗17.4 PreparedStatement接口 ......................................................................142
實驗17.5 DatabaseMetaData與ResultSetMetaData接口 .................................144
實驗17.6 事務操作 ..............................................................................................146
第17章實驗參考答案 ............................................................................................148
第二部分 習 題 解 答
第1章習題解答 ·························································································151
第2章習題解答 ·························································································153
第3章習題解答 ·························································································157
第4章習題解答 ·························································································165
第5章習題解答 ·························································································173
第6章習題解答 ·························································································178
第7章習題解答 ·························································································181
第8章習題解答 ·························································································185
第9章習題解答 ·························································································187
第10章習題解答 ·······················································································192
第11章習題解答 ·······················································································195
第12章習題解答 ·······················································································197
第13章習題解答 ·······················································································203
第14章習題解答 ·······················································································212
第15章習題解答 ·······················································································222
第16章習題解答 ·······················································································226
第17章習題解答 ·······················································································234