0
我有三個類;包含我的主要方法的Movies/tester類。一個DVDCollection類,它對數組進行增量排序,並具有一個具有Constuctor方法並覆蓋compareTo的DVD Comparable Class。我們的導師要求我們使用電影/測試人員類的主要方法來搜索特定導演的收藏。我完全卡住了,因爲我認爲我需要傳遞一個Comparable Array和一個Comparable目標,但是指令說我只是傳遞一個String參數。我收到了無效的數據類型。任何人都在幫助有需要的學生。在binarySearch中傳遞參數
public class Movies
{
public static void main (String[] args)
{
Comparable found;
DVDCollection movies = new DVDCollection();
movies.addDVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
movies.addDVD ("District 9", "Neill Blonkamp", 2009, 19.95, false);
movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
movies.addDVD ("All About Eve", "Joseph Makiewicz", 1950, 17.50, false);
movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
System.out.println (movies);
movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
System.out.println (movies);
Comparable target = ("Jon Favreau");
found = DVD.searchForDVD(target);
if (found != null)
System.out.println ("Found: " + index);
else
System.out.println ("The director was not found.");
Comparable target = ("John Smith");
found = DVD.searchForDVD(target);
DVD.searchForDVD(target);
if (found != null)
System.out.println ("Found: " + index);
else
System.out.println ("The director was not found.");
}
}
import java.text.NumberFormat;
public class DVDCollection
{
private DVD[] list;
private int count;
private double totalCost;
public DVDCollection()
{
list = new DVD[100];
count = 0;
totalCost = 0;
}
public void addDVD (String title, String director, int year, double cost, boolean bluray)
{
list[count] = new DVD (title, director, year, cost, bluray);
for (int index = 1; index < list.length; index++)
{
DVD key = list[count];
int position = count;
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
totalCost += cost;
count++;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "My DVD Collection\n\n";
report += "Number of DVDs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);
report += "\n\nDVD List:\n\n";
for (int dvd = 0; dvd < count; dvd++)
report += list[dvd].toString() + "\n";
return report;
}
}
import java.text.NumberFormat;
public class DVD implements Comparable
{
private String title, director;
private int year;
private double cost;
private boolean bluray;
public DVD (String title, String director, int year, double cost, boolean bluray)
{
this.title = title;
this.director = director;
this.year = year;
this.cost = cost;
this.bluray = bluray;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(cost) + "\t" + year + "\t";
description += title + "\t" + director;
if (bluray)
description += "\t" + "Blu-Ray";
return description;
}
public String getDirector()
{
return director;
}
public int compareTo (Object list)
{
int result;
String otherDirector = ((DVD)list).getDirector();
result = director.compareTo(otherDirector);
return result;
}
public static int searchForDVD (String director)
{
int index = 0, min = 0, max = 7, mid=0;
boolean found = false;
while (!found && min <= max)
{
mid = (min+max)/2;
if (director.compareTo(director) == 0)
return index;
else
if (director.compareTo(director) < 0)
max = mid - 1;
else
min = mid + 1;
}
if (found)
return index;
else
return -1;
}
}
我們需要返回目標「director」的數組位置索引或返回-1。
'director.compareTo(導演)'無論如何總是會返回'0'。 –
我認爲'searchForDVD'應該是'DVDCollection'中的一種方法。 –