Write a class called Movie that will store some basic information about a certai
ID: 3902841 • Letter: W
Question
Write a class called Movie that will store some basic information about a certain movie. It should have a field for the title, the year it was released, and a rating between 0 and 10. The class should have the following methods.
// Fields
private String title;
private int releaseYear;
private double rating;
// Inside of the constructor verify that the parameters are valid.
// The title shouldn't be an empty string, the releaseYear can't
// be in the future, and the rating has to be between 0 and 10. If
// any of the parameters are invalid, then print out an error to the console
public Movie(String title, int releaseYear, double rating)
// This should return a string that represents this movie object.
// It should be in the following format: TITLE (YEAR), RATING out of 10
public String toString()
// This class should also include getters for all of the fields.
Next, using the Movie class, create a MovieDatabase class that will handle any number of movies. The movies inside the MovieDatabase should be stored as an array. The MovieDatabase class should have the following methods:
// Fields
private Movie[] movies;
// For this constructor set the Movie array equal to the one passed in
public MovieDatabase(Movie[] m)
// Returns the highest rated movie
public Movie getHighestRatedMovie()
// Returns the newest movie (by year)
public Movie getNewestMovie()
// Returns the Movie at the index passed in. If the index is invalid,
// then return null.
public Movie getMovieAt(int index)
// Returns the number of movies in the database
public int getMovieCount()
Then, create a tester class which uses the array below. Create a MovieDatabase object which uses that array. Finally, test all the methods from the MovieDatabase class.
Movie[] test =
{
new Movie("The Dark Knight", 2008, 8.9),
new Movie("Fight Club", 1999, 8.8),
new Movie("12 Angry Men", 1957, 8.9),
new Movie("The Godfather", 1972, 9.2),
new Movie("The Lord of the Rings: The Return of the King", 2003, 8.9),
new Movie("The Godfather: Part II", 1974, 9.0),
new Movie("Pulp Fiction", 1994, 8.9),
new Movie("Schindler's List", 1993, 8.9),
new Movie("The Shawshank Redemption", 1994, 9.2),
new Movie("The Good, the Bad and the Ugly", 1966, 8.9)
};
Write in Java and at a simple level
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Movie.java
------
public class Movie {
// Fields
private String title;
private int releaseYear;
private double rating;
// Inside of the constructor verify that the parameters are valid.
// The title shouldn't be an empty string, the releaseYear can't
// be in the future, and the rating has to be between 0 and 10. If
// any of the parameters are invalid, then print out an error to the console
public Movie(String title, int releaseYear, double rating)
{
boolean valid = true;
if(title == null || title.isEmpty())
{
System.out.println("Invalid title!");
valid = false;
}
if(releaseYear > 2018)
{
System.out.println("Release year can't be in future");
valid = false;
}
if(rating < 0 || rating > 10)
{
System.out.println("Invalid rating. Should be in 0-10");
valid = false;
}
if(valid)
{
this.title = title;
this.releaseYear = releaseYear;
this.rating = rating;
}
}
// This should return a string that represents this movie object.
// It should be in the following format: TITLE (YEAR), RATING out of 10
public String toString()
{
return String.format("%s (%d), %.1f out of 10", title, releaseYear, rating);
}
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 double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
}
MovieDatabase.java
--------
public class MovieDatabase {
// Fields
private Movie[] movies;
// For this constructor set the Movie array equal to the one passed in
public MovieDatabase(Movie[] m)
{
movies = m;
}
// Returns the highest rated movie
public Movie getHighestRatedMovie()
{
int highIndex = 0;
for(int i = 1; i < movies.length; i++)
{
if(movies[i].getRating() > movies[highIndex].getRating())
highIndex = i;
}
return movies[highIndex];
}
// Returns the newest movie (by year)
public Movie getNewestMovie()
{
int newIndex = 0;
for(int i = 1; i < movies.length; i++)
{
if(movies[i].getReleaseYear() > movies[newIndex].getReleaseYear())
newIndex = i;
}
return movies[newIndex];
}
// Returns the Movie at the index passed in. If the index is invalid,
// then return null.
public Movie getMovieAt(int index)
{
if(index >=0 && index < movies.length)
return movies[index];
else
return null;
}
// Returns the number of movies in the database
public int getMovieCount()
{
return movies.length;
}
}
TestMovies.java
-----
public class TestMovies {
public static void main(String[] args) {
Movie[] test =
{
new Movie("The Dark Knight", 2008, 8.9),
new Movie("Fight Club", 1999, 8.8),
new Movie("12 Angry Men", 1957, 8.9),
new Movie("The Godfather", 1972, 9.2),
new Movie("The Lord of the Rings: The Return of the King", 2003, 8.9),
new Movie("The Godfather: Part II", 1974, 9.0),
new Movie("Pulp Fiction", 1994, 8.9),
new Movie("Schindler's List", 1993, 8.9),
new Movie("The Shawshank Redemption", 1994, 9.2),
new Movie("The Good, the Bad and the Ugly", 1966, 8.9)
};
MovieDatabase db = new MovieDatabase(test);
System.out.println("No. of movies: " + db.getMovieCount());
System.out.println("Movie at index 4: " + db.getMovieAt(4));
System.out.println("Highest rated: " + db.getHighestRatedMovie());
System.out.println("Newest Movie: " + db.getNewestMovie());
}
}
output
----
No. of movies: 10
Movie at index 4: The Lord of the Rings: The Return of the King (2003), 8.9 out of 10
Highest rated: The Godfather (1972), 9.2 out of 10
Newest Movie: The Dark Knight (2008), 8.9 out of 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.