Question

In: Computer Science

JAVA programming language Modify the following code. Make changes so that Movies can be sorted by...

JAVA programming language

Modify the following code. Make changes so that Movies can be sorted by title

-----------------------------------------------------------------

package Model;
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
// TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title
public class Movie extends DataStoreObj
{
private String title;
private String description;
private LocalDate releaseDate;
private Duration runningTime;
private Rating rating;
private List<Genre> genres = new ArrayList<>();
private List<Actor> actors = new ArrayList<>();
// TODO: Add list of reviews
private List<Review> reviews = new ArrayList<>();
public Movie(String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {
this(null, title, description, rating, releaseDate, runningTimeMinutes);
}
public Movie(Long id, String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {
super(id);
this.title = title;
this.description = description;
this.rating = rating;
this.releaseDate = LocalDate.parse(releaseDate);
// https://stackoverflow.com/a/41800301/673393
this.runningTime = Duration.ofMinutes(runningTimeMinutes);
}
public Duration getRunningTime() {
return runningTime;
}
public String getTitle() {
return title;
}
public void addGenre(Genre genre) {
genres.add(genre);
}
public void addActor(long actorId) {
Actor foundActor = Datastore.getActorById(actorId);
actors.add(foundActor);
}
public List<Actor> getActors() {
return actors;
}
public void addReview(Review review) {
// TODO: Write code to add the review to the list
reviews.add(review);
}
public String toString() {
int runningTimeMinutes = (int) this.getRunningTime().getSeconds() / 60;
return String.format("%s (%s, %s) %s {%s min}", this.title, this.rating, this.releaseDate.getYear(), genres, runningTimeMinutes);
}
// TODO - You'll need a new method here to tell Java how to compare two movies by title
public List<Review> getReviews() {
return reviews;
}

}

Solutions

Expert Solution

Here is the completed code for this problem. Made Movie class implement Comparable interface, and implemented compareTo method that is needed for sorting Movie objects by title in alphabetical order. No other change has been made. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Movie.java (updated)

import java.time.Duration;

import java.time.LocalDate;

import java.util.ArrayList;

import java.util.List;

//made Movie class implement Comparable interface

public class Movie extends DataStoreObj implements Comparable<Movie> {

    private String title;

    private String description;

    private LocalDate releaseDate;

    private Duration runningTime;

    private Rating rating;

    private List<Genre> genres = new ArrayList<>();

    private List<Actor> actors = new ArrayList<>();

// TODO: Add list of reviews

    private List<Review> reviews = new ArrayList<>();

    public Movie(String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {

        this(null, title, description, rating, releaseDate, runningTimeMinutes);

    }

    public Movie(Long id, String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {

        super(id);

        this.title = title;

        this.description = description;

        this.rating = rating;

        this.releaseDate = LocalDate.parse(releaseDate);

// https://stackoverflow.com/a/41800301/673393

        this.runningTime = Duration.ofMinutes(runningTimeMinutes);

    }

    public Duration getRunningTime() {

        return runningTime;

    }

    public String getTitle() {

        return title;

    }

    public void addGenre(Genre genre) {

        genres.add(genre);

    }

    public void addActor(long actorId) {

        Actor foundActor = Datastore.getActorById(actorId);

        actors.add(foundActor);

    }

    public List<Actor> getActors() {

        return actors;

    }

    public void addReview(Review review) {

// TODO: Write code to add the review to the list

        reviews.add(review);

    }

    public String toString() {

        int runningTimeMinutes = (int) this.getRunningTime().getSeconds() / 60;

        return String.format("%s (%s, %s) %s {%s min}", this.title, this.rating, this.releaseDate.getYear(), genres, runningTimeMinutes);

    }

    public List<Review> getReviews() {

        return reviews;

    }

    //method that returns an integer value after comparing titles of this movie and another. returns a negative value

    //if this movie should come before other, 0 if both have same titles, a positive value if other should come first

    @Override

    public int compareTo(Movie other) {

        return this.title.compareTo(other.title);

    }

}


Related Solutions

JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE...
*****************PLEASE PROVIDE THE CODE IN RACKET PROGRAMMING LANGUAGE ONLY AND MAKE SURE CODE RUNS ON THE WESCHEME IDE************* Write a tail-recursive Racket function "kept-short-rec" that takes an integer and a list of strings as parameters and evaluates to an integer. The resulting integer should be the number of strings on the original list whose string length is less than the integer parameter. For example, (kept-short-rec 3 '("abc" "ab" "a")) should evaluate to 2 because there are only 2 strings shorter...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
PLEASE MODIFY CODE IN JAVA In-line comments denote your changes and briefly describe the functionality of...
PLEASE MODIFY CODE IN JAVA In-line comments denote your changes and briefly describe the functionality of each method or element of the class Appropriate variable and method naming conventions are used throughout your code. modify the Driver.java class file to do the following: Implement the method you have chosen Add attributes, as needed, to support the required functionality SYSTEM REQUIREMENTS- Each dog goes through a six- to nine month training regimen before they are put into service. Part of our...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT