Please help fill in the method in this Format! On the last part of the project,
ID: 3714537 • Letter: P
Question
Please help fill in the method in this Format!
On the last part of the project, you'll add searching and sorting functionalities to the MovieApp. MovieApp.java contains all the methods you need to implement. Refer to the methods description on file for your implementation. Fill in the code in the following methods and submit MovieApp.java: 1. searchMovieByName: search a movie by name using the iterative binary search algorithm. Assume a sorted array 2. searchMovieByName: search a movie by name recursively. Assume an unsorted array 3 sortByYear:sort the array of movies by year using the insertion sort algorithm. 4. sortByName: sorts the array of movies by name using the selection sort algorithm We provide the files here 1. 10.java: the IO class 2. Movie.java: the Movie class from the first part of the project 3 MovieApp.java: the application file with the methods from the second part of the project and the methods you are to update and submit 4 MovieDriver.java: a driver to test yourMovieApp Observe the following rules: DO NOT add any import statements to MovieApp.java DO NOT change the headers of ANY of the given methods DO NOT change/remove any of the given class fields DO NOT add any new class fields DO NOT use System.exit DO NOT use the IO module in MovieApp.java YOU MAY add new helper methods, but you must declare them private If you wish to change MovieDriver, feel free. You are not submitting it.Explanation / Answer
I have only updated the MovieApp class as asked in the question. Bold is the part of code which is added. Let me know incomments if you have any issues
/*
*
* This class implements a library of movies
*
* @author runb-cs111
*
*/
public class MovieApp {
private Movie[] items; // keep movies in an unsorted array
private int numberOfItems; // number of movies in the array
/*
* Default constructor creates array with capacity for 20 movies
*/
MovieApp() {
items = new Movie[20];
numberOfItems = 0;
}
/*
* One argument constructor creates array with user defines capacity
*
* @param capacity defines the capacity of the movie library
*/
MovieApp(int capacity) {
items = new Movie[capacity];
numberOfItems = 0;
}
/*
* Add a movie into the library (unsorted array)
*
* If the library is full (there is no space to add another movie) - create
* a new array with double the size of the current array. - copy all current
* movies into new array - add new movie
*
* @param m The movie to be added to the libary
*/
public void addMovie(Movie m) {
if (numberOfItems == items.length) {
// double the size of the array
Movie[] old = items;
items = new Movie[items.length * 2];
for (int i = 0; i < old.length; i++) {
items[i] = old[i];
}
}
// insert in the first empty position
items[numberOfItems] = m;
numberOfItems += 1;
}
/*
* Removes a movie from the library. Returns true if movie is removed, false
* otherwise. The array should not become sparse after a remove, that is,
* all movies should be in consecutive indexes in the array.
*
* @param m The movie to be removed
*
*/
public boolean removeMovie(Movie m) {
for (int i = 0; i < numberOfItems; i++) {
if (m.equals(items[i])) {
// move the last movie into the ith position
items[i] = items[numberOfItems - 1];
// remove the last movie from array
items[numberOfItems - 1] = null;
numberOfItems -= 1;
return true;
}
}
return false;
}
/*
* Returns the library
*
* @return The array of movies
*/
public Movie[] getMovies() {
return items;
}
/*
* Returns the current number of movies in the library
*
* @return The number of items in the array
*/
public int getNumberOfItems() {
return this.numberOfItems;
}
/*
* Update the rating of movie @m to @ratings
*
* @param @m The movie to have its ratings updated
*
* @param @ratings The new ratings of @m
*
* @return tue if update is successfull, false otherwise
*
*/
public boolean updateRating(Movie m, int ratings) {
for (int i = 0; i < numberOfItems; i++) {
if (m.equals(items[i])) {
// found @m
m.setRatings(ratings);
return true;
}
}
return false;
}
/*
* Prints all movies
*/
public void print() {
for (int i = 0; i < numberOfItems; i++) {
System.out.println(items[i]);
}
}
/*
* Return all the movies by @director. The array size should be the number
* of movies by @director.
*
* @param director The director's name
*
* @param An array of all the movies by @director
*
*/
public Movie[] getMoviesByDirector(String director) {
int cnt = 0;
// 1. finds out how many movies are made by @director
for (int i = 0; i < numberOfItems; i++) {
if (items[i].getDirector().equalsIgnoreCase(director)) {
cnt += 1;
}
}
// 2. create array with exact cnt movies made by @director
Movie[] arr = new Movie[cnt];
cnt = 0;
for (int i = 0; i < numberOfItems; i++) {
if (items[i].getDirector().equalsIgnoreCase(director)) {
arr[cnt++] = items[i];
}
}
return arr;
}
/*
* Return all the movies made in @year. The array size should be the number
* of movies made in @year.
*
* @param year The year the movies were made
*
* @return An array of all the movies made in @year
*
*/
public Movie[] getMoviesByYear(int year) {
int cnt = 0;
// 1. finds out how many movies are made on @year
for (int i = 0; i < numberOfItems; i++) {
if (items[i].getYear() == year) {
cnt += 1;
}
}
// 2. create array with exact cnt movies made on @year
Movie[] arr = new Movie[cnt];
cnt = 0;
for (int i = 0; i < numberOfItems; i++) {
if (items[i].getYear() == year) {
arr[cnt++] = items[i];
}
}
return arr;
}
/*
* Return all the movies with ratings greater then @ratings
*
* @param ratings
*
* @return An array of all movies with ratings greater than @ratings
*
*/
public Movie[] getMoviesWithRatingsGreaterThan(int ratings) {
Movie[] arr = new Movie[numberOfItems];
int cnt = 0;
for (int i = 0; i < numberOfItems; i++) {
if (items[i].getRatings() > ratings) {
arr[cnt++] = items[i];
}
}
return arr;
}
/*
* Search for movie name @name using the iterative binary search algorithm.
* Assumes the array items is sorted
*/
public Movie searchMovieByName(String name) {
/** COMPLETE THIS METHOD **/
// THE FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE
// Changed as needed for your implementation
this.sortByName();
int l = 0, r = numberOfItems - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (items[m].getName().equals(name))
return items[m];
// If x greater, ignore left half
if (items[m].getName().compareTo(name) < 0)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return null;
}
/*
* Sorts movies (items field) by year using insertion sort
*/
public void sortByYear() {
/** COMPLETE THIS METHOD **/
int n = numberOfItems;
for (int i = 1; i < n; ++i) {
Movie key = items[i];
int j = i - 1;
while (j >= 0 && items[j].getYear() > key.getYear()) {
items[j + 1] = items[j];
j = j - 1;
}
items[j + 1] = key;
}
}
/*
* Sorts array of movies by name using selection sort
*/
public void sortByName() {
/** COMPLETE THIS METHOD **/
int n = numberOfItems;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (items[j].getName().compareTo(items[min_idx].getName()) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
Movie temp = items[min_idx];
items[min_idx] = items[i];
items[i] = temp;
}
}
/*
* Search for movie @name using recursive linear search
*/
public static Movie searchMovieByName(String name, Movie[] movies, int l, int r) {
/** COMPLETE THIS METHOD **/
// Changed as needed for your implementation
if (l > r) {
return null;
}
if (movies[l].getName().equals(name)) {
return movies[l];
}
return searchMovieByName(name, movies, l + 1, r);
}
}
Output
====================================================
Created a Movie app with the default constructor.
Would you like to load in the first 20 movies? (Y for yes or n for no)
y
Loading Movie #1: The Shawshank Redemption - 1994 [Rate: 5]
Loading Movie #2: Schindler's List - 1993 [Rate: 5]
Loading Movie #3: The Wizard of Oz - 1939 [Rate: 4]
Loading Movie #4: One Flew Over the Cuckoo's Nest - 1975 [Rate: 5]
Loading Movie #5: Forrest Gump - 1994 [Rate: 4]
Loading Movie #6: The Sound of Music - 1965 [Rate: 5]
Loading Movie #7: West Side Story - 1961 [Rate: 3]
Loading Movie #8: Star Wars IV - 1977 [Rate: 4]
Loading Movie #9: 2001: A Space Odyssey - 1968 [Rate: 3]
Loading Movie #10: E.T - 1982 [Rate: 2]
Loading Movie #11: The Silence of the Lambs - 1992 [Rate: 3]
Loading Movie #12: It's a Wonderful Life - 1946 [Rate: 1]
Loading Movie #13: The Lord of The Rings: Return of the Kind - 2003 [Rate: 1]
Loading Movie #14: Gladiator - 2000 [Rate: 5]
Loading Movie #15: Titanic - 1997 [Rate: 3]
Loading Movie #16: Saving Private Ryan - 1998 [Rate: 4]
Loading Movie #17: Raiders of the Lost Arc - 1981 [Rate: 2]
Loading Movie #18: Rocky I - 1976 [Rate: 5]
Loading Movie #19: To Kill a Mockingbird - 1962 [Rate: 2]
Loading Movie #20: Jaws - 1975 [Rate: 4]
Would you like to load in the last of the movies? (Y for yes or n for no)
y
Loading Movie #1: Patton - 1970 [Rate: 1]
Loading Movie #2: Braveheart - 1995 [Rate: 4]
Loading Movie #3: Jurassic Park - 1993 [Rate: 5]
Loading Movie #4: The Exorcist - 1973 [Rate: 3]
Loading Movie #5: The Grapes of Wrath - 1940 [Rate: 1]
Loading Movie #6: The Green Mile - 1999 [Rate: 5]
====================================================
====================================================
====================================================
You have loaded a total of: 26 movies.
Now entering user interactivity mode.
====================================================
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
sortByYear
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
Printing all of the movies stored in the app.
The Wizard of Oz - 1939 [Rate: 4]
The Grapes of Wrath - 1940 [Rate: 1]
It's a Wonderful Life - 1946 [Rate: 1]
West Side Story - 1961 [Rate: 3]
To Kill a Mockingbird - 1962 [Rate: 2]
The Sound of Music - 1965 [Rate: 5]
2001: A Space Odyssey - 1968 [Rate: 3]
Patton - 1970 [Rate: 1]
The Exorcist - 1973 [Rate: 3]
One Flew Over the Cuckoo's Nest - 1975 [Rate: 5]
Jaws - 1975 [Rate: 4]
Rocky I - 1976 [Rate: 5]
Star Wars IV - 1977 [Rate: 4]
Raiders of the Lost Arc - 1981 [Rate: 2]
E.T - 1982 [Rate: 2]
The Silence of the Lambs - 1992 [Rate: 3]
Schindler's List - 1993 [Rate: 5]
Jurassic Park - 1993 [Rate: 5]
The Shawshank Redemption - 1994 [Rate: 5]
Forrest Gump - 1994 [Rate: 4]
Braveheart - 1995 [Rate: 4]
Titanic - 1997 [Rate: 3]
Saving Private Ryan - 1998 [Rate: 4]
The Green Mile - 1999 [Rate: 5]
Gladiator - 2000 [Rate: 5]
The Lord of The Rings: Return of the Kind - 2003 [Rate: 1]
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
sortByName
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
Printing all of the movies stored in the app.
2001: A Space Odyssey - 1968 [Rate: 3]
Braveheart - 1995 [Rate: 4]
E.T - 1982 [Rate: 2]
Forrest Gump - 1994 [Rate: 4]
Gladiator - 2000 [Rate: 5]
It's a Wonderful Life - 1946 [Rate: 1]
Jaws - 1975 [Rate: 4]
Jurassic Park - 1993 [Rate: 5]
One Flew Over the Cuckoo's Nest - 1975 [Rate: 5]
Patton - 1970 [Rate: 1]
Raiders of the Lost Arc - 1981 [Rate: 2]
Rocky I - 1976 [Rate: 5]
Saving Private Ryan - 1998 [Rate: 4]
Schindler's List - 1993 [Rate: 5]
Star Wars IV - 1977 [Rate: 4]
The Exorcist - 1973 [Rate: 3]
The Grapes of Wrath - 1940 [Rate: 1]
The Green Mile - 1999 [Rate: 5]
The Lord of The Rings: Return of the Kind - 2003 [Rate: 1]
The Shawshank Redemption - 1994 [Rate: 5]
The Silence of the Lambs - 1992 [Rate: 3]
The Sound of Music - 1965 [Rate: 5]
The Wizard of Oz - 1939 [Rate: 4]
Titanic - 1997 [Rate: 3]
To Kill a Mockingbird - 1962 [Rate: 2]
West Side Story - 1961 [Rate: 3]
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
searchBS
NOTE: MovieApp must be sorted by name
Which name would you like to search by?
Titanic
Found: Titanic - 1997 [Rate: 3]
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
searchRec
Which name would you like to search by?
Titanic
Found: Titanic - 1997 [Rate: 3]
Please press enter to continue.
====================================================
====================================================
Possible Commands:
print: print all of the movies.
director: get all movies by a director.
year: get all movies by a year.
rating: get all movies about a certain rating.
remove: Removes a movie at a certain index.
update: Update the ratings of a movie.
sortByYear: sort all movies by year.
sortByName: sort all movies by name.
searchBS: search all movies by name using binary search.
searchRec: search all movies by name recursively.
exit: stop the program.
What would you like to do? (use the commands above. For example, type print to print all movies):
You may type exit or press Control C to stop this program.
exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.