2014-09-03 40 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 

如果您使用的是接口,请不要忘记实现它的所有方法。 这样做,你很好去。

相关问题