Based on Movie package example, you will create a package about your favorite TV series and provide the plot()
Readme.txt: Explain how you apply polymorphism in your program
package Movie;
public class MovieTester {
public static void main(String[] args) {
for(int i = 1; i < 11; i++){
Movie movie = randomMovie();
System.out.println("Movie # " + i + ": " + movie.getTitle() + ",
Genre: " + movie.getGenre() + '\n'
+"Plot: " + movie.plot());
}
}
public static Movie randomMovie(){
int randomNumber = (int)(Math.random() * 5) + 1; //1 and 5
System.out.println("Random number is going to be generated and it is " + randomNumber);
switch(randomNumber){
case 1: return new Borat();
case 2: return new Inception();
case 3: return new JohnWick();
case 4: return new Parasite();
case 5: return new Shrek();
}
return null;
}
}
class Movie{
private String title;
private String genre;
private String rating;
private int year;
private int length; //minute
public Movie(String title, String genre, String rating, int
year, int length) {
this.title = title;
this.genre = genre;
this.rating = rating;
this.year = year;
this.length = length;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getRating() {
return rating;
}
public int getYear() {
return year;
}
public int getLength() {
return length;
}
public String plot(){
return "No plot yet provided";
}
}
//Shrek, JohnWick, Inception, Borat, Parasite
class Shrek extends Movie{
public Shrek(){
super("Shrek","Animation","R",1991,120);
}
@Override
public String plot(){
return "To save Fiona.";
}
}
class JohnWick extends Movie{
public JohnWick(){
super("John Wick","Action","R",2000,120);
}
@Override
public String plot(){
return "To revenge";
}
}
class Inception extends Movie{
public Inception(){
super("Inception", "Thriller", "PG",1996, 150);
}
@Override
public String plot(){
return "To chasing.";
}
}
class Borat extends Movie{
public Borat(){
super("Borat", "Comedy", "R", 2020,120);
}
@Override
public String plot(){
return "Trying to find Pamela Anderson.";
}
}
class Parasite extends Movie{
public Parasite(){
super("Parasite", "Horror", "R", 2019, 120);
}
@Override
public String plot(){
return "About three family";
}
}
You already have a code where multiple movies with their plots
are given.. So i will only focus my answer on below:
Explain how you apply polymorphism in your
program
The movie class is the base class, where each of the specifc
movies(child classes) such as Parasite, Inception etc, they extend
the original movie class. The plot method in base movie class says
"No plot yet provided", while each of the child movie classes
override this method and provide their own specific plots..
At run time with dynamic polymorphism, on the movie object it is
actually decided which plot method should be called.. As the child
classes provide their implementation, hence instead of base class,
child class methods are actually invoked and thus the plots are
printed..
please let me know if anything is not clear in my answer or any
issues. Thanks
please upvote if possible.
Get Answers For Free
Most questions answered within 1 hours.