Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the given program so that it keeps the DVDs sorted by Author. In order to

ID: 3677261 • Letter: M

Question

Modify the given program so that it keeps the DVDs sorted by Author. In order to produce efficient code do not apply sorting to keep the DVDs sorted. Redesign method add instead, so that it inserts the DVD into a sorted collection and produces sorted collection. Method should take advantage of the fact that DVDs are sorted by author at the start.

In addition, implement an efficient method to search for given DVD by the author. Method should be called searchForDVD. It should have only String type formal parameter that provides the author that we are searching for. The return type should be an integer that specifies the index in the array where the DVD is located. When search is not successful method returns -1.

Method searchForDVD should be in the DVD collection class. Modify main method so that at the end it also tests method searchForDVD by performing one successful and one unsuccessful search. Display the DVD found in successful search, and write an appropriate comment otherwise.

   

PROGRAM RUN outline:

   Display all DVDs

   Add two more DVDs

   Display all DVDs after adding those two DVDs

   Search for specific DVD that is in the collection

   Search for specific DVD that is not in the collection

Here is the given code:

public class Movies

{

            public static void main (String[] args)

            {

                  DVDCollection movies = new DVDCollection();

                  movies.addDVD(“The Godfather”, “Francis Ford Coppola”, 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, false);

                 System.out.println(movies);

                 movies.addDVD(“Iron Man 2”, “JonFavreau”, 2010, 22.99, false);

                 movies.addDVD(“Casablanca”, “Michael Curtiz”, 1942, 19.95, false);

                System.out.println(movies);

            }

}

import java.text.NumberFormat;

public class DVDCollection

{

            private DVD[] collection;

            private int count;

            private double totalCost;

            public DVDCollection()

            {

                 collection = new DVD[100];

                 count = 0;

                 totalCost = 0.0;

            }

           

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

            }

            public String toString()

            {

                 NumberFormat fmt = NumberFormat.getCurrencyInstance();

                 String report = “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ”;

                 report += “My DVD Collection ”;

                 report += ”Number of DVDs: “ + count + “ ”;

                 report +=”Total cost: “ + fmt.format(totalCost) + “ ”);

                 report += “Average cost: “ + fmt.format(totalCost/count);

                 report += “ DVD List: ”;

                 for (int dvd = 0; dvd < count; dvd++)

                      report += collection[dvd].toString() + “ ”;

                 return report;

            }

            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;

            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) + “ ” + year + “ ”;

                        description += title + “ ” + director;

                        if (bluray)

                                    description += “ ” + “Blu-ray”;

                        return description;

            }

}

Explanation / Answer

Hi, I have made it sorted by the director. I could not get, what did you meant by author.

public class Movies
{
            public static void main (String[] args)
            {
                  DVDCollection movies = new DVDCollection();

                  movies.addDVD(“The Godfather”, “Francis Ford Coppola”, 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, false);
                 System.out.println(movies);
                 movies.addDVD(“Iron Man 2”, “JonFavreau”, 2010, 22.99, false);
                 movies.addDVD(“Casablanca”, “Michael Curtiz”, 1942, 19.95, false);
                System.out.println(movies);
            }
}

import java.text.NumberFormat;
public class DVDCollection
{
            private DVD[] collection;
            private double totalCost;

            public DVDCollection()
            {
                 collection = new DVD[100];
                 totalCost = 0.0;
            }
          
            public void addDVD(String title, String director, int year, double cost, boolean bluray)
            {
   DVD newDvd = new DVD(title, director, year, cost, bluray);
   int index = Collections.binarySearch(collection, newDvd);
   if(index >= 0) {
       System.out.println(“DVD with title ” + newDvd.getTitle() + “ already exists.”);
   }
   else {
       int index1 = -index - 1;
       collection = insertDVD(collection, newDVD, index1);
       System.out.println(“DVD with title ” + newDvd.getTitle() + “ added.”);
   }
                 totalCost += cost;
            }

private DVD[] insertElement(DVD[] original, DVD newDVD, int in)
{
   int length = original.length;
   DVD[] destination = new DVD[length+1];
   System.arraycopy(original, 0, destination, 0, in);
   destination[in] = newDVD;
   System.arraycopy(original, in, destination, in+1, length-in);
   return destination;
}

            public String toString()
            {
                 NumberFormat fmt = NumberFormat.getCurrencyInstance();

                 String report = “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ”;
                 report += “My DVD Collection ”;

                 report += ”Number of DVDs: “ + count + “ ”;
                 report +=”Total cost: “ + fmt.format(totalCost) + “ ”);
                 report += “Average cost: “ + fmt.format(totalCost/count);

                 report += “ DVD List: ”;

                 for (int dvd = 0; dvd < count; dvd++)
                      report += collection[dvd].toString() + “ ”;

                 return report;
            }

            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 implements Comparator<DVD>, Comparable<DVD>
{
            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 int compareTo(DVD d)
   {
       return (this.director).compareTo(d.getDirector());
   }
      @Override
      public int compare(DVD d1, DVD d2)
      {
           if (d1.getDirector() == d2.getDirector()) {
               return 0;
           }
           if (d1.getDirector() == null) {
               return -1;
           }
           if (d2.getDirector() == null) {
               return 1;
           }
           return d1.getDirector().compareTo(d2.getDirector());
      }


   public String getDirector() { return director; }
   public String getTitle() { return title; }
            public String toString()
            {
                        NumberFormat fmt = NumberFormat.getCurrencyInstance();

                      
                        String description;

                        description = fmt.format(cost) + “ ” + year + “ ”;
                        description += title + “ ” + director;
                        if (bluray)
                                    description += “ ” + “Blu-ray”;

                        return description;
            }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote