2014-09-03 38 views
-1

我正在研究一個程序,它會將隨機數添加到數組中,直到它將一個重複數字放到數組中,然後它應該打印生成重複數字之前生成了多少數字。爲什麼我收到此運行時異常?

當我運行該程序時,我收到此錯誤。

Exception in thread "main" java.lang.ExceptionInInitializerError 
    at arrayintlog.TestLuck.main(TestLuck.java:25) 
Caused by: java.lang.RuntimeException: Uncompilable source code - arrayintlog.ArrayIntLog is not abstract and does not override abstract method contains(java.lang.String) in arrayintlog.IntLogInterface 
    at arrayintlog.ArrayIntLog.<clinit>(ArrayIntLog.java:6) 
    ... 1 more 
Java Result: 1 

我不知道這個例外的含義。我以前只做過一個接口,而且幾乎完全一樣,但是有一個字符串數組,我沒有問題。

有了這一個NetBeans不斷告訴我將抽象添加到我的ArrayIntLog類,但如果我這樣做,我的ArrayIntLog構造函數不能在我的主類中工作?

我在做什麼錯誤/缺少這個?

這裏是我的主類

package arrayintlog; 

import java.util.Random; 
import java.util.Scanner; 

public class TestLuck 
{ 

    public static void main(String[] args) 
    { 
     int cycles = 0; 
     String name; 
     int min = 1; 
     int max = 10000; 
     int duplicate; 

     Random rand = new Random(); 
     int random = rand.nextInt(max - min + 1) + min; 

     Scanner in = new Scanner(System.in); 
     System.out.println("What is the name of your Log: "); 
     name = in.next(); 


     ArrayIntLog myLog = new ArrayIntLog(name); 

     for (int index = 0; index <= myLog.size(); index++) 
     { 
      myLog.insert(12); 
      int duplicateCheck = log[index]; 

      if (myLog.contains(duplicateCheck)) 
      { 
       myLog.toString(); 
       System.out.println("It took " + cycles + " cycles to generate duplicate numbers randomly."); 
      } 
      else 
      { 
       cycles++; 
      } 
     }  

    } 

} 

這裏是我的數組INT日誌類:

package arrayintlog; 


public class ArrayIntLog implements IntLogInterface 
{ 
    protected String name; //name of the IntLog 
    protected int[] log; //array that holds the integers 
    protected int lastIndex = -1; 

    //==========================Constructor===================================== 
    public ArrayIntLog(String name, int maxSize) 
    { 
     log = new int[maxSize]; 
     this.name = name; 
    } 

    //==========================Constructor===================================== 
    public ArrayIntLog(String name) 
    { 
     log = new int[100]; 
     this.name = name; 
    } 

    //===========================Insert========================================= 
    public void insert(int element) 
    { 
     lastIndex++; 
     log[lastIndex] = element; 
    } 

    //===========================isFull========================================= 
    public boolean isFull() 
    { 
     if(lastIndex == (log.length - 1)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    //============================Size========================================== 
    public int size() 
    { 
     return lastIndex + 1; 
    } 

    //===========================Contains======================================= 
    public boolean contains(int element) 
    { 
     int location = 0; 
     while (location <= lastIndex) 
     { 
      if (element == (log[location])) 
      { 
       return true; 
      } 
      else 
      { 
       location++; 
      } 
     } 
     return false; 
    } 

    //=============================Clear======================================== 
    public void clear() 
    { 
     for (int index = 0; index <= lastIndex; index++) 
     { 
      log[index] = null; 
     } 
     lastIndex = -1; 
    } 

    //=============================getName====================================== 
    public String getName() 
    { 
     return name; 
    } 

    public String toString() 
    { 
     String logString = "Log " + name +"/n/n"; 

     for (int index = 0; index <= lastIndex; index++) 
     { 
      logString = logString + (index+1) + ". " + 
        log[index] + "/n"; 
     } 
     return logString; 
    } 

} 

回答

2

的問題是,IntLogInterface接口(你沒有發佈)包含至少另一種方法(contains(String)),你還沒有在ArrayIntLog班。

你的IDE不斷告訴你的abstract關鍵字添加到您的ArrayIntLog類,因爲一個類必須要麼落實實現的接口繼承或定義的所有abstract方法或類本身必須聲明abstract離開貫徹「失蹤」的方法爲子類。

很明顯,直到「缺失」方法沒有實現,該類不能實例化,因此如果它是abstract,您不能實例化ArrayIntLog

只需實現IntLogInterface定義的所有方法。

+0

好吧,我必須無意中在我的界面中使用了包含方法String而不是int。我讚賞完整的解釋。對不起,我沒有發佈界面開始。我確信這不能包含這個問題......生活和學習 – twjohns29 2014-09-03 13:20:51

0

您應該實現IntLogInterface接口的所有功能

0

這個例外是不言自明的。您必須在ArrayIntLog類中實施contains(String element)。當你實現interface時,你需要實現該接口中的所有方法。

0

您必須實現IntLogInterface中的所有抽象方法。

它說「arrayintlog.ArrayIntLog不是抽象的,也不會覆蓋抽象方法contains(java.lang.String)」。但是你實施的contains方法有不同的簽名:public boolean contains(int element)

0

錯誤是不言自明的。

arrayintlog.ArrayIntLog is not abstract and does not override abstract method contains(java.lang.String) in arrayintlog.IntLogInterface 

如果您使用的是接口,請不要忘記實現它的所有方法。 這樣做,你很好去。

相關問題