2012-04-10 74 views
0

我已經給了一個assigment,它是它自己的最後一天。我做了大部分,但最後一個問題,我創建一個compareTo()函數有問題。Java compareTo(Object obj)

以下是我們想要的東西;

的compareTo

公衆詮釋的compareTo(java.lang.Object繼承等)

Specified by: 
    compareTo in interface java.lang.Comparable 

這裏是我做過什麼

public int compareTo(Object obj) 
{ 
Document tmp = (Document)obj; 
if(this.text < tmp.text) 
{ 
/* instance lt received */ 
return -1; 
} 
else if(this.text > tmp.text) 
{ 
/* instance gt received */ 
return 1; 
} 
/* instance == received */ 
return 0; 
} 

這裏是我的整個Document.java文件

class Document { private Str文本;

/** 
* Default constructor; Initialize amount to zero. 
*/ 
public Document() 
{ 
    text = ""; 
} 

/** 
* Default constructor; Initialize text to input value 
* @param text New text value 
*/ 
public Document(String text) 
{ 
    this.text = text; 
} 

/** 
* Returns as a string the contents of the Document. 
*/ 
public String toString() 
{ 
    return text; 
} 

這裏是它自己的測試文件。

import java.util.Arrays;

公共類Homework2擴展文件

/** ====================== 
* ContainsKeyword 
* Returns true if the Document 
* object passed in contains keyword as a substring 
* of its text property. 
* ====================== 
*/ 
public static boolean ContainsKeyword(Document docObject, String keyword) 
{ 
    if (docObject.toString().indexOf(keyword,0) >= 0) 
     return true; 
    return false; 
} 


public static void main(String[] args){ 

    Email email1= new Email("Programming in Java", 
      "Larry", "Curly", "Programming"); 
    Email email2 = new Email("Running marathons", 
      "Speedy", "Gonzales", "races"); 

    System.out.println(email1); 

    File file1 = new File("Some Java file", "file.txt"); 
    File file2 = new File(
      "Boluspor wins against Besiktas. Muahahahaha", 
    "bolutas.txt"); 

    Document doc = new Document (
    "ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?"); 

    System.out.println("\n"+file1); 

    System.out.println("\nWhich contains Java?"); 
    if (ContainsKeyword(email1,"Java")) System.out.println(" Email1"); 
    if (ContainsKeyword(email2,"Java")) System.out.println(" Email2"); 
    if (ContainsKeyword(file1,"Java")) System.out.println(" File1"); 
    if (ContainsKeyword(file2,"Java")) System.out.println(" File2"); 

    Document [] da = new Document [5]; 
    da[0] = email1; 
    da[1] = email2; 
    da[2] = file1; 
    da[3] = file2; 
    da[4] = doc; 
    Arrays.sort(da); 
    System.out.println("\nAfter sort:"); 
    for(Document d : da){ 
     System.out.println(d); 
    } 
} 
} 

我想問的是,我不能對象從我Email.java和File.java比較什麼,我可以做任何事情,但最後一部分開始{使用Document [] da ...該部分發生錯誤。我在這裏做錯了什麼?

錯誤是;

Exception in thread "main" java.lang.ClassCastException: Email cannot be cast to java.lang.Comparable 
at java.util.Arrays.mergeSort(Arrays.java:1144) 
at java.util.Arrays.sort(Arrays.java:1079) 
at Homework2.main(Homework2.java:53) 

UPLOAD **這裏是我的電子郵件和文件類..

/** 
* First define class for Email, derive from Document 
*/ 
class Email extends Document 
{ 
    private String sender; 
    private String recipient; 
    private String title; 
    private String body; 

    /** 
    * Constructors 
    */ 
    public Email() 
    { 
     super(); 
     sender = ""; 
     recipient = ""; 
     title = ""; 
     body = ""; 
    } 

    public Email(String body, String sender, String recipient, String title) 
    { 

     this.sender = sender; 
     this.recipient = recipient; 
     this.title = title; 
     this.body = body; 
    } 

    // ====================== 
    // Various accessor and mutator methods 
    // ====================== 
    public String getSender() 
    { 
     return sender; 
    } 

    public void setSender(String sender) 
    { 
     this.sender = sender; 
    } 

    public String getRecipient() 
    { 
     return recipient; 
    } 

    public void setRecipient(String recipient) 
    { 
     this.recipient = recipient; 
    } 

    public String getTitle() 
    { 
     return title; 
    } 

    public void setTitle(String title) 
    { 
     this.title = title; 
    } 
    public String getBody(){ 
     return body; 
    } 
    public void getBody(String body){ 
     this.body = body; 
    } 

    /** 
    * Returns as a string the contents of the text fields concatenated 
    * together. Uses super.toString to get the parent's text. 
    */ 
    public String toString() 
    { 
return "Sender:" + sender + ",Recipient:" + recipient + ",Title:" + title + ",Body:" + body + " " + 
super.toString(); 
    } 
} // Email 

和文件;

/** 
* Next define class for File, derive from Document 
* For brevity, short one-line methods are defined here in the 
* header. 
*/ 
class File extends Document 
{ 
private String pathname; 

/** 
* Constructors. 
*/ 
public File() 
{ 
    super(); 
    pathname = ""; 
} 

public File(String body, String pathname) 
{ 
    super(body); 
    this.pathname = pathname; 
} 

// ====================== 
// Various accessor and mutator methods 
// ====================== 
public void setPathname(String s) 
{ 
    pathname = s; 
} 

public String getPathname() 
{ 
    return pathname; 
} 

/** 
* Returns as a string the contents of the text fields concatenated 
* together. Uses super.toString to get the parent's text. 
*/ 
public String toString() 
{ 
    return "Pathname " + pathname + " Body " + super.toString(); 
} 

} //文件

+0

這是功課? – Aaron 2012-04-10 16:27:22

+0

分配給我,其中有10個問題,並給出1.5周。它是我堅持的最後一個問題的一部分。 – 2012-04-10 16:28:04

+3

您還沒有向我們展示任何有關您的電子郵件或文件類的信息,也沒有向我們展示您的compareTo方法的實施位置。爲什麼你的Homework2類擴展Document? – 2012-04-10 16:28:42

回答

0

你所做錯了,是錯誤消息。

只能對實現Comparable的類進行排序。你的班級沒有。

當您對多種不同類型進行排序時,您可能想要提供自定義比較器,或者使文檔實現爲Comparable。

2

你把compareTo方法放在哪裏?如果你想進行排序Document秒的數組,你需要有文件實現可比(或傳遞一個比較):

public class Document implements Comparable<Document> { 

或者,如果一些奇怪的原因,你不能使用泛型:

public class Document implements Comparable { 

然後把compareToDocument

1

它是失敗的確切原因是因爲你試圖調用Arrays.sort上沒有實現可比

實現可比一類允許

calling Collections.sort and Collections.binarySearch 
    calling Arrays.sort and Arrays.binarySearch 
    using objects as keys in a TreeMap 
    using objects as elements in a TreeSet 

電子郵件沒有實現可比接口

使用

public class Email implements Comparable<Email> 

讀這做你幫個忙http://www.javapractices.com/topic/TopicAction.do?Id=10

另外需要注意的是,你說你要比較

Email.java和File.java

您將需要一個基於邏輯的自定義功能。

compareTo用於比較相同類型的兩個實例。這也意味着該函數住在電子郵件類

Email myEmail = new Email(); 
Email hisEmail = new Email(); 

myEmail.compareTo(hisEmail); 
0

試試這個:

這裏是我的整個Document.java文件

class Document implements Comparable { //this line is your solution. 
private String text; 

/** 
* Default constructor; Initialize amount to zero. 
*/ 
public Document() 
{ 
    text = ""; 
} 

/** 
* Default constructor; Initialize text to input value 
* @param text New text value 
*/ 
....} 
+0

我嘗試了兩種解決方案,但仍無法對給定輸入進行排序:/ 更改了所需的內容。使用Comparable擴展它,但是我是否還需要爲電子郵件和文件創建另一種方法? – 2012-04-10 16:53:43

+0

1)你可以比較只有可比較的東西,所以是的,你需要實現的電子郵件和文件。 2),因爲你電子郵件和文件擴展文件是compareable,電子郵件和文件也是相當的,只要你爲它提供「文本」(我的意思是你應該調用super(「someInfo」)) 所以在您的電子郵件構造函數(這一個:公共電子郵件(字符串正文,字符串發件人,字符串收件人,字符串標題))你忘記調用超級(標題) – guness 2012-04-10 17:38:29

相關問題