面向對象程序設計作業答案_第1頁
面向對象程序設計作業答案_第2頁
面向對象程序設計作業答案_第3頁
面向對象程序設計作業答案_第4頁
面向對象程序設計作業答案_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、car1:Vehiclecar2:Vehiclecar3:Vehicle面向對象程序設計作業一1.類和對象的概念和關系是什么?對象是系統中用來描述客觀事物的一個實體,它是構成系統的一個基本單位。一個對象由一組屬性和對這組屬性進行操作的一組服務組成,類是具有相同屬性和服務的一組對象的集合。類是對象的模板,對象是類的實例2.用UM展示交通工具 Vehicle 類及名為carl , car2及car3的三個 Vehicle 對象對于賬戶的最小余額, 每個賬戶都共享一個共同 的值。3 .簡述對象之間的消息傳遞機制是如何實現的?當程序運行時,我們使用類和由類生成的對象來完成任務。而要命令類或對象執行 某

2、項任務,就需要給它發送一條消息(message)。為了能夠處理所接收到的消息,類或對象必須擁有相應的方法(method)。一個方法就是一個指令序列,也就是一段程序代碼,類似于C語言中的函數。為類定義的方法稱為類方法(class method),為對象定義的方法稱為實例方法(instance method)。類方法可以通過類直接調用,實例方法則必須先創建類的實例才能夠 調用。4 . import語句的用途是什么?Java程序是否總要包括import語句?import保留字用于引入其他包中的類。Java如果使用的都是同一包的類的話則不需要import保留字。5 .什么是Java的源文件?什么是字節

3、碼文件?Java的源文件是以.java結尾的文本文件,字節碼文件是將Java源文件經過Java編譯器編譯后的文件,字節碼文件不能直接運行,只能運行于Java虛擬機之上。6 . Java虛擬機是什么?它有作用是什么?Java虛擬機是一個想象中的機器,在實際的計算機上通過軟件模擬來實現。Java語言的一個非常重要的特點就是與平臺的無關性。而使用Java虛擬機是實現這一特點的關鍵。一般的高級語言如果要在不同的平臺上運行,至少需要編譯成不同的目標代碼。而引入Java7.8.9.語言虛擬機后Java語言在不同平臺上運行時不需要重新編譯。Java語言使用模式 Java虛擬機屏蔽了與具體平臺相關的信息,使得

4、Java語言編譯程序只需生成在 Java虛擬機上 運行的目標代碼(字節碼),就可以在多種平臺上不加修改地運行。Java虛擬機在執行字節碼時,把字節碼解釋成具體平臺上的機器指令執行。描述對象聲明和對象生成之間的區別。使用內存狀態圖來說明這種區別對象聲明是為對象的引用創建一個空間,而對象生成則是創建一個類的實例,即為對象分配空間,如果需要的話,其還會將對象空間的地址賦給其應用。如 Testert1;t1t1 = new Tester。;用一個對話框顯示當前時間編寫Java應用程序, import javax.swing.*; importjava.util.*;public class Test

5、public static void main(String口 args) Date today = new Date();JOptionPane.showMessageDialog(null,today);下面的代碼段會有什么樣的輸出:class Q2mainpublic static void main(String口 args) QuestionTwo q2;q2= new QuestionTwo();q2.init();q2.increment();q2.increment();System.out.println(q2.getCount();class QuestionTwopriv

6、ate int count;public void init()count=1;public void increment()count=count+1;public int getCount() return count;輸出結果:310. 編寫可以根據用戶的年齡和身高給出推薦的體重的程序,利用下面的公式計算出推薦的體重:recommandedWeight=(height -100 + age/10)*0.9定義名為Height (身高)的公共服務類,他應該有可以根據身高得出推薦體重的方法 public class Testpublic static void main(String arg

7、s)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;作業二1.假如xa) b)c)d)10 , y 的值為20, z 的值為x>y && y>x : false(x<y+z) && (x+10<=20) : true!(x<y

8、+z) | !(x+10<=20) : false!(x =y) && (x!=y) && (x<y | y<x)30,求出下列布爾表達式的值:true2.用switch 語句重寫下面的if 語句。selection=Inter.parseInt(JOptionPane.showInputDialog(null, ” Enter selection:”);if (selection=0)System.out.println(else if (selection= =1)System.out.println(else if (selection=

9、 =2)System.out.println(else if (selection =3)System.out.println( elseSystem.out.println(改寫為:You selected Magenta”);You selected Red” );You selected Blue”);You selected Green”);Invalid selection ”);selection=Inter.parseInt(JOptionPane.showInputDialog(null, ” Enter selection:”); swith (selection)case

10、0:System.out.println( “You selected Magenta” );break;System.out.println(You selected Red");break;case 1:System.out.println( You selected Blue");break;case 2:System.out.println( You selected Green");break;default:System.out.println( Invalid selection ");)3 .畫出下面兩個switch語句的控制流程圖a)

11、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)是d=34 .分別用for、do-while和while語句計算下面的累加和: c)1 + 1/2+1/3+1/4+ +1/15for循環:int x;double result=0.0F;for(double i=1;i<=15;i+) result+=1/i;S

12、ystem.out.println(result);do-while 循環:double x=1;double result=0.0F;doresult+=1/x;x+;while(x<=15);System.out.println(result);while循環:double x=1;double result=0.0F;while(x<=15)result+=1/x;x+;System.out.println(result);d) 5+10+15+50for 循環:int x;int result=0;for(int i=1;i<=10;i+) result+=i*5;S

13、ystem.out.println(result);do-while 循環:int x=1;int result=0;doresult+=x*5;x+;while(x<=10);System.out.println(result);while 循環:int x=1;int result=0;while(x<=10)result+=x*5;x+;System.out.println(result);5.編寫一個計算閏年的程序,要求用戶輸入一個年份,如果用戶輸入的年份不在03000 年內則給予用戶提示要求其重新輸入,否則判斷該年份是否為閏年并返回結果。public class Test

14、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)ifif(year % 4 = 0 && year % 100 != 0 )(year % 100 = 0&&am

15、p; year % 400 = 0 )return true;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) public void doWork() pu

16、blic void doWork(String name) public int doWork(double num) 第a)組7 .完成下面這個類中的前四個構造方法。其中每一構造方法都是用關鍵字this 調用第五個構造方法:class Catprivate static final String DEFAULT_NAME = "NO NAME"private static final int DEFAULT_HGT=6;private static final double DEFAULT_WGT=10.0;private String name;private int

17、height;private double weight;public cat()/分配缺省值給個成員變量this(“” ,0,0);public cat(String name) 將參數彳I賦給 name這個成員變量,height和weight賦缺省值 this(name,0,0);public cat(String name int height)將參數值分別賦給 name和height兩個成員變量,weight賦缺省值 this(name,height,0);public cat(String name int weight)將參數值分別賦給 name和weight兩個成員變量,heig

18、ht賦缺省值 this(name,0, weight);publie cat(String name,int height,double weight) =name;this.height=height; this.weight=weight; 8 .為Fraction定義一個類方法 compare, compare接受兩個 Fraction對象fl和f2,并返回:1 .如果f1 小于 f2 ,返回 -12 .如果fl等于f2 ,返回03 .如果fl大于f2 ,返回1public static int compare (Fraction f1, Fraction f2) do

19、uble 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;作業三1. 什么是異常?Java的異常處理機制是?異常(exception)表示在程序正常運行過程中可能發生的錯誤的情形。Java 通過面向對象的方法來處理異常。在一個方法的運行過程中,如果發生了異常,則這個方法生成代表該異常的一個對象,并把它交給運行時系統,運行時系統尋找相應的代碼來處理這一異常。把生成異常對象并把它提交

20、給運行時系統的過程稱為拋棄(throw) 一個異常。 運行時系統在方法的調用棧中查找,從生成異常的方法開始進行回朔,直到找到包含相應異常處理的方法為止,這一個過程稱為捕獲(catch) 一個異常。2. 分別輸入-1, 0和12XY,請寫出這段代碼的輸出結果。int number1;trynumber1 = Integer.parseInt(JOptionPane.showInputDialog(null,"input");if (number1 !=0)throw new Exception("Not Zero");catch (NumberFormat

21、Exception e)System.out.println("Cannot convert to int");catch (Exception e)System.out.println("Error:"+e.getMessage();finallySystem.out.println("finally Clause Executed");輸入 -1 時:Error:Not Zerofinally Clause Executed輸入0 時:finally Clause Executed輸入12XY 時:Cannot convert t

22、o intfinally Clause Executed3. 分析下面程序代碼的輸出結果:class MyException extends Exception public MyException() public MyException(String msg) super(msg); public class ExceptionTestpublic static void f() throws MyException System.out.println("The 1st line of f()");throw new MyException("Origina

23、ted 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();finally System.

24、out.println("The 4th line of main()");System.out.println("The 5th line of main()");輸出結果為:The 1st line of main()The 2nd line of main()The 1st line of f()Originated in f()The 4th line ofmain()The 5th line ofmain()4. 分析下面程序代碼的輸出結果:class Shape void draw()System.out.println("Shap

25、e.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: return 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.shap

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論