2017-10-16 84 views
2
package Comparar; 

import java.util.*; 

public class Exercici { 

    public void Exercici(){ 

    } 

    public static <E extends Comparable <E>> int numMinors(E e, Iterator<E> it){ 
     int numMenors = 0; 
     while(it.hasNext()){ 
      int comparacio = e.compareTo(it.next()); 
      if (comparacio == -1){ 
       numMenors += 1; 
      } 
     } 
     return numMenors; 
    } 

    public static <E extends Comparable <E>> int numMinors(Comparator<E> comp, E e, Iterator<E> it){ 
     int numMenors =0; 
     while (it.hasNext()){ 
      int comparacio = comp.compare(e, it.next()); 
      if(comparacio == -1){ 
       numMenors += 1; 
      } 
     } 
     return numMenors; 
    } 

} 

單元測試類:JUnit測試不承認我的功能(通用功能)

import java.util.*; 
import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import static org.junit.Assert.*; 

public class ExerciciTest { 

    public ExerciciTest() { 

    } 

    @Test 
    public void compararCosesComparables() { 
     int num = 0; 

     Bottle b1 = new Bottle(0.33, 10, "Agua de Oro", "Dubai"); 
     Bottle b2 = new Bottle(1.5, 1, "asdf", "Rabat"); 
     Bottle b3 = new Bottle(2.5, 1.5, "qwerty", "Londres"); 
     Bottle b4 = new Bottle(5, 2, "poiuy", "Paris"); 
     Bottle b5 = new Bottle(2.5, 2, "Botella", "EEUU"); 

     List<Bottle> alb = new ArrayList<>(); 
     Iterator<Bottle> it = alb.iterator(); 
     alb.add(b1); 
     alb.add(b2); 
     alb.add(b3); 
     alb.add(b4); 

     num = numMinors(b5, it); //Error cannot find symbol 

    } 

    @Test 
    public void compararCosesAmbComparador() { 
     int num = 0; 

     Bottle b1 = new Bottle(0.33, 10, "Gold water", "Dubai"); 
     Bottle b2 = new Bottle(1.5, 1, "asdf", "Rabat"); 
     Bottle b3 = new Bottle(2.5, 1.5, "qwerty", "Londres"); 
     Bottle b4 = new Bottle(5, 2, "poiuy", "Paris"); 
     Bottle b5 = new Bottle(2.5, 2, "Botella", "EEUU"); 

     ArrayList<Bottle> alb = new ArrayList<>(); 
     BottleComparator comp = new BottleComparator(); 
     Iterator<Bottle> it = alb.iterator(); 

     alb.add(b1); 
     alb.add(b2); 
     alb.add(b3); 
     alb.add(b4); 

     num = numMinors(comp, b5, it); //Error cannot find symbol 

    } 

} 

所以我得到了3個班,BottleComparator它實現了比較,瓶,它實現可比性和exercici這是包含我被要求做的方法的類。

問題是測試類似乎不能識別我的函數numMinors,我已經嘗試將所有文​​檔放在另一個包中,並關閉項目並再次打開它。任何線索?

(我仍然需要投入對測試斷言功能)

錯誤消息:

找不到符號

符號:方法numMinors(......)

位置:類exerciciTest

回答

0

numMinors不是您的課程ExerciciTest的靜態方法。這是您的班級Exercici的靜態方法。

所以你可以做Exercici.numMinors(...)但不是ExerciciTest.numMinors(...)

在這裏您可以直接在ExerciciTest類中致電numMinors(...)。由於沒有靜態方法,因此出現錯誤numMinorsExerciciTest