Question
Using a list of movies you own or wish to see, sort them based on title, year created, or movie studio. Use the selection sort
Using a list of movies you own or wish to see, sort them based on title, year created, or movie studio. Use the selection sort algorithm to reorder the collection as requested in ascending or descending order.
- Create a new project called 07.10 Assignment in your Module 07 assignments folder.
- Copy your Movie.java and tester file from the previous sorting project as a starting point. Rename the tester to V3. Delete the existing sort methods.
- Declare an array of at least 10 Movie objects. For each, you will need the movie title, year, and studio. Of course, be sure to use school-appropriate movie titles. For example: Meet the Robinsons, 2007, Disney
- Design a static method that traverses through the array and prints each element.
- Create the following static methods in the tester class. Utilize the selection sort algorithm. Each method will take two arguments: the array and an int parameter. Sort appropriately depending on the value of the second parameter: 1 sort ascending, or 2 sort descending.
- a method that sorts the array by the movie title
- a method that sorts the array by year released
- a method that sorts the array by the name of the studio that produces it
- Test your sorting methods by calling each and displaying the results. Start by showing the array without sorting. Then demonstrate sorting by title in both ascending and descending order. Do the same for year and studio. Be sure to clearly label your output so someone looking at it knows which sort was applied each time.
Movie.java
/** * Purpose: * * @author (enter your name) * @version (enter today's date) * */ public class Movie { // instance variables private int year; private String title; private String studio;
// Constructor for objects of class Movie public Movie(String title, int year, String studio) { // initialize instance variables this.title = title; this.year = year; this.studio = studio; }
public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getStudio() { return studio; } public void setStudio(String studio) { this.studio = studio; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String toString() { String str = String.format("%-30s %4d %-20s", title, year, studio); return str; } }
MovieTesterV2.java
/** * Write a description of class MovieV2 here. * * @author * @version Jan 2022 */
public class MovieTesterV2 { //goes through the array public static void print(Movie[] movies){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i]); } } //takes an array of Movie objects and an integer choice as parameter public static void sortByTitle(Movie[] movies, int choice){ if(choice == 1){ for (int j = 1; j < movies.length; j++) { String key = movies[j].getTitle(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key.compareTo(movies[i].getTitle()) > 0) { break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } }else if(choice == 2){ for (int j = 1; j < movies.length; j++) { String key = movies[j].getTitle(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key.compareTo(movies[i].getTitle()) < 0) {//here too break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } } } //takes an array of Movie objects and an integer choice as parameter to check for choice value public static void sortByYear(Movie[] movies, int choice){ if(choice == 1){ for (int j = 1; j < movies.length; j++) { int key = movies[j].getYear(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key > movies[i].getYear()) { break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } }else if(choice == 2){ for (int j = 1; j < movies.length; j++) { int key = movies[j].getYear(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key < movies[i].getYear()) { break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } } } //takes an array of Movie objects and an integer choice as parameter public static void sortByStudio(Movie[] movies, int choice){ if(choice == 1){ for (int j = 1; j < movies.length; j++) { String key = movies[j].getStudio(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key.compareTo(movies[i].getStudio()) > 0) { break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } }else if(choice == 2){ for (int j = 1; j < movies.length; j++) { String key = movies[j].getStudio(); Movie temp = movies[j]; int i = j - 1; while (i >= 0) { if (key.compareTo(movies[i].getStudio()) < 0) { break; } movies[i + 1] = movies[i]; i--; } movies[i + 1] = temp; } } } public static void main(String[] args) { //array of Movies with 10 elements Movie[] movies = new Movie[10]; //instantiate each Movie movies[0] = new Movie("The Avengers", 2012, "Marvel"); movies[1] = new Movie("Spectre", 2015, "Columbia Pictures"); movies[2] = new Movie("Justice League", 2017, "Warner Bros."); movies[3] = new Movie("Almost Famous", 2000, "Paramount Pictures"); movies[4] = new Movie("Scott Pilgrim Vs. The World", 2010, "United Artists"); movies[5] = new Movie("The Notebook", 2004, "New Line Cinema"); movies[6] = new Movie("Howl's Moving Castle", 2004, "Ghibli"); movies[7] = new Movie("Spirited Away", 2001, "Ghibli"); movies[8] = new Movie("Luca", 2021, "Walt Disney Studios"); movies[9] = new Movie("Star Wars", 1977, "Lucas Films"); //print an unsorted array of movie objets System.out.printf(" [Unsorted] %-30s %4s %-20s ", "Title", "Year", "Studio"); print(movies); //sort objects by title in ascending order System.out.printf(" [Sort by title in ascending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByTitle(movies, 1); print(movies); //sort objects by title in descending order System.out.printf(" [Sort by title in descending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByTitle(movies, 2); print(movies); //sort objects by year in ascending order System.out.printf(" [Sort by year in ascending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByYear(movies, 1); print(movies); //sort our Movie objects by year in descending order System.out.printf(" [Sort by year in descending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByYear(movies, 2); print(movies); //sort objects by Studio in ascending order System.out.printf(" [Sort by name of studio in ascending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByStudio(movies, 1); print(movies); //sort objects by studio in descending order System.out.printf(" [Sort by name of studio in descending order] %-30s %4s %-20s ", "Title", "Year", "Studio"); sortByStudio(movies, 2); print(movies); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Below is the updated MovieTesterV3java file with the selection sort methods for sorting the movies based on title year and studio in both ascending an...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