Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA註解

interface ICanCompare {

int compare(ICanCompare i); // 進行比較

}

class Sort { // 提供排序功能的類別

static void bubbleSort(ICanCompare[] objs) { // 氣泡排序法

for(int i = objs.length - 1;i > 0;i--) {

for(int j = 0;j < i;j++) {

if(objs[j].compare(objs[j + 1]) < 0) {

ICanCompare temp = objs[j];

objs[j] = objs[j + 1];

objs[j + 1] = temp;

}

}

}

}

}

abstract class people implements ICanCompare { // 父類別

abstract double hight(); // 計算面積

public int compare(ICanCompare i) { // 實作compare

people l = (people) i;

return (int)(this.hight() - l.hight()); // 依據面積比較大小

}

}

class Person extends people {

String names;

double hight;

Person (String n, double h){

names = n;

hight = h;

}

double hight() { // 多重定義的版本

return hight;

}

public String toString() {

return "姓名:" + names + "\t身高:" + hight();

}

}

public class sorting {

public static void main(String[] argv) {

Person[] people = {

new Person("mary",165),

new Person("john",180),

new Person("terry",170),

new Person("nancy",150),

new Person("anna",173)

};

for(Person l : people) {

System.out.println(l);

}

Sort.bubbleSort(people);

System.out.println("排序後...");

for(Person l : people) {

System.out.println(l);

}

}

}

---------------------------------------------------------------------------------------

幫忙寫出每一行是做什麼的^^THX

3 Answers

Rating
  • 1 decade ago
    Favorite Answer

    interface ICanCompare { // 介面

    int compare(ICanCompare i); // 進行比較

    }

    class Sort { // 提供排序功能的類別

    static void bubbleSort(ICanCompare[] objs) { // 氣泡排序法

    // 依據平均數排行排序索引值

    for (int i = objs.length - 1; i &gt; 0; i--) {

    for (int j = 0; j &lt; i; j++) {

    // 若該值小於下個值則進行排序陣列交換索引值

    if (objs[j].compare(objs[j + 1]) &lt; 0) {

    // 給他一個記憶體空間Tmep

    ICanCompare temp = objs[j];

    // 就是把它丟到陣列的下一個位置)

    objs[j] = objs[j + 1];

    // 之後再把下一個位置丟給temp便完成排序動作

    objs[j + 1] = temp;

    //

    }

    }

    }

    }

    }

    // 裝備介面以及進入點

    abstract class people implements ICanCompare { // 父類別

    abstract double hight(); // 計算身高

    public int compare(ICanCompare i) { // 實作compare

    people l = (people) i;// 傳入參數

    return (int) (this.hight() - l.hight()); // 依據身高比較大小

    }

    }

    //

    class Person extends people {

    String names;// 定義欄位

    double hight;// 定義欄位

    Person(String n, double h) {// 定義參數

    names = n;// 定義欄位n

    hight = h;// 定義欄位h

    }

    double hight() { // 多重定義的版本

    return hight;// 回傳hight參數

    }

    public String toString() {// 程式進入點

    return &quot;姓名:&quot; + names + &quot;&#92;t身高:&quot; + hight();// 回傳名稱即hight參數

    }

    }

    // 程式進入點

    public class Test1 {

    public static void main(String[] argv) {

    // 產生陣列並紀錄所有人的資料

    Person[] people = { new Person(&quot;mary&quot;, 165), new Person(&quot;john&quot;, 180),

    new Person(&quot;terry&quot;, 170), new Person(&quot;nancy&quot;, 150),

    new Person(&quot;anna&quot;, 173) };

    // 利用回圈印出結果

    for (Person l : people) {

    System.out.println(l);

    }

    // 呼叫Sort裡面的bubbleSort功能並印出排序後字樣

    Sort.bubbleSort(people);

    System.out.println(&quot;排序後...&quot;);

    // 利用回圈印出結果

    for (Person l : people) {

    System.out.println(l);

    }

    }

    }

    Source(s): 小弟淺見~給您當參考唷~
  • 1 decade ago

    interface ICanCompare { //ICanCompare 介面

    int compare(ICanCompare i); // 進行整數比較

    }

    class Sort { // 提供排序功能的類別

    static void bubbleSort(ICanCompare[] objs) { // 氣泡排序法

    //依序平均數索引值排序

    for (int i = objs.length - 1; i &gt; 0; i--) { //迴圈 i是否大於0,每次執行迴圈i-1

    for (int j = 0; j &lt; i; j++) { //迴圈 j是否小於i,每次執行迴圈i+1

    if (objs[j].compare(objs[j + 1]) &lt; 0) { //如果obj[j]比較[j+1]小於0

    ICanCompare temp = objs[j]; //objs[j]值丟给temp

    objs[j] = objs[j + 1]; //objs[j+1]值丟給[j]

    objs[j + 1] = temp; //temp值丟給 objs[j+1]

    }

    }

    }

    }

    }

    abstract class people implements ICanCompare { // 父類別

    abstract double hight(); // 計算身高

    public int compare(ICanCompare i) { // 實作比較

    people l = (people) i; //i值丟給 l

    return (int) (this.hight() - l.hight()); // 依據身高比較差

    }

    }

    class Person extends people { //繼承父類別

    String names; //宣告名字字串

    double hight;//宣告身高浮點數

    Person(String n, double h) { //定義名字字串,身高浮點數

    names = n; //定義names參數為n

    hight = h; //定義hight參數為h

    }

    double hight() { // 多重定義的版本

    return hight; //傳回身高值

    }

    public String toString() {

    return &quot;姓名:&quot; + names + &quot;&#92;t身高:&quot; + hight();

    //傳回排序結果

    }

    }

    public class sorting {

    public static void main(String[] argv) {

    Person[] people = { new Person(&quot;mary&quot;, 165), new Person(&quot;john&quot;, 180),

    new Person(&quot;terry&quot;, 170), new Person(&quot;nancy&quot;, 150),

    new Person(&quot;anna&quot;, 173) };

    //Person矩陣丟入矩陣people參數值

    for (Person l : people) { //把每個people值丟入person l

    System.out.println(l); //印出每個people l 值

    }

    Sort.bubbleSort(people); //排序people

    System.out.println(&quot;排序後...&quot;); //印出排序結果

    for (Person l : people) { //把每個people值丟入person l

    System.out.println(l); //印出每個people l 值

    }

    }

    }

  • 1 decade ago

    interface ICanCompare { //建立介面 ICanCompare

    int compare(ICanCompare i); // 進行比較

    }

    class Sort { // 提供排序功能的類別 Sort

    static void bubbleSort(ICanCompare[] objs) {

    //氣泡排序法

    for (int i = objs.length - 1; i &gt; 0; i--) {

    for (int j = 0; j &lt; i; j++) {

    //由實作方法:compare 得到 objs[j].compare(objs[j + 1] 之值

    if (objs[j].compare(objs[j + 1]) &lt; 0) {

    //將較矮的objs[j]置入 temp 內

    ICanCompare temp = objs[j];

    //交換objs[j+1] 與 objs[j] 位置

    objs[j] = objs[j + 1];

    //在將objs[j+1] 放回temp 作為下一次比較之標準,若沒有比temp高則不會再交換位置

    objs[j + 1] = temp;

    }

    }

    }

    }

    }

    abstract class people implements ICanCompare { // 父類別

    abstract double hight(); // 計算身高

    public int compare(ICanCompare i) { // 實作compare

    people l = (people) i;

    return (int) (this.hight() - l.hight()); // 依據身高差比較大小

    }

    }

    class Person extends people { //Person 繼承 people

    String names; //自訂names 欄位

    double hight; //自訂hight 欄位

    Person(String n, double h) {

    names = n; //定義 names 欄位參數為 n

    hight = h; //定義 hight 欄位參數為 h

    }

    double hight() { // 多重定義的版本

    return hight; // 回傳hight

    }

    public String toString() { //建立

    //回傳格式 姓名: + names + &#92;t身高:+ hight()

    return &quot;姓名:&quot; + names + &quot;&#92;t身高:&quot; + hight();

    }

    }

    public class sorting { //檔案名稱為: sorting

    public static void main(String[] argv) {

    //建立Person矩陣 命名為:people

    Person[] people = { new Person(&quot;mary&quot;, 165), //建立記憶體空間置入n = &quot;mary&quot; , h = 165

    new Person(&quot;john&quot;, 180), //建立記憶體空間置入n = &quot;john&quot; , h = 180

    new Person(&quot;terry&quot;, 170),//建立記憶體空間置入n = &quot;terry&quot; , h = 170

    new Person(&quot;nancy&quot;, 150),//建立記憶體空間置入n = &quot;nancy&quot; , h = 150

    new Person(&quot;anna&quot;, 173)};//建立記憶體空間置入n = &quot;anna&quot; , h = 173

    //(排序前) 將people 丟入物件 矩陣 l 再將 矩陣 l 逐一印出

    System.out.println(&quot;排序前...&quot;);

    for (Person l : people) {

    System.out.println(l);

    }

    //傳入people參數

    Sort.bubbleSort(people);

    System.out.println(&quot;排序後...&quot;); //印出執行方法:bubbleSort之結果

    //(排序後) 將people 丟入物件 矩陣 l 再將 矩陣 l 逐一印出

    for (Person l : people) {

    System.out.println(l);

    }

    }

    }

    Source(s): 愚昧的自己
Still have questions? Get your answers by asking now.