Java語言程式設計課程實驗題目 第五次實驗
1. 在IDE中輸入並觀察以下程式碼,分析該段程式的作用。
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject(){
dateCreated = new java.util.Date();
}
protected GeometricObject(String colr, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public String toString(){
return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
2. 編寫程式。
使用Java API中提供的Comparable介面,設計和實現名為ComparableMatrix的類,使ComparableMatrix類的物件之間存在可比性,比較規則如下:
(1) 給定參與比較的矩陣(兩個物件分別標識為A和B),如果矩陣行列數不相同,則比較兩個矩陣所包含的元素個數,元素個數較多的矩陣(如果為A)通過比較被認定為“Larger coverage”,即A.compareTo(B)返回1;同理,在個數相同的情況下返回0(Equal coverage),否則返回-1(Smaller coverage);
(2) 如果矩陣行列數相同,則依次比較對應位置上的元素值,當一個矩陣(如果為A)中所含的較大元素個數超過矩陣元素總數的一半,則認定該矩陣為“Larger grid”,即A.compareTo(B)返回1;如果參與比較的矩陣各對應位置上元素均相同,則認定“Equal grid”,返回0;此外,認定為“Less grid”,返回-1。
3. 編寫程式。
設計一個名為Colorable介面,其中有名為howToColor()的void方法。可著色物件的每個類必須實現Colorable介面。設計一個擴充套件GeometricObject類並實現Colorable介面的名為Square,Circle,Triangle的類,實現howToColor方法。例如針對Square,顯示訊息“Color all four sides”,對於其他的著色方法,請自行設計。
1、
package org.cust.test5;
public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;
protected GeometricObject() { dateCreated = new java.util.Date(); }
protected GeometricObject(String colr, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public boolean isFilled() { return filled; }
public void setFilled(boolean filled) { this.filled = filled; }
public java.util.Date getDateCreated() { return dateCreated; }
public String toString() { return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; }
public abstract double getArea();
public abstract double getPerimeter();
} /** * 這是一個抽象類帶有String屬性color、boolean型屬性filled、 * (util)Date型屬性dateCreated * 兩個構造方法 * 以及獲得各種屬性的方法 * 以及自帶toString方法輸出所有屬性 * 以及兩個抽象方法 需要子類繼承重寫該方法才能使用 */
2、
package org.cust.test5;
import java.util.InputMismatchException; import java.util.Scanner;
public class ComparableMatrix implements Comparable<ComparableMatrix> {
private int x; private int y; private int[][] matrix;
public ComparableMatrix(int x, int y) { this.x = x; this.y = y; this.matrix = new int[this.x][this.y]; this.matrix = this.matrixEnter(this.x, this.y); }
public int[][] matrixEnter(int x, int y) { int[][] matrix = new int[x][y]; Scanner in = new Scanner(System.in); System.out.println("物件矩陣初始化輸入:\n"); for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
try { matrix[i][j] = in.nextInt(); } catch (InputMismatchException e) { // TODO Auto-generated catch block in = new Scanner(System.in); System.out.println("請輸入整形數字"); j--; continue; } } } System.out.println("矩陣輸入完成"); // for (int i = 0; i < this.matrix.length; i++) { // System.out.println("矩陣的長度:"+this.matrix.length); // for (int j = 0; j < this.matrix[0].length; j++) { // System.out.println(this.matrix[i][j]); // } // } return matrix;
}
@Override public int compareTo(ComparableMatrix cm) { // TODO Auto-generated method stub if (this.x != cm.x || this.y != cm.y) { if (this.x * this.y > cm.x * cm.y) { return 1; } else if (this.x * this.y < cm.x * cm.y) { return -1; } else { return 0; } } else { int sum = this.x * this.y, overCount = 0, equalCount = 0; for (int i = 0; i < this.matrix.length; i++) { for (int j = 0; j < this.matrix[0].length; j++) { if (this.matrix[i][j] > cm.matrix[i][j]) { overCount++; } else if (this.matrix[i][j] == cm.matrix[i][j]) { equalCount++; // System.out.println("++"); } else {
} // System.out.println(this.matrix[i][j]); // System.out.println(cm.matrix[i][j]); } } // System.out.println("overCount:" + overCount); // System.out.println("sum/2:" + sum / 2); // System.out.println("sum:" + sum); // System.out.println("equalCount:" + equalCount); if (overCount > sum / 2) { return 1; } else if (equalCount == sum) { return 0;
} else { return -1; }
}
}
public static void main(String[] args) { ComparableMatrix a = new ComparableMatrix(2, 2); ComparableMatrix b = new ComparableMatrix(2, 2); System.out.println("The result of 'a copareTO a':" + a.compareTo(a)); System.out.println("The result of 'a copareTO b':" + a.compareTo(b));
}
}
3、
package org.cust.test5; public class Circle extends GeometricObject implements Colorable {
@Override public void howToColor() { // TODO Auto-generated method stub System.out.println("Color inside"); }
@Override public double getArea() { // TODO Auto-generated method stub return 0; }
@Override public double getPerimeter() { // TODO Auto-generated method stub return 0; }
}
package org.cust.test5;
public class Square extends GeometricObject implements Colorable {
@Override public void howToColor() { // TODO Auto-generated method stub System.out.println("Color all four sides"); }
@Override public double getArea() { // TODO Auto-generated method stub return 0; }
@Override public double getPerimeter() { // TODO Auto-generated method stub return 0; }
}
package org.cust.test5;
public class Triangle extends GeometricObject implements Colorable {
@Override public void howToColor() { // TODO Auto-generated method stub System.out.println("Color all three sides"); }
@Override public double getArea() { // TODO Auto-generated method stub return 0; }
@Override public double getPerimeter() { // TODO Auto-generated method stub return 0; }
}
package org.cust.test5;
public interface Colorable {
void howToColor();
}
package org.cust.test5;
public class Test { public static void main(String[] args) {
Square s = new Square(); Circle c = new Circle(); Triangle t = new Triangle(); s.howToColor(); c.howToColor(); t.howToColor();
} }