Write a Java program that mimics the framework for an online
Write a Java program that mimics the framework for an online movie information system similar to IMDB written in Java. Create an abstract base class, two subclasses, and one interface to provide basic functionality for the system. Use the provided UML, the class descriptions, and the test output to determine the proper behavior for your classes.
Create an interface Rateable that: Provides a method vote(int numVotes, int voteScore).
Create an abstract class Movie that: Implements the Rateable and Comparable interfaces. Provides the data fields: title, releaseYear, genre, numVotes, and voteScore. Provides four constructors: o Default, no-arg constructor o Three convenience constructors that allow as parameters: the title the title and releaseYear the title, releaseYear, and genre. Provides an abstract getInfo() method. Provides a concrete toString() method that returns the only movie title. Provides a concrete getRating() method that returns: o -1 if numVotes is 0, o voteScore/numVotes otherwise. Implements the Rateable interface’s vote() method by increasing the existing values of numVotes and voteScore by the amount passed in the appropriate parameter. Implements the Comparable interface’s compareTo() method by comparing ratings. Movies should be sorted in descending order, meaning that a higher rating should be considered “less than” a lower rating.
Create a concrete class Horror that: Automatically sets the genre to Horror in its constructors. Adds a data attribute for number of deaths, numOfDeaths, with the appropriate getter/setter methods.
2
Provides a getInfo() method that returns “title, genre, releaseYear (rating): numDeaths deaths” with the appropriate values. o Detects if it receives a -1 from getRating() and uses “No ratings” as the rating. o To pass LiveLab’s auto-grader, make sure ratings are limited to one decimal place (see documentation on using String.format() which works similar to System.out.printf()).
Create a concrete class Comedy that: Automatically sets the genre to Comedy in its constructors. Adds a data attribute, hasProfanity, to indicate whether there is profanity in the film with the appropriate getter/setter methods. Provides a getInfo() method that returns “title, genre, releaseYear (rating): Profanity/No Profanity” with the appropriate values. o Detects if it receives a -1 from getRating() and use “No ratings” as the rating. o To pass LiveLab’s auto-grader, make sure ratings are limited to one decimal place (see documentation on using String.format() which works similar to System.out.printf()).
NOTE: Movie, Horror, and Comedy should provide all of the appropriate accessor/mutator methods for their data fields. For brevity, these are not included in the UML.
Main method Create the horror movies: o A Nightmare on Elm Street, 1984, 4 deaths o Final Destination 5, 2011, 94 deaths1 o Saw, 2004, 6 deaths Create the comedy movies: o Napoleon Dynamite, 2004, no profanity o Keeping Up with the Joneses, 2016, profanity o Uncle Buck, 1989, profanity
Use the vote() method to assign votes to the respective movies: (# votes - total vote score) o A Nightmare on Elm Street: 151,963 – 1,139,723 o Final Destination 5: 84,907 – 500,951 o Saw: 303,578 – 2,337,551 o Napoleon Dynamite: 163,240 – 1,126,356 o Uncle Buck: 64,031 – 448,217
Create an Array of type Movie and populate it with all of the created movies. Do not create them in sorted order, let the sort method handle that. Sort the array using the java.util.Arrays.sort() method. Using a loop, print the results of the getInfo() call for each movies in the sorted array. 1 http://finaldestination.wikia.com/wiki/List_of_deaths
3
Simplified UML
Test Output Saw, Horror, 2004 (7.7): 6 deaths A Nightmare on Elm Street, Horror, 1984 (7.5): 4 deaths Napoleon Dynamite, Comedy, 2004 (6.9): No Profanity Uncle Buck, Comedy, 1989 (6.9): Profanity Final Destination 5, Horror, 2011 (5.9): 94 deaths Keeping Up with the Joneses, Comedy, 2016 (No ratings): Profanity
Solution
PROGRAM CODE:
Rateable.java
package movies;
public interface Rateable {
//abstract method for the rateable class
public void vote(int numVotes, int voteScore);
}
Movie.java
package movies;
public abstract class Movie implements Rateable, Comparable<Movie>{
// Data fields for the class
String title;
int releaseYear;
String genre;
int numVotes;
int voteScore;
// Default constructor
Movie()
{
title = \"\";
genre = \"\";
}
//Parameterized Constructors
Movie(String title)
{
this.title = title;
}
Movie(String title, int releaseYear)
{
this.title = title;
this.releaseYear = releaseYear;
}
Movie(String title, int releaseYear, String genre)
{
this.title = title;
this.releaseYear = releaseYear;
this.genre = genre;
}
//setters and getters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public int getNumVotes() {
return numVotes;
}
public void setNumVotes(int numVotes) {
this.numVotes = numVotes;
}
public int getVoteScore() {
return voteScore;
}
public void setVoteScore(int voteScore) {
this.voteScore = voteScore;
}
// abstract method
public abstract String getInfo();
// toString() to return the movie title
public String toString()
{
return this.title;
}
//getRating() method to calculate rating
public int getRating()
{
if(this.numVotes > 0)
return -1;
else
return this.voteScore/numVotes;
}
// method to define from Comparable interface
@Override
public int compareTo(Movie movie) {
if(this.getRating() == movie.getRating())
return 0;
else if(this.getRating() > movie.getRating())
return 1;
else
return -1;
}
//method to define from Rateable interface
@Override
public void vote(int numVotes, int voteScore) {
this.numVotes = numVotes;
this.voteScore = voteScore;
}
}
Horror.java
package movies;
public class Horror extends Movie{
int numOfDeaths;
String genre;
//Setting the genre to Horror in default comstructor
public Horror() {
this.genre = \"Horror\";
}
//setter and getter
public int getNumOfDeaths() {
return numOfDeaths;
}
public void setNumOfDeaths(int numOfDeaths) {
this.numOfDeaths = numOfDeaths;
}
// Implementing the abstract method
public String getInfo()
{
String rating = this.getRating() < 0? \"No ratings\": String.valueOf(this.getRating());
return this.title + \",\" + this.genre + \",\" + this.releaseYear + \"(\" + rating + \"):\" + this.numOfDeaths +\"deaths\";
}
}
Comedy.java
package movies;
public class Comedy extends Movie{
boolean hasProfanity;
//Setters and getters
public boolean isHasProfanity() {
return hasProfanity;
}
public void setHasProfanity(boolean hasProfanity) {
this.hasProfanity = hasProfanity;
}
public Comedy()
{
this.genre = \"Comedy\";
}
@Override
public String getInfo() {
String rating = this.getRating() < 0? \"No ratings\": String.valueOf(this.getRating()); // using ternary operator to check if value is less than 0
String profanity = this.hasProfanity? \"Profanity\" : \"No Profanity\";
// using ternary operator to check if profanity is true or false and setting string value based on that
return this.title + \",\" + this.genre + \",\" + this.releaseYear + \"(\" + rating + \"):\" + profanity;
}
}
MainClass.java
package movies;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import javax.sound.midi.ControllerEventListener;
public class MainClass {
public static void main(String[] args) {
//creating objects for Horror and Comedy and adding data
Horror horror = new Horror();
horror.setTitle(\"A Nightmare on Elm Street\");
horror.setReleaseYear(1984);
horror.setNumOfDeaths(4);
Horror horror2 = new Horror();
horror2.setTitle(\"Final Destination 5\");
horror2.setReleaseYear(2011);
horror2.setNumOfDeaths(94);
Horror horror3= new Horror();
horror3.setTitle(\"Saw\");
horror3.setReleaseYear(2004);
horror3.setNumOfDeaths(6);
Comedy comedy1 = new Comedy();
comedy1.setTitle(\"Napolean Dynamite\");
comedy1.setReleaseYear(2004);
comedy1.setHasProfanity(false);
Comedy comedy2 = new Comedy();
comedy2.setTitle(\"Keeping Up with the Joneses\");
comedy2.setReleaseYear(2016);
comedy2.setHasProfanity(true);
Comedy comedy3 = new Comedy();
comedy3.setTitle(\"Uncle Buck\");
comedy3.setReleaseYear(1989);
comedy3.setHasProfanity(true);
horror.vote(151963, 1139723);
horror2.vote(84907, 500951);
horror3.vote(303578, 2337551);
comedy1.vote(163240, 1126356);
comedy3.vote(64031, 448217);
//creating an arraylist and adding all the movies into it
ArrayList<Movie> movieList = new ArrayList<>();
movieList.add(horror);
movieList.add(horror2);
movieList.add(horror3);
movieList.add(comedy1);
movieList.add(comedy2);
movieList.add(comedy3);
//creating an arraylist and adding all the movies into it
Movie[] movies = new Movie[6];
movies[0] = horror;
movies[1] = horror2;
movies[2] = horror3;
movies[3] = comedy1;
movies[4] = comedy2;
movies[5] = comedy3;
//Sorting the arraylist
Arrays.sort(movies);
//printing the arrays
for(int i=0; i<movies.length;i++)
{
System.out.println(movies[i].getInfo());
}
}
}
OUTPUT:
Keeping Up with the Joneses,Comedy,2016(No ratings):Profanity
Final Destination 5,Horror,2011(5):94deaths
Napolean Dynamite,Comedy,2004(6):No Profanity
A Nightmare on Elm Street,Horror,1984(7):4deaths
Saw,Horror,2004(7):6deaths
Uncle Buck,Comedy,1989(7):Profanity








