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;
}
3 일 동안 사용해 본 적이 있다면 무엇을 시도 했습니까? –
정보가 충분하지 않습니다. 테스트 실행 방법, 객체 초기화 방법 등을 표시하지 마십시오. –
코드를 단계별로 (런타임에) 보여줍니다. 매번 작동합니다. – keyser