2013-08-05 2 views
-2

Пожалуйста, помогите мне разобраться, почему getStation и findStation не проходят тесты. Я получаю null для getStation и Expected: 6, но получил -1 для findStation. Я пробовал уже три дня, чтобы понять это. Я уже опоздал на задание. Благодаря!Нужна помощь в выяснении, почему мой код не проходит тесты

Редактировать: я добавил весь код, чтобы помочь. Сначала мне нужно было создать тестовые примеры для класса. Должен ли я включить это также?

public class StationCollection 
{ 
// instance variables 
private ArrayList<Station> stations; 

// constants 
/** 
* constant indicating no matching station was found. 
*/  
public static final int NO_MATCH = -1; 

/** 
* constant indicating no such station. 
*/  
public static final Station NO_STATION = null; 

/** 
* Constructor for objects of class StationCollection. 
*/ 
public StationCollection() 
{ 
    // initialise instance variables 
    this.stations = new ArrayList<Station>(); 
} 

/** 
* Returns the number of stations in the collection. 
* 
* @return the number of stations in the collection. 
*/ 
public int getStationCount() 
{ 
    // REPLACE this comment & return statement with your code 

    return this.stations.size(); 
} 

/** 
* Returns the station at a position in the stations collection. 
* 
* @param pos  a position in the collection. 
* @return the station in the specified position; 
*   returns NO_STATION if position is not in bounds. 
*/ 
public Station getStation(int pos) 
{ 
    if (pos >= 0 && pos < getStationCount()) 
    { 
     return this.stations.get(pos); 
    } 
    else 

    return NO_STATION; 
} 

/** 
* Find the position in the collection of 
*  the Station with a given description. 
* 
* @param toMatch description of station. 
* @return the current position of the matching station; 
*   returns NO_MATCH if no match is found. 
*/ 
public int findStation(String toMatch) 
{ 
    // REPLACE this comment, HINTs & return statement with your code 
    // HINT: return the index where you find a match 
    for (int i = 1; i < getStationCount(); i++) 
    { 
     Station tmpStation = this.stations.get(i); 
     String tmp2 = tmpStation.getDescription(); 

     if (tmp2.equals(toMatch)) 
     { 
      return this.stations.indexOf(tmpStation); 
     } 

    } 
    return NO_MATCH; 
} 

/** 
* Add a station to the station collection 
*  (do not add stations whose description 
*  matches one in the collection). 
* 
* @param inLatitude  distance in degrees from equator. 
* @param inLongitude  distance in degrees from prime meridian. 
* @param inDescription description of station. 
* @param inPrice   price per gallon. 
* @param inFuelType  fuel type 
* 
* @return true if the station was added; 
*   returns false if description 
*   matches that of a station in the collection. 
*/ 
public boolean addStation(double inLatitude, double inLongitude, 
    String inDescription, double inPrice, String inFuelType) 
{ 
    // REPLACE this comment, HINTs & return statement with your code 
    // HINT: be sure inDescription is not a description in the collection 
    return false; 
} 

/** 
* Add a station to the station collection 
*  (do not add stations whose description 
*  matches one in the collection). 
* 
* @param inStation instance of station. 
* @return true if the station was added; 
*   returns false if description 
*   matches that of a station in the collection. 
*/ 
public boolean addStation(Station inStation) 
{ 
    // REPLACE this comment, HINTs & return statement with your code 
    // HINT: same as above, but you a Station parameter 
    // HINT: be sure to make a copy of inStation to add 
    return false;  
} 

/** 
* Remove a station from the station collection. 
* 
* @param toMatch description of station. 
* @return true if a matching station is found and removed; 
*   returns false if no match is found. 
*/ 
public boolean removeStation(String toMatch) 
{ 
    // REPLACE this comment, HINTs & return statement with your code 
    return false;  
} 

/** 
* Rate a station given it's description. 
*  (ignore ratings for stations that 
*  do not exist or out of range ratings). 
* 
* @param toMatch description of station. 
* @param rating of the station. 
*/ 
public void rateStationByName(String toMatch, int rating) 
{ 
    // REPLACE this comment & HINTs with your code 
    // HINT: use a method of Station to do the rating 
} 

/** 
* Filter the stations in the collection by category. 
* 
* @param category descriptor of the category. 
* @return a collection with only stations matching category. 
*/ 
public StationCollection filterStations(int category) 
{ 
    // REPLACE this comment, HINTs & return statement with your code 
    // HINT: create a new instance of StationCollection 
    // HINT: add to this new collection copies of those stations 
    // HINT: in this collection that are in the parameter category 
    return null; 
} 

/** 
* Sort the stations according to distance from a target location. 
* 
* @param target the target location. 
*/ 
public void sortByDistance(Location target) 
{ 
    // REPLACE this comment & HINTs with your code 
    // HINT: you may use any of the covered sort methods 
} 

/** 
* Find the closest station in the collection to the target. 
* 
* @param target the target location. 
* @return the closest station to the target; 
*   return NO_STATION if there no such stations. 
*/ 
public Station findClosest(Location target) 
{ 
    this.sortByDistance(target); 
    return this.getStation(0); 
} 

/** 
* Return a String listing stations in the collection. 
* 
* @return the list of collections in format specified 
*   in the write up of the lab. 
**/ 
public String toString() 
{ 
    String result = ""; 

    // add the station descriptor for each station in the collection 
    for (int i = 0; i < this.getStationCount(); i++) 
    { 
     result = result + 
      this.stations.get(i).toString() + "\n\n"; 
    } 

    return result; 
} 
+2

Если вы пробовали в течение 3 дней, что искали? –

+2

Недостаточная информация. Вы не показываете, как выполняете свои тесты, как вы инициализируете объект и т. Д. –

+0

Пройдите через свой код (во время выполнения). Работает каждый раз. – keyser

ответ

0

Ваш список станций пуст, следовательно, все, что не удается

 for (int i = 0; i < getStationCount(); i++) 
     //wont enter this loop since getStationCount() will return 0, i !<0 , so NO_MATCH 
    //and 

     if (pos >= 0 && pos < getStationCount()) 
     //will produce false since getStationCount() will return 0 ,so returns NO_STATION 

Вставьте несколько станций в списке.

+0

Спасибо всем! После некоторых ваших сообщений стало ясно. – user1807902

Смежные вопросы