面向?qū)ο蠹夹g(shù)Java期末復(fù)習(xí)試卷(五)_第1頁
面向?qū)ο蠹夹g(shù)Java期末復(fù)習(xí)試卷(五)_第2頁
面向?qū)ο蠹夹g(shù)Java期末復(fù)習(xí)試卷(五)_第3頁
面向?qū)ο蠹夹g(shù)Java期末復(fù)習(xí)試卷(五)_第4頁
面向?qū)ο蠹夹g(shù)Java期末復(fù)習(xí)試卷(五)_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、文檔供參考,可復(fù)制、編制,期待您的好評與關(guān)注! 期末復(fù)習(xí)試卷(五)一、 程序題1、請根據(jù)UML圖,代碼實(shí)現(xiàn)Person類、Student類和Volunteer接口的定義。解題要求:編寫測試程序,創(chuàng)建一個名為”Tom”的一年級學(xué)生,這名學(xué)生參加了“北京青年志愿者協(xié)會”的活動。輸出該學(xué)生的姓名、年級及所屬社團(tuán)的信息。效果如下:interface Volunteer  public abstract String volunteer(String organization);class Person  private String name; 

2、60; public Person()     public Person(String name)     = name;     Override  public String toString()    return "姓名" + + "n"  class Student extend

3、s Person implements Volunteer  private int grade;   public static final int FRESHMAN = 1;  public static final int SOPHOMORE = 2;  public static final int JUNIOR = 3;  public static final int SENIOR = 4;    public Student(String n

4、ame, int grade)    super(name);    this.grade = grade;     Override  public String toString()    return super.toString() +           "年級" +

5、this.grade + "n"     Override  public String volunteer(String organization)    return organization;  public class Test  public static void main(String args)    Student student = new Student("

6、;Tom",1);        System.out.println(student + "所屬社團(tuán)" + student.volunteer("北京青年志愿者協(xié)會") + "n");   2、根據(jù)給定的GeometricObject類層次圖,分別實(shí)現(xiàn)GeometricObject、Circle和Triangle類。要求:(1) 按照類圖,定義各類的數(shù)據(jù)域?qū)傩院头椒?。?) 默認(rèn)的GeometricObject的顏

7、色為white。默認(rèn)的Circle對象的半徑為10。默認(rèn)的Triangle對象的三邊為(3,4,5)。(3) GeometricObject類的toString()方法,返回對象的顏色信息。Circle和Triangle的toString()方法,覆蓋父類的toString(),分別返回包括顏色信息、半徑或邊長的信息。(4) GeometricObject類實(shí)現(xiàn)java.lang.Comparable接口,覆蓋public int compareTo()方法,實(shí)現(xiàn)兩個幾何對象在周長上的比較。(5) Circle和Triangle類分別覆蓋實(shí)現(xiàn)getPerimeter()。編寫一個用戶程序:(1

8、)創(chuàng)建一個幾何對象數(shù)組object;(2)分別存放一個默認(rèn)的圓、一個半徑為20的圓、一個默認(rèn)的三角形和一個三邊為(10,20,15)的三角形;(3)計(jì)算這一組幾何對象的總周長并輸出;(4)使用toString(),輸出每個幾何對象的相關(guān)信息;(5)使用java.util.Arrays.sort()方法,對幾何對象數(shù)組中的幾何圖形,按周長進(jìn)行排序,并輸出排序結(jié)果。解題要求:寫出程序代碼。定義一個Java源文件,名為Test.java,其中包括:用戶類Test、GeometricObject類、Circle類和Triangle類的定義。abstract class GeometricObject

9、implements Comparable<geometricobject> private String color = "white" protected GeometricObject() protected GeometricObject(String color) this.color = color; public String getColor() return color; public void setColor(String color) this.color = color; Override public int compareTo(Ge

10、ometricObject o) if (this.getPerimeter() > o.getPerimeter() return 1; else if (this.getPerimeter() < o.getPerimeter() return -1; else return 0; public String toString() return "顏色" + color + "n" public abstract double getPerimeter();class Circle extends GeometricObject priv

11、ate double radius; public Circle() this.radius = 10.0; public Circle(double radius) this.radius = radius; public Circle(String color, double radius) super(color); this.radius = radius; public double getRadius() return radius; public void setRadius(double radius) this.radius = radius; Override public

12、 double getPerimeter() return 2 * radius * Math.PI; public String toString() return super.toString() + "圓的半徑是" + radius + "n" class Triangle extends GeometricObject private double side1; private double side2; private double side3; public Triangle() this.side1 = 3.0; this.side2 =

13、4.0; this.side3 = 5.0; public Triangle(double side1 ,double side2, double side3) this.side1 = side1; this.side2 = side2; this.side3 = side3; public Triangle(String color,double side1 ,double side2, double side3) super(color); this.side1 = side1; this.side2 = side2; this.side3 = side3; Override publi

14、c double getPerimeter() return side1 + side2 + side3; Override public String toString() return super.toString() + "三角形的三條邊是" + side1 + " " + side2 + " " + side3 + "n" public class Test public static void main(String args) /創(chuàng)建對象數(shù)組 GeometricObject object = new C

15、ircle(), new Circle(20), new Triangle(), new Triangle(10,20,15); double totalPerimeter = 0; /自己學(xué)習(xí)一下這種for循環(huán)的使用方法 for(GeometricObject go:object) totalPerimeter += go.getPerimeter(); System.out.println("The total perimeter is " + totalPerimeter); for(GeometricObject go:object) System.out.prin

16、tln(go.toString(); /*抽象類GeometricObject實(shí)現(xiàn)了Comparable接口之后,所有子類(Circle和Triangle)的對象 就可以比較大小了,由此也就可以調(diào)用Arrays的sort方法對上面的對象數(shù)組排序了。 */ java.util.Arrays.sort(object); for(int i = 0; i < object.length; i+) System.out.println("The " + i + "th short perimeter is " + objecti.getPerimeter(

17、); 3、根據(jù)UML圖,實(shí)現(xiàn)Person類、Faculty類和Volunteer接口的定義。解題要求:編寫用戶測試程序,創(chuàng)建一個名為“Jerry”的副教授, 他參加了“北京市慈善義工協(xié)會”的活動,輸出其姓名、職稱和所屬社團(tuán)信息。效果如下:class Person private String name; public Person() public Person(String name) = name; Override public String toString() return "姓名" + + "n" &#

18、160;interface Volunteer     public abstract String volunteer(String organization); class Faculty extends Person implements Volunteer private String title; public static final String TEACHING_ASSISTANT = "助教" public static final String LECTURER = "講師"

19、 public static final String ASSOCIATE_PROFESSOR = "副教授" public static final String PROFESSOR = "教授" public Faculty(String name,String title) super(name); this.title = title; Override public String toString() return super.toString() + "職稱" + this.title + "n" Ov

20、erride public String volunteer(String organization) return organization; public class TestPerson public static void main(String args) Faculty faculty = new Faculty("Jerry","副教授"); System.out.println(faculty + "所屬社團(tuán)" + faculty.volunteer("北京市慈善義工協(xié)會"); 4、請根據(jù)UML圖,

21、給出類Person 、類Student、Faculty類和接口Volunteer的定義實(shí)現(xiàn)。解題要求:編寫測試程序,創(chuàng)建一個名為“Tom”的一年級學(xué)生、“北京青年志愿者協(xié)會”成員,創(chuàng)建一個名為“JERRY”的助教、“北京市慈善義工協(xié)會”成員,輸出兩個人的姓名、年級和所屬志愿者協(xié)會的基本信息。效果如下:interface Volunteer  public abstract String volunteer(String organization);class Person private String name; public Person() public Person(

22、String name) = name; Override public String toString() return "姓名" + + "n" class Student extends Person implements Volunteer private int grade; public static final int FRESHMAN = 1; public static final int SOPHOMORE = 2; public static final int JUNIOR = 3; pub

23、lic static final int SENIOR = 4; public Student(String name, int grade) super(name); this.grade = grade; Override public String toString() return super.toString() + "年級" + this.grade + "n" Override public String volunteer(String organization) return organization; class Faculty extends Person implements Volunteer private String title; public static final String TEACHING_ASSISTANT = "助教" public static final

溫馨提示

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

評論

0/150

提交評論