有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要帮助找出我的代码测试失败的原因

请帮助我找出getStation和findStation测试失败的原因。对于getStation,我得到null,预期为:6,但是对于findStation,得到了-1。我已经试了三天想弄明白。我的作业已经迟到了。谢谢

编辑:我添加了所有代码以提供帮助。我必须首先为类创建测试用例。我应该也包括这个吗

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;
}

共 (1) 个答案

  1. # 1 楼答案

    你的电台列表是空的,因此一切都失败了

         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
    

    在列表中插入一些电台