In: Computer Science
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";
}
}
If you have any problem with the code feel free to comment. Add more tv series class accordingly.
TvSeries
package tvSeries;
public class TVSeries {
//instance variables
private String title;
private String genre;
private String rating;
private int year;
private int numEpisodes;
//constructor
public TVSeries(String title, String genre, String rating, int year, int numEpisodes) {
this.title = title;
this.genre = genre;
this.rating = rating;
this.year = year;
this.numEpisodes = numEpisodes;
}
//all getter methods
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getRating() {
return rating;
}
public int getYear() {
return year;
}
public int getNumEpisodes() {
return numEpisodes;
}
//plot method
public String plot() {
return "no plot yet";
}
//toString method
@Override
public String toString() {
return "Title=" + title + "\nGenre=" + genre + "\nRating=" + rating + "\nYear=" + year + "\nNumber of Episodes="
+ numEpisodes;
}
}
BigBangTheory
package tvSeries;
public class BigBangTheory extends TVSeries {
//initializing the TvSeries constructor
public BigBangTheory() {
super("The Big Bang Theory", "Comedy", "8.3", 2007, 279);
}
//giving the plot
@Override
public String plot() {
return "Nerds learning to interact with girls";
}
}
BreakingBad
package tvSeries;
public class BreakingBad extends TVSeries {
public BreakingBad() {
super("Breaking Bad", "Crime", "9.6", 2008, 62);
}
@Override
public String plot() {
return "A chemistry teacher becomes a drug lord";
}
}
TvSeriesTester
package tvSeries;
public class TVSeriesTester {
public static void main(String[] args) {
//using polymorphism
TVSeries bbt = new BigBangTheory();
TVSeries bb = new BreakingBad();
//showing result
System.out.println(bbt);
System.out.println("Plot= "+bbt.plot());
System.out.println("\n"+bb);
System.out.println("Plot= "+bb.plot());
}
}
Output