In: Computer Science
Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method.
Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to include the appropriate setter, getter methods and toString to display descriptions in a nicely formatted output. In addition to default constructors, define overloaded constructors to accept and initialize class data. When using a default constructor, use the setter from the main program to set values. Use a static variable to keep track of the number of objects in the video library. Display the number of items prior to listing them.
Data for movies should contain attributes for the title, release date, genre, studio, and streaming source (Netflix, Prime or Hulu).
Data for TvShow should contain the title, genre (same as others), date of the first episode, number of seasons, network, and running time.
Data for MiniSeries should include attributes for title, genre (same as others), number of episodes, running time, lead actor or actress.
Create a main driver class to create several objects of each type of video content. Use examples of both the default and overloaded constructors. Then display your complete video library with all fields.
Solution
In the code common attributes is taken as parent class which is superclass
superclass get inherited three times by movies, tvshows , miniseries
superclass has two attributes which is common in almost all three derived class which is title and genre
movies attributes are following in the data types are
TV_shows attributes are following :-
similarly minseries attributes are :-
tostringmethod()
toString() method returns the string representation of the object.
java compiler internally invokes the toString() method on the object.
overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
//--------------------
//file name : result.java
//----------------------
class superclass
{
public String title;
public String genre;
}
class movies extends superclass
{
public String release_date;
public String studio;
public String streamingsource;
movies(String title,String genre, String release_date,String studio, String streamingsource)
{
this.title=title;
this.genre=genre;
this.release_date=release_date;
this.studio=studio;
this.streamingsource=streamingsource;
}
public String toString(){//overriding the toString() method
return title+" "+genre+" "+release_date+" "+studio+" "+streamingsource;
}
}
class tvshows extends superclass
{
public String date_of_first_series;
public int no_of_series;
public String network;
public String running_time;
tvshows(String title,String genre,String date1,int num, String network,String running_time)
{
this.title=title;
this.genre=genre;
this.date_of_first_series=date1;
this.no_of_series=num;
this.network=network;
this.running_time=running_time;
}
public String toString(){//overriding the toString() method
return date_of_first_series+" "+no_of_series+" "+network+" "+running_time;
}
}
class mini_series extends superclass
{
public int no_of_running_time;
public String lead_actor;
mini_series(String title,String genre, int no_of_running_time,String lead_actor)
{
this.title=title;
this.genre=genre;
this.no_of_running_time=no_of_running_time;
this.lead_actor=lead_actor;
}
public String toString(){
//overriding the toString() method
return title+" "+genre+" "+no_of_running_time+" "+lead_actor;
}
}
class result
{
public static void main(String args[])
{
movies mv1 =new movies("Harley Quinn","Action","12/7/2005","Dreamburgs","Netflix");
tvshows tv1=new tvshows("TVshows","Comedy","12/05/1998",3,"hello","56 minutes");
mini_series ms1=new mini_series("Minishows","Drama",5,"Leonardo ");
System.out.println("Movies Details ::::");
System.out.println("title, Genre, release_date, studio, streaming source");
System.out.println(mv1);
System.out.println("\nTV Details ::::");
System.out.println("title, Genre, number in series, network ,running_time ");
System.out.println(tv1);
System.out.println("\nMini_series ::::");
System.out.println("title, Genre, running_time, lead_actor ");
System.out.println(ms1);
}
}
//output
all output has done through tostring method which has been shown in question
STEP 1: save file result.java
STEP2 : for compiling type javac result.java
STEP3 : for run to the file type java result
then it will print the desired output as you wants