




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、7 / 8得分、程序修改(20分)說(shuō)明:描述程序中的語(yǔ)法錯(cuò)誤原因并修改,每小題4分,錯(cuò)誤原因和改正錯(cuò)誤各2分。l.isPrime方法用于檢查參數(shù) num是否是質(zhì)數(shù)。public boolean isPrime(int num) boolean result = false 。for(int i=2。 i<num。 i+)if(num%i=0) break 。 if(i>=num) result = true。 return result 。2 .具體類(lèi) Reader 實(shí)現(xiàn)了 Readable 接口。interface Readable void read()。 class Read
2、er implements Readable void read() System.out.println("I Can read it.")。 3 . main方法調(diào)用重載的 max方法求2個(gè)數(shù)的最大值。public class Error03 public static double max(int a, double b) return a>b?a:b° public static double max(double a, int b) return a>b?a:b° public static void main(String口 ar
3、gs) double c = max(10, 100) ° 4.子類(lèi)Child覆蓋了父類(lèi) Parent中的output方法。class Parentpublic final void output() System.out.println("Parent") class Child extends Parentpublic void output() System.out.println("Child")° ° 5. main方法調(diào)用sum方法求數(shù)組所有元素的和。public class Error05 public dou
4、ble sum(double口 array) double result = 0。for (double value : array) result += value。 return result 。public static void main(String口 args) double arr = 1.0, 2.0, 3.0,4.0, 5.0。System.out.println(sum(arr)。得分、程序填空(20分)說(shuō)明:填充程序中的空白,使程序能夠正確運(yùn)行,每空 2分。1 .以下程序功能為輸入多個(gè)班的考試成績(jī),并分別計(jì)算每個(gè)班的總成績(jī)。import java.util.Scanner
5、 。public class Score /inputScore方法用于輸入一個(gè)班的所有成績(jī),參數(shù)num是班級(jí)的學(xué)生人數(shù)public static double口 inputScore(int num) double口 array = new doublenum。Scanner scanner = new Scanner(System.in)。for (int i = 0。i <(1)。 i+) arrayi = scanner.nextDouble()。return array 。public static double sumScore(double口 array) double r
6、esult = 0。for (int i = 0。 i < array.length 。 i+) result += arrayi。return result 。public static void main(String口 args) double口口 scores。int numOfClass, numOfStudent 。Scanner scanner = new Scanner(System.in)。System.out.print("一共有幾個(gè)班?")。numOfClass = scanner.nextInt() 。scores =(2。for (int i
7、 = 0。 i < numOfClass 。 i+) System.out.println("第"+ (i + 1) + "班有幾人?")。numOfStudent = scanner.nextInt()。scoresi = inputScore( (3) 。for (int i = 0。 i < numOfClass 。 i+) System.out.print(" 第"+ (i + 1) + " 班的總分:")。System.out.println(sumScore( (4) J) 。2 .以下程序
8、定義了 Circle和Cylinder兩個(gè)類(lèi)。類(lèi) 1, Circle.javapublic class Circle private double radius 。 / 圓的半徑public Circle() /無(wú)參構(gòu)造方法/ 調(diào)用有參構(gòu)造方法將radius初始化為0.0public Circle(double radius) (6)/把參數(shù)radius賦給數(shù)據(jù)域radiuspublic double getArea()return (7)。 /求圓的面積,使用 Math.PI public double getRadius() return radius 。public void setRa
9、dius(double r) radius = r 。得分類(lèi) 2, Cylinder.javapublic class Cylinder extends Circle private double height。 /圓柱的高度public Cylinder() /無(wú)參構(gòu)造方法this.height = 0.0。public Cylinder(double radius, double height) (8)。/調(diào)用父類(lèi)構(gòu)造方法將radius初始化為參數(shù) radiusthis.height = height。Overridepublic double getArea() double area1
10、 =(9)。 /求圓柱表面積double area2 =(10)。 /求圓柱的底面積return area1 + area2。public double getHeight() return height。public void setHeight(double height) this.height = height。三、閱讀程序(20分)說(shuō)明:閱讀以下每段程序,寫(xiě)出運(yùn)行的結(jié)果,每小題 5分。1.閱讀程序1class Data public int a = 10, b = 100。public class Read01 public static void main(String口 args
11、) int a = 10, b = 100。int array = 10,100。Data data = new Data()。System.out.println("a=" + a + ",b=" + b)。System.out.println("array0="+ array。+",array1="+array1)。System.out.println("data.a=" + data.a + ",data.b=" + data.b) swap(a, b)。oswap(a
12、rray) 。swap(data) 。System.out.println("a=" + a + ",b=" + b)。System.out.println("array0="+array。+",array1="+array1)。System.out.println("data.a=" + data.a + ",data.b=" + data.b)public static void swap(int a, int b) int t = a。a = b。b = t 。publ
13、ic static void swap(int array) int t = array0。 array0 = array1。 array1 = tpublic static void swap(Data data) Ooint t = data.a。 data.a = data.b 。 data.b = t 。閱讀程序2public class Read02 public static void main(String口 args) A x = new B()。System.out.println("x.i: "+x.i)。System.out.println("
14、;(2)(B)x.i: "+(B)x).i)。System.out.println("(3)x.j: "+x.j)。System.out.println("(4)(B)x.j: "+(B)x).j)。System.out.println("(5)x.m1():"+x.m1()。System.out.println("(6) (B)x.m1(): "+(B)x).m1()System.out.println("(7)x.m2():"+x.m2()。System.out.println(&
15、quot;(8)x.m3():"+x.m3()。oclass A public int i=1。public static int j=11。public static String m1() return "類(lèi) A 的靜態(tài)方法public String m2() return "類(lèi) A 的實(shí)例方法 m2."。 public String m3() return "類(lèi) A 的實(shí)例方法 m3."。 m1."。class B extends Apublic int i=2。public static int j=22。publi
16、c static String m1() return "類(lèi) B 的靜態(tài)方法public String m2() return "類(lèi) B 的實(shí)例方法 m2."。 m1."。2.3 .閱讀程序3class Person public Person() System.out.println("Person()") class Employee extends Person public Employee() this("調(diào)用 Employee(s)")。System.out.println("Employee
17、()")。° public Employee(String s) System.out.println(s) class Faculty extends Employee° public Faculty() System.out.println("Faculty()") class Test° public static void main(String口 args) new Faculty() ° 4 .閱讀程序4,分別寫(xiě)出??處的值是30和50的輸出結(jié)果。public class Read04 public static
18、 void main(String口 args) int value = ?。try if(value<40)throw new Exception("value is too small")System.out.println("value="+value)。 catch (Exception e) System.out.println(e.getMessage()。 finallySystem.out.println("process finished")。System.out.println("program c
19、ontinued")。四、程序設(shè)計(jì)(40分)1.設(shè)計(jì)并編寫(xiě)一個(gè)名為MyPoint的類(lèi)表示平面上一個(gè)具有x坐標(biāo)和y坐標(biāo)的點(diǎn),完成以下要求:(15分)將該類(lèi)放置于包 prog01中;兩個(gè)double類(lèi)型數(shù)據(jù)域x和y表示坐標(biāo),并進(jìn)行封裝;無(wú)參構(gòu)造方法創(chuàng)建點(diǎn)(0.0, 0.0)。有參構(gòu)造方法按指定坐標(biāo)創(chuàng)建一個(gè)點(diǎn);方法distance返回當(dāng)前點(diǎn)對(duì)象到參數(shù)點(diǎn)對(duì)象之間的距離;編寫(xiě)測(cè)t后ITestMyPoint ,其 main方法中創(chuàng)建兩個(gè)點(diǎn) (0.0, 0.0) 和(10.0,35.5),輸出這兩個(gè)點(diǎn)之間的距離。2 .根據(jù)材料完成程序代碼(15分)。要求利用多態(tài)性計(jì)算若干不同類(lèi)型幾何圖形的面積之和
20、,類(lèi)與接口的關(guān)系見(jiàn)下面的類(lèi) 圖。類(lèi)CricleVI ,類(lèi)RectangleVI和測(cè)試類(lèi)Tester的源碼已經(jīng)給出。CrkleVl< interface >>CalcAreaRectangleVICircleVl.javapublic class CircleVI private double radius 。 public CircleV1() this(1.0)。 public CircleV1(double radius) this.radius = radius。 public double getRadius() return radius。 public void
21、setRadius(double radius) this.radius = radius。 RectangleV1.javapublic class RectangleV1 private double width 。 private double height 。public RectangleV1() this(1.0, 1.0)。 public RectangleV1(double width, double height) this.width = width 。 this.height = height 。public double getWidth() return width。
22、 public void setWidth(double width) this.width = width。 public double getHeight() return height。 public void setHeight(double height) this.height = height。 Tester.javapublic class Tester public static void main(String口 args) Object口 shapes = new CircleV2(10), / new RectangleV2(10, 2), / new CircleV2
23、(), / new RectangleV2() / °System.out.println(sumArea(shapes)。 public static double sumArea(Object口 shapes) double result = 0。for (int i = 0。i < shapes.length 。 i+) if (shapesi instanceof CalcArea) /result += (CalcArea) shapesi).getArea()。 /return result 。請(qǐng)根據(jù)以上給定的材料,完成以下代碼,使的 Tester類(lèi)中的main方法能夠順利運(yùn)行。注意Tester類(lèi)中行尾有“ ”標(biāo)注的行使用了你需要完成的接口和類(lèi)。(1)編寫(xiě)完成
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)村私人房屋合同范例
- 東川區(qū)財(cái)務(wù)咨詢(xún)合同范本
- 南寧新房購(gòu)房合同范本
- 口腔設(shè)備售后合同范本
- 配送飯菜合同范本
- 醫(yī)院保安協(xié)議合同范例
- 廚房垃圾合同范本
- 單位房產(chǎn)贈(zèng)與合同范本
- 雙方平等供應(yīng)合同范本
- 上海學(xué)區(qū)房出租合同范本
- 電子書(shū) -品牌設(shè)計(jì)法則
- 2021版勞動(dòng)實(shí)踐河北科學(xué)技術(shù)出版社二年級(jí)下冊(cè)超輕黏土創(chuàng)意多教案
- 中考復(fù)習(xí)物理力學(xué)部分綜合試題(人教版含答案)
- BCP業(yè)務(wù)連續(xù)性管理手冊(cè)
- 2024年湖南鐵路科技職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)及答案解析word版
- 2024年中考英語(yǔ)第一次模擬試卷-(廣州卷)(全解全析)
- 使用農(nóng)產(chǎn)品承諾函
- 分式方程說(shuō)課王彥娥
- 2023配電網(wǎng)施工典型工藝
- 少兒美術(shù)幼兒園課件- 4-6歲 《沙漠鴕鳥(niǎo)》
- ChatGPT人工智能與通用大模型演講稿
評(píng)論
0/150
提交評(píng)論