在binarySearch中发出传递参数

时间:2013-10-20 02:27:47

标签: java binary-search

我有三节课;包含我的主要方法的电影/测试器类。一个DVDCollection类,用于逐步对我的数组进行排序,以及一个具有Constuctor方法并覆盖compareTo的DVD Comparable Class。我们的教练要求我们使用Movies / tester类主要方法搜索特定导演的集合。我完全陷入困境,因为我认为我需要传递一个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;
    }     

}

我们需要返回目标“director”的数组位置的索引或返回-1。

1 个答案:

答案 0 :(得分:0)

我希望下面的代码是您所期望的,只需将其作为一个单独的程序&amp;执行。

public class Movies
{
   public static DVDCollection movies;
   public static void main (String[] args)
   {
     Comparable found;
     int index;

   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");
  System.out.println("Target: "+target.toString());
  found = DVD.searchForDVD(target.toString());
  index=Integer.parseInt(found.toString());

  System.out.println ("Found: " + index);      

  Comparable target1 = ("John Smith");
  System.out.println("Target1: "+target1.toString());
  found = DVD.searchForDVD(target1.toString());
  index=Integer.parseInt(found.toString());

    System.out.println ("Found1: " + index);   
   }
}



  class DVDCollection
   {
      public static 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++;
    }

   @Override
   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;
   }

} 

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;
}

 @Override
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;
 }

 @Override
 public int compareTo (Object list)
 {
  int result;

  String otherDirector = ((DVD)list).getDirector();
  result = director.compareTo(otherDirector);

  return result;
 }

 public static int searchForDVD (String director) 
 {
   boolean flag=false;
   for(int i=0;i<DVDCollection.list.length;i++)
   {
       if(DVDCollection.list[i]!=null)
       {
           System.out.println("Director: "+DVDCollection.list[i].getDirector());
           if(DVDCollection.list[i].getDirector().equals(director))
           {
               flag=true;
               return i;
           }
       }               
   }
   if(!flag)
       return -1;
   return -1;

   }     

 }