以下にコードのブロックを含めましたが、何が間違っているのか理解できないようです。このコードは、いくつかの明示的なパラメーターを含む仮想ガソリン スタンド オブジェクトを配列リストに入力する、私が書いているプログラムの一部です。そのうちの 1 つは、ガソリン スタンドの名前である文字列パラメーターである「説明」です。問題のメソッド findStation() は、ガソリン スタンドの名前に設定された明示的なパラメーターを受け取ります。次に、これを使用して配列リストを検索し、toMatch 明示的パラメーターに一致するガソリン スタンドの説明を探します (正しく機能することがわかっている、以前に宣言された getDescription() メソッドを使用)。一致が見つかった場合は、見つかった整数位置が返されます。
コードを適切に記述したように感じますが、Junit テストを実行しようとすると、「<6> が必要ですが、<-1> が返されました」というエラーが表示されます。メソッドの構文またはメソッドのテスト方法のいずれかにすぐに問題があることに気付く人はいますか? 素晴らしい人々からのすべての助けに前もって感謝します!
ちなみに、問題の特定の方法を理解することに関連するすべてを含めようとしました. 他に何か必要な場合はお知らせください。
問題の方法: リストの "i" 位置にある情報を保持する一時的な Station オブジェクトを作成します。次に、取得したオブジェクトの説明を保持する一時的な String を作成します。説明が入力された明示的なパラメーターと一致する場合、それが見つかったインデックス番号を返すようにします。私の定数変数 NO_MATCH は "-1" に等しく設定されているため、ガソリン スタンドの説明は明らかに存在するのに、存在しないことがテストでわかります。
public int findStation(String toMatch)
{
//returns the index where a match is found
for (int i = 0; i < stations.size(); i++)
{
Station tmpStation = stations.get(i);
String tmpDescription = tmpStation.getDescription();
if (tmpDescription.equals(toMatch))
{
return i;
}
}
return NO_MATCH;
}
また、ステーションの追加に使用される addStation() メソッド。
public boolean addStation(double inLatitude, double inLongitude,
String inDescription, double inPrice, String inFuelType)
{
// Be sure inDescription is not a description in the collection
if (this.findStation(inDescription) == NO_MATCH)
{
Station tmpStation = new Station();
stations.add(tmpStation);
return true;
}
else return false;
}
setup() を含む Junit テストで、私が何を扱っているかがわかります。
protected void setUp()
{
// collection for testing
collection2 = new StationCollection();
// add stations to the collection
this.collection2.addStation(39.933611, -82.4725,
"Snake on the Lake", 2.99, "B80");
this.collection2.addStation(39.9621, -83.0005,
"Snake Central", 2.25, "E45");
this.collection2.addStation(39.94, -82.48,
"Snake Brothers", 2.27, "E45");
this.collection2.addStation(39.8614, -82.8916,
"Anna Conda Oil", 2.71, "PROPANE");
this.collection2.addStation(39.8614, -82.8916,
"Anna Conda Oil - II", 2.27, "E45");
this.collection2.addStation(39.9060, -82.7562,
"Tiger Snake", 2.31, "E40");
this.collection2.addStation(39.9611, -82.9988,
"Rattler Ray's", 2.15, "E84");
this.collection2.addStation(40.011792, -82.973196,
"Water Adder Oil", 3.20, "B80");
this.collection2.addStation(40.011792, -82.974,
"Water Adder Oil - II", 2.31, "E40");
}
public void testFindStation()
{
// Create an empty collection
StationCollection collection1 = new StationCollection();
// Attempt to find a station in the empty collection
assertEquals(StationCollection.NO_MATCH, collection1.findStation("Rattler Ray's"));
// Use non-empty collection2
// Look for position of matching station
assertEquals(6, collection2.findStation("Rattler Ray's"));
// Check that it detects a non-matching station
assertEquals(StationCollection.NO_MATCH, collection2.findStation("Sam Snake's"));
}