Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Provide the minimal code for a class hierarchy as described below: a) Provide an

ID: 3699076 • Letter: P

Question

Provide the minimal code for a class hierarchy as described below: a) Provide an abstract class Movie with instance variablestitle, minutes year, and an abstract method rating that returns the rating. Provide a constructor and make all instance variables protected. b) Provide a child class of Movie, called ActionMovie that always has a rating of "PG13". Implement any necessary constructor. c) Provide a child class of Movie, called AnimatedMovie, that always has a rating of "G". Implement any necessary constructor. d) Now assume the Movie class already implements java.lang. Comparable. Provide the code in the Movie class to implement Comparable by comparing years first, and if the yeaza are the same then comparing the minutes.

Explanation / Answer

//Abstract movie class implementing Comparable interface

public abstract class Movie implements Comparable<Movie> {

//Members

String title;

String minutes;

String year;

String rating;

//Constructor

public Movie(String rating, String title, String year, String minutes) {

this.rating = rating;

this.title = title;

this.minutes = minutes;

this.year = year;

}

//getter methods

public String getTitle() {

return title;

}

public String getRating() {

return rating;

}

public String getMinutes() {

return minutes;

}

public String getYear() {

return year;

}

//CompareToMethod - inputs a movie object and compares the year and minutes of the current object with the input object

public int compareTo (Movie m) {

//Compare the year and minutes.

//Returns 0 if both the objects have equal value

//Returns negative if this object < other

//Returns positive if this object > other

int cmpyear = this.getYear().compareTo(m.year);

int cmpmin = this.getMinutes().compareTo(m.minutes);

//We have coded a logic to return 1 if both the objects have same year and minutes. Otherwise, return 0.

if(cmpyear == 0 && cmpmin == 0) {

return 1;

}

return 0;

}

}

//ActionMovie Class

public class ActionMovie extends Movie {

//In the Constructor, calling superclass constructor with rating "PG13"

public ActionMovie(String t, String y, String m) {

super("PG13",t,y,m);

}

}

//AnimatedMovie Class

public class AnimatedMovie extends Movie {

//In the Constructor, calling superclass constructor with rating "G"

public AnimatedMovie(String t, String y, String m) {

super("G",t,y,m);

}

}

//Main Class

public class Mainclass {

public static void main(String[] args) {

int compare_result;

ActionMovie a = new ActionMovie("movie1", "1998", "125");

ActionMovie b = new ActionMovie("movie2", "1995", "125");

compare_result = a.compareTo(b);

printResult(a.getTitle(), b.getTitle(), compare_result);

AnimatedMovie an1 = new AnimatedMovie("movie_an_1", "2003", "105");

AnimatedMovie an2 = new AnimatedMovie("movie_an_2", "2003", "105");

compare_result = an1.compareTo(an2);

printResult(an1.getTitle(), an2.getTitle(), compare_result);

}

static void printResult(String title1, String title2, int compare_result) {

if(compare_result == 1) {

System.out.println("The year and minutes of the objects "+title1+" "+title2+" are the same");

}

else {

System.out.println("The year and minutes of the objects "+title1+" "+title2+" are different");

}

}

}

SAMPLE OUTPUT:

The year and minutes of the objects movie1 movie2 are different
The year and minutes of the objects movie_an_1 movie_an_2 are the same

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote