Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java We have written the following Movie class below, and we want to be able to use the Collections. sort method to sort an ArrayList

Javaimage text in transcribedimage text in transcribed

We have written the following Movie class below, and we want to be able to use the Collections. sort method to sort an ArrayList movies = new ArrayList ;. movies.add(new Movie("Good Burger", 1997)); movies.add(new Movie("The Lord of the Rings: The Fellowship of the Ring", 2001)); movies.add(new Movie("Fast Five", 2011)); Collections. sort(movies) ; for (Movie m : movies) { System.out.println(m): We will print the following: (Fast Five, 2011) (Good Burger, 1997) (The Lord of the Rings: The Fellowship of the Ring, 2001) HINT: We asked a tutor for help by submitting a ticket to the lab queue, and the tutor suggested we look into how to properly implement the Comparable interface. Tutors are so helpful and kind 1 class Movie { // instance variables private final String title; private final int year; // constructor public Movie(String t, int y) { if(t == null) { this.title = ""; } else { this.title = t; this.year = y; // instance methods public String getTitle() { return this.title; public int getYear() { return this.year; public String toString() { return "(" + this.title + "," + this.year + ")"; public boolean equals(Object o) { return (o instanceof Movie) && // check if o is a Movie this.year == ((Movie)) .year && Il check years for equality this.title.equals(((Movie)o).title); // check titles for equality

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions