私には 3 つのクラスがあります。私のメインメソッドを含む映画/テスタークラス。配列をインクリメンタルにソートする DVDCollection クラスと、Constuctor メソッドを持ち、compareTo を上書きする DVD Comparable Class です。インストラクターから、映画/テスター クラスのメイン メソッドを使用して、特定の監督のコレクションを検索するように依頼されました。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 を返す必要があります。