-2

getStation と findStation がテストに失敗する理由を理解するのを手伝ってください。getStation と Expected:6 で null を取得しましたが、findStation で -1 を取得しました。私はそれを理解するために3日間試みてきました。私はすでに課題に遅れています。ありがとう!

編集:私は助けるためにすべてのコードを追加しました。まず、クラスのテスト ケースを作成する必要がありました。これも含めるべきですか?

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

1 に答える 1

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

リストにステーションをいくつか挿入します。

于 2013-08-05T23:21:43.097 に答える