Movie Array of Classes The following movies are coming out this year or early ne
ID: 3600479 • Letter: M
Question
Movie Array of Classes
The following movies are coming out this year or early next year:
1. Aquaman, PG-13
2. Justice League, PG-13
3. Bad Day for the Cut, R
4. Star Wars: The Last Jedi, PG-13
5. Paddington 2, G
6. Blade Runner 2049, R
7. Insidious: Chapter 4, R Your mission:
Your Task:
1. Create a Movie class that stores the movie name and the MPAA rating in separate variables. Write appropriate constructors, setters, getters, and a writeOutput method that prints the movie name and rating.
2. In a separate MovieSort class, create an array of type Movie in your main function that stores the movie information.
Write a sort function (e.g bubble sort or selection sort) that sorts the movies alphabetically by name. Recall the String.compareTo method for this sort algorithm.
In your main function call the sort function and output both the movie names and ratings in both the above order and in name-sorted order.
*I'm only in a beginner level programming course
Explanation / Answer
Please find my implementation.
###########
public class Movie {
private String name;
private String rating;
public Movie() {
name = "";
rating = "";
}
public Movie(String name, String rating) {
this.name = name;
this.rating = rating;
}
public String getName() {
return name;
}
public String getRating() {
return rating;
}
public void setName(String name) {
this.name = name;
}
public void setRating(String rating) {
this.rating = rating;
}
public void writeOutput() {
System.out.println("Name: "+name+", Rating: "+rating);
}
}
################
public class MovieSort {
static void bubbleSort(Movie arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j].getName().compareToIgnoreCase(arr[j+1].getName()) > 0)
{
// swap temp and arr[i]
Movie temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
public static void main(String[] args) {
Movie[] array = {
new Movie("Aquaman", "PG-13"),
new Movie("Justice League", "PG-13"),
new Movie("Bad Day for the Cut", "R"),
new Movie("Star Wars: The Last Jedi", "PG-13"),
new Movie("Paddington 2", "G"),
new Movie("Blade Runner 2049", "R"),
new Movie("Insidious: Chapter 4", "R Your mission")
};
System.out.println("Before sorting ");
for(Movie m : array)
m.writeOutput();
bubbleSort(array);
System.out.println(" After Sorting ");
for(Movie m : array)
m.writeOutput();
}
}
/*
Sample run:
Before sorting
Name: Aquaman, Rating: PG-13
Name: Justice League, Rating: PG-13
Name: Bad Day for the Cut, Rating: R
Name: Star Wars: The Last Jedi, Rating: PG-13
Name: Paddington 2, Rating: G
Name: Blade Runner 2049, Rating: R
Name: Insidious: Chapter 4, Rating: R Your mission
After Sorting
Name: Aquaman, Rating: PG-13
Name: Bad Day for the Cut, Rating: R
Name: Blade Runner 2049, Rating: R
Name: Insidious: Chapter 4, Rating: R Your mission
Name: Justice League, Rating: PG-13
Name: Paddington 2, Rating: G
Name: Star Wars: The Last Jedi, Rating: PG-13
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.