Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image 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. We want to sort by the title instance variable, and we want to break ties by the year instance variable. However, our code doesn't work, and we have no idea why.

We have written the following Movie class below, and we want to be able to use the Collections. sort method to sort an ArrayList. We want to sort by the title instance variable, and we want to break ties by the year instance variable. However, our code doesn't work, and we have no idea why. TASK: Fix the bug(s) in our code. EXAMPLE: After running the following lines of code: 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 0) { return (o instanceof Movie) && // check if o is a Movie this.year == ((Movie)o) .year && // check years for equality this.title.equals(((Movie)o).title); // check titles for equality 31 }

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

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions

Question

3. Define the roles individuals play in a group

Answered: 1 week ago