我有三门课;一个包含我的主要方法的电影/测试器类。一个对我的数组进行增量排序的 DVDCollection 类和一个具有构造函数方法并覆盖 compareTo 的 DVD 可比较类。我们的讲师要求我们使用 Movies / tester 类的 main 方法来搜索特定导演的集合。我完全被卡住了,因为我认为我需要传递一个 Comparable Array 和一个 Comparable 目标,但指令说我只传递一个 String 参数。我收到无效的数据类型。任何人都关心帮助有需要的学生。
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;
}
}
我们需要返回目标“导演”的数组位置的索引或返回-1。