Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions

Question

What is the use of a tagline?

Answered: 1 week ago

Question

Explain what Residual risk is. Chpt # 9

Answered: 1 week ago

Question

=+Where does the focus of labor relations lie? Is it collective

Answered: 1 week ago

Question

=+With whom does the firm have to negotiate?

Answered: 1 week ago