![面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)答案_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/23/77eef9e4-aecf-4ada-977f-fffea93d7d02/77eef9e4-aecf-4ada-977f-fffea93d7d021.gif)
![面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)答案_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/23/77eef9e4-aecf-4ada-977f-fffea93d7d02/77eef9e4-aecf-4ada-977f-fffea93d7d022.gif)
![面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)答案_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/23/77eef9e4-aecf-4ada-977f-fffea93d7d02/77eef9e4-aecf-4ada-977f-fffea93d7d023.gif)
![面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)答案_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/23/77eef9e4-aecf-4ada-977f-fffea93d7d02/77eef9e4-aecf-4ada-977f-fffea93d7d024.gif)
![面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)答案_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/23/77eef9e4-aecf-4ada-977f-fffea93d7d02/77eef9e4-aecf-4ada-977f-fffea93d7d025.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、面向?qū)ο蟪绦蛟O(shè)計(jì)作業(yè)一1. 類和對(duì)象的概念和關(guān)系是什么?對(duì)象是系統(tǒng)中用來描述客觀事物的一個(gè)實(shí)體,它是構(gòu)成系統(tǒng)的一個(gè)基本單位。一個(gè)對(duì)象由一組屬性和對(duì)這組屬性進(jìn)行操作的一組服務(wù)組成,類是具有相同屬性和服務(wù)的一組對(duì)象的集合。類是對(duì)象的模板,對(duì)象是類的實(shí)例2. 用UML表示交通工具Vehicle類及名為car1,car2及car3的三個(gè)Vehicle對(duì)象3. 簡(jiǎn)述對(duì)象之間的消息傳遞機(jī)制是如何實(shí)現(xiàn)的? 4. import語句的用途是什么?Java程序是否總要包括import語句?import保留字用于引入其他包中的類。Java如果使用的都是同一包的類的話則不需要import保留字。5. 什么是Java的
2、源文件?什么是字節(jié)碼文件?Java的源文件是以.java結(jié)尾的文本文件,字節(jié)碼文件是將Java源文件經(jīng)過Java編譯器編譯后的文件,字節(jié)碼文件不能直接運(yùn)行,只能運(yùn)行于Java虛擬機(jī)之上。6. Java虛擬機(jī)是什么?它有作用是什么?Java虛擬機(jī)是一個(gè)想象中的機(jī)器,在實(shí)際的計(jì)算機(jī)上通過軟件模擬來實(shí)現(xiàn)。Java語言的一個(gè)非常重要的特點(diǎn)就是與平臺(tái)的無關(guān)性。而使用Java虛擬機(jī)是實(shí)現(xiàn)這一特點(diǎn)的關(guān)鍵。一般的高級(jí)語言如果要在不同的平臺(tái)上運(yùn)行,至少需要編譯成不同的目標(biāo)代碼。而引入Java語言虛擬機(jī)后,Java語言在不同平臺(tái)上運(yùn)行時(shí)不需要重新編譯。Java語言使用模式Java虛擬機(jī)屏蔽了與具體平臺(tái)相關(guān)的信息,
3、使得Java語言編譯程序只需生成在Java虛擬機(jī)上運(yùn)行的目標(biāo)代碼(字節(jié)碼),就可以在多種平臺(tái)上不加修改地運(yùn)行。Java虛擬機(jī)在執(zhí)行字節(jié)碼時(shí),把字節(jié)碼解釋成具體平臺(tái)上的機(jī)器指令執(zhí)行。7. 描述對(duì)象聲明和對(duì)象生成之間的區(qū)別。使用內(nèi)存狀態(tài)圖來說明這種區(qū)別對(duì)象聲明是為對(duì)象的引用創(chuàng)建一個(gè)空間,而對(duì)象生成則是創(chuàng)建一個(gè)類的實(shí)例,即為對(duì)象分配空間,如果需要的話,其還會(huì)將對(duì)象空間的地址賦給其應(yīng)用。如 Tester t1;t1t1 = new Tester();t1 :Tester8. 編寫Java應(yīng)用程序,用一個(gè)對(duì)話框顯示當(dāng)前時(shí)間import javax.swing.*;import java.util.*;p
4、ublic class Test public static void main(String args) Date today = new Date( ); JOptionPane.showMessageDialog(null,today); 9. 下面的代碼段會(huì)有什么樣的輸出:class Q2main public static void main(String args) QuestionTwo q2; q2= new QuestionTwo(); q2.init(); q2.increment(); q2.increment(); System.out.println(q2.getCo
5、unt(); class QuestionTwo private int count; public void init() count=1; public void increment() count=count+1; public int getCount() return count; 輸出結(jié)果:310. 編寫可以根據(jù)用戶的年齡和身高給出推薦的體重的程序,利用下面的公式計(jì)算出推薦的體重:recommandedWeight=(height -100 + age/10)*0.9定義名為Height(身高)的公共服務(wù)類,他應(yīng)該有可以根據(jù)身高得出推薦體重的方法public class Test
6、public static void main(String args) Weight w1 = new Weight(); System.out.println( w1.getRecommendedWeight(30,170); class Weightpublic double getRecommendedWeight(int age,int height) return (height - 100 + age/10) *0.9; 作業(yè)二1.假如x的值為10,y的值為20,z的值為30,求出下列布爾表達(dá)式的值:a) x>y && y>x:falseb) (x&l
7、t;y+z) && (x+10<=20):truec) !(x<y+z) | !(x+10<=20):falsed) !(x=y) && (x!=y) && (x<y | y<x):true2.用switch語句重寫下面的if語句。selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selection:”);if (selection=0) System.out.println(“You selected Magenta”);else if
8、 (selection=1) System.out.println(“You selected Red”);else if (selection=2) System.out.println(“You selected Blue”);else if (selection=3) System.out.println(“You selected Green”);else System.out.println(“Invalid selection”);改寫為:selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enter selecti
9、on:”);swith (selection)case 0: System.out.println(“You selected Magenta”);break;case 1: System.out.println(“You selected Red”);break;case 2:System.out.println(“You selected Blue”);break;case 3:System.out.println(“You selected Green”);break;default: System.out.println(“Invalid selection”);)3.畫出下面兩個(gè)sw
10、itch語句的控制流程圖a) switch (choice) case 1: a=0; break; case 2: b=1; break; case 3: c=2; break; case 4: d=3: break;b) switch (choice) case 1: a=0; case 2: b=1; case 3: c=2; default: d=3;a)b)4.分別用for、do-while和while語句計(jì)算下面的累加和:c) 1+1/2+1/3+1/4+1/15for循環(huán):int x;double result=0.0F;for(double i=1;i<=15;i+) r
11、esult+=1/i;System.out.println(result);do-while循環(huán):double x=1;double result=0.0F;do result+=1/x; x+;while(x<=15);System.out.println(result);while循環(huán):double x=1;double result=0.0F;while(x<=15) result+=1/x; x+; ;System.out.println(result);d) 5+10+15+50for循環(huán):int x;int result=0;for(int i=1;i<=10;i
12、+) result+=i*5;System.out.println(result);do-while循環(huán):int x=1;int result=0;do result+=x*5; x+;while(x<=10);System.out.println(result);while循環(huán):int x=1;int result=0;while(x<=10) result+=x*5; x+;System.out.println(result);5.編寫一個(gè)計(jì)算閏年的程序,要求用戶輸入一個(gè)年份,如果用戶輸入的年份不在03000年內(nèi)則給予用戶提示要求其重新輸入,否則判斷該年份是否為閏年并返回結(jié)果。
13、public class Test public static void main(String args) LeapYear ly=new LeapYear(); System.out.println(puteLeapYear(1998); System.out.println(puteLeapYear(1900); System.out.println(puteLeapYear(2000); class LeapYearpublic boolean computeLeapYear(int year)if (year % 4 = 0 && year % 100 != 0 )r
14、eturn true;if (year % 100 = 0 && year % 400 = 0 )return true;return false;6.下列哪一組重載方法是不合法的?e) public void compute(int num)public int compute(int num)f) public void move(double length)public void move()g) public int adjust(double amount)public void adjust(double amount , double charge)h) publ
15、ic void doWork() public void doWork(String name)public int doWork(double num)第 a)組7.完成下面這個(gè)類中的前四個(gè)構(gòu)造方法。其中每一構(gòu)造方法都是用關(guān)鍵字this調(diào)用第五個(gè)構(gòu)造方法: class Cat private static final String DEFAULT_NAME = "NO NAME" private static final int DEFAULT_HGT=6; private static final double DEFAULT_WGT=10.0; private Str
16、ing name; private int height; private double weight; public cat() /分配缺省值給個(gè)成員變量 this(“”,0,0); public cat(String name) /將參數(shù)值賦給name這個(gè)成員變量,height和weight賦缺省值 this(name,0,0); public cat(String name int height) /將參數(shù)值分別賦給name和height兩個(gè)成員變量,weight賦缺省值 this(name,height,0); public cat(String name int weight) /將
17、參數(shù)值分別賦給name和weight兩個(gè)成員變量,height賦缺省值 this(name,0, weight); publie cat(String name,int height,double weight) =name; this.height=height; this.weight=weight; .8.為Fraction定義一個(gè)類方法compare,compare接受兩個(gè)Fraction對(duì)象f1和f2,并返回:1.如果f1小于f2,返回-12.如果f1等于f2,返回03.如果f1大于f2,返回1public static int compare (Fraction
18、f1, Fraction f2) double f1_dec = f1.decimal();double f2_dec = f2.decimal();if (f1_dec <f2_dec) return -1; else if(f1_dec =f2_dec) return 0; else return 1;作業(yè)三1.什么是異常?Java的異常處理機(jī)制是?異常(exception)表示在程序正常運(yùn)行過程中可能發(fā)生的錯(cuò)誤的情形。Java通過面向?qū)ο蟮姆椒▉硖幚懋惓?。在一個(gè)方法的運(yùn)行過程中,如果發(fā)生了異常,則這個(gè)方法生成代表該異常的一個(gè)對(duì)象,并把它交給運(yùn)行時(shí)系統(tǒng),運(yùn)行時(shí)系統(tǒng)尋找相應(yīng)的代碼來處理
19、這一異常。把生成異常對(duì)象并把它提交給運(yùn)行時(shí)系統(tǒng)的過程稱為拋棄(throw)一個(gè)異常。運(yùn)行時(shí)系統(tǒng)在方法的調(diào)用棧中查找,從生成異常的方法開始進(jìn)行回朔,直到找到包含相應(yīng)異常處理的方法為止,這一個(gè)過程稱為捕獲(catch)一個(gè)異常。2.分別輸入-1,0和12XY,請(qǐng)寫出這段代碼的輸出結(jié)果。int number1;trynumber1 = Integer.parseInt(JOptionPane.showInputDialog(null,"input");if (number1 !=0)throw new Exception("Not Zero");catch (
20、NumberFormatException e)System.out.println("Cannot convert to int");catch (Exception e) System.out.println("Error:"+e.getMessage();finallySystem.out.println("finally Clause Executed");輸入-1時(shí):Error:Not Zerofinally Clause Executed輸入0時(shí):finally Clause Executed輸入12XY時(shí):Cannot
21、convert to intfinally Clause Executed3. 分析下面程序代碼的輸出結(jié)果: class MyException extends Exception public MyException() public MyException(String msg) super(msg); public class ExceptionTest public static void f() throws MyException System.out.println("The 1st line of f()"); throw new MyException(&
22、quot;Originated in f()"); public static void main(String args) System.out.println("The 1st line of main()"); try System.out.println("The 2nd line of main()"); f(); System.out.println("The 3rd line of main()"); catch(MyException e) System.out.println(e.getMessage();
23、 finally System.out.println("The 4th line of main()"); System.out.println("The 5th line of main()"); 輸出結(jié)果為:The 1st line of main()The 2nd line of main()The 1st line of f()Originated in f()The 4th line of main()The 5th line of main()4. 分析下面程序代碼的輸出結(jié)果:class Shape void draw() System.o
24、ut.println("Shape.draw()"); class Circle extends Shape void draw() System.out.println("Circle.draw()"); class Square extends Shape void draw() System.out.println("Square.draw()"); class ShapeGenerator public Shape getShape(int index) switch(index) default: case 0: retur
25、n new Circle(); case 1: return new Square(); public void shapeDraw(Shape s) s.draw(); public class Shapes private static ShapeGenerator gen =new ShapeGenerator(); public static void main(String args) Shape s = new Shape3; for(int i = 0; i < s.length; i+) si = gen.getShape(i); for(int i = 0; i < s.length; i+) gen.shapeDraw(si); 輸出結(jié)果為:Circle
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國(guó)二位式差壓控制器數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年中國(guó)鎳毛細(xì)管市場(chǎng)調(diào)查研究報(bào)告
- 2025至2031年中國(guó)食道-氣管聯(lián)合搶救導(dǎo)管行業(yè)投資前景及策略咨詢研究報(bào)告
- 2025至2031年中國(guó)美容潤(rùn)膚香水香皂行業(yè)投資前景及策略咨詢研究報(bào)告
- 2025至2031年中國(guó)中底卡釘機(jī)行業(yè)投資前景及策略咨詢研究報(bào)告
- 2025至2030年杏干項(xiàng)目投資價(jià)值分析報(bào)告
- 2025至2030年中國(guó)油灰數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025至2030年中國(guó)大理石吊燈數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 建筑人造石生產(chǎn)設(shè)備考核試卷
- 噴槍在印刷行業(yè)中的應(yīng)用考核試卷
- 第7章-無人機(jī)法律法規(guī)
- 藥劑科基本藥物處方用藥狀況點(diǎn)評(píng)工作表
- 初中音樂聽課筆記20篇
- 央國(guó)企信創(chuàng)化與數(shù)字化轉(zhuǎn)型規(guī)劃實(shí)施
- 拆遷征收代理服務(wù)投標(biāo)方案
- 完形療法概述
- SL631-637-2012-水利水電工程單元工程施工質(zhì)量驗(yàn)收評(píng)定標(biāo)準(zhǔn)
- 商標(biāo)基礎(chǔ)知識(shí)課件
- 監(jiān)理質(zhì)量管理講義監(jiān)理工作的基本知識(shí)
- 涉詐風(fēng)險(xiǎn)賬戶審查表
- 2023年大學(xué)英語四級(jí)考試模擬真題及答案
評(píng)論
0/150
提交評(píng)論