I need to modify this movies program so that way the DVDs will be sorted by title at all times (A first thru Z).
I need to use Binary Search so I put a method in DVDCollection but I'm probably missing something or calling it incorrectly.

The program needs to be sorted by title alphabetically both times it prints.

Thank you for your help. The 3 classes of this program are pasted below. (Movies -> DVDCollection -> DVD)

public class Movies
{
   //  Creates a DVDCollection object and adds some DVDs to it. Prints
   //  reports on the status of the collection.
   public static void main (String[] args)
   {
      DVDCollection movies = new DVDCollection();
*
      movies.addDVD ("The Godfather", "Francis Ford Coppala", 1972, 24.95, true);
      movies.addDVD ("District 9", "Neill Blomkamp", 2009, 19.95, false);
      movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
      movies.addDVD ("All About Eve", "Joseph Mankiewicz", 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);
   }
}
import java.text.NumberFormat;
import java.util.Collections;
public class DVDCollection
{
   private DVD[] collection;
   private int count;
   private double totalCost;
*
   //-----------------------------------------------------------------
   //  Constructor: Creates an initially empty collection.
   //-----------------------------------------------------------------
   public DVDCollection ()
   {
      collection = new DVD[100];
      count = 0;
      totalCost = 0.0;
   }
*
   //  Adds a DVD to the collection, increasing the size of the
   //  collection array if necessary.
*
   public void addDVD (String title, String director, int year, double cost, boolean bluRay)
   {
      if (count == collection.length)
         {
          increaseSize();
          }
*
      collection[count] = new DVD (title, director, year, cost, bluRay);
      totalCost += cost;
      count++;
   }
*
   //  Returns a report describing the DVD collection.
   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 += collection[dvd].toString() + "\n";
      }
*
      return report;
   }
   //  Increases the capacity of the collection by creating a
   //  larger array and copying the existing collection into it.
*
   private void increaseSize ()
   {
      DVD[] temp = new DVD[collection.length * 2];
*
      for (int dvd = 0; dvd < collection.length; dvd++)
       {
         temp[dvd] = collection[dvd];
        }
*
      collection = temp;
   }
}
import java.text.NumberFormat;
*
public class DVD
{
   private String title, director;
   private int year;
   private double cost;
   private boolean bluRay;
*
   //  Creates a new DVD with the specified information.
*
   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 getTitle()
   {
       return title;
   }
   //  Returns a string description of this DVD.
   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;
   }
*
}

It prints the following so far, unsorted:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My DVD Collection

Number of DVDs: 5
Total cost: $98.30
Average cost: $19.66

DVD List:

$24.95 1972 The Godfather Francis Ford Coppala Blu-Ray
$19.95 2009 District 9 Neill Blomkamp
$15.95 2008 Iron Man Jon Favreau
$17.50 1950 All About Eve Joseph Mankiewicz
$19.95 1999 The Matrix Andy & Lana Wachowski Blu-Ray

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My DVD Collection

Number of DVDs: 7
Total cost: $141.24
Average cost: $20.18

DVD List:

$24.95 1972 The Godfather Francis Ford Coppala Blu-Ray
$19.95 2009 District 9 Neill Blomkamp
$15.95 2008 Iron Man Jon Favreau
$17.50 1950 All About Eve Joseph Mankiewicz
$19.95 1999 The Matrix Andy & Lana Wachowski Blu-Ray
$22.99 2010 Iron Man 2 Jon Favreau
$19.95 1942 Casablanca Michael Curtiz

Recommended Answers

All 2 Replies

Yes it is thanks. That site gave me an error saying the sites bandwidth limit was reached so I couldn't read the comments anymore so I had to put it on here. It'll only be up for one more day but I figured it couldn't hurt to put on here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.