Question
Modify the program so that it keeps the DVDs sorted by director. In order to produce efficient code do not apply sorting to keep the
Modify the program so that it keeps the DVDs sorted by director. 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. In addition, implement an efficient method to search for given DVD by the director. Method should be called searchForDVD. It should have only String type formal parameter that provides the director 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 a comment otherwise
PROGRAM RUN outline:
Display all DVDs
Add three more DVDs
Display all DVDs after adding those three DVDs
Search for specific DVD that is in the collection and display it.
Search for specific DVD that is not in the collection
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;
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 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; } }
import java.text.NumberFormat;
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; } 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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started