JAVA 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.
Inheritance.java
//parent class for all the sub classes
class VideoContent{
//data variable
private String title;
private String genre;
//Static variable that keeps track of number of objects in the
Video Library
public static int numberOfObjects = 0;
//default constructor
public VideoContent(){
this.title="";
this.genre="";
numberOfObjects++;
}
//overloaded constructor
public VideoContent(String title, String genre){
this.title = title;
this.genre = genre;
numberOfObjects++;
}
//setter for title
public void setTitle(String title){
this.title = title;
}
//getter for title
public String getTitle(){
return this.title;
}
//setter for genre
public void setGenre(String genre){
this.genre = genre;
}
//getter for genre
public String getGenre(){
return this.genre;
}
//toString method
public String toString(){
return "VideoContent\n____________\nTitle: "+getTitle()+"\nGenre:
"+getGenre()+"\n";
}
}
//Movieclass that inherits from VideoContent
class Movies extends VideoContent{
//data variables
private String releaseDate;
private String studio;
private String streamingSource;
//default constructor
public Movies(){
this.releaseDate ="";
this.studio = "";
this. streamingSource = "";
}
//overloaded constructor
public Movies(String title, String date, String genre, String
studio, String source){
super(title,genre);
this.releaseDate = date;
this.studio = studio;
this.streamingSource = source;
}
//setters
public void setReleaseDate(String date){
this.releaseDate = date;
}
public void setStudio(String studio){
this.studio = studio;
}
public void setStreamingSource(String source){
this.streamingSource = source;
}
//getters
public String getReleaseDate(){
return this.releaseDate;
}
public String getStudio(){
return this.studio;
}
public String getStreamingSource(){
return this.streamingSource;
}
//toString method
public String toString(){
return "Movie\nTitle: "+getTitle()+"\nRelease Date: "+
getReleaseDate()+"\nGenre: "+getGenre()+"\nStudio: "+
getStudio()+"\nStreaming Source: "+getStreamingSource()+"\n";
}
}
//TvShow class that inherits from VideoContent class
class TvShow extends VideoContent{
//data variables
private String dateOfE1;
private int seasons;
private String network;
private String runningTime;
//default constructor
public TvShow(){
this.dateOfE1 = "";
this.seasons = 0;
this.network = "";
this.runningTime = "";
}
//overloaded constructor
public TvShow(String title, String genre, String date, int
seasons,
String network, String runningTime){
super(title,genre);
this.dateOfE1 = date;
this.seasons = seasons;
this.network = network;
this.runningTime = runningTime;
}
//setters
public void setDateOfE1(String date){
this.dateOfE1 = date;
}
public void setSeasons(int num){
this.seasons = num;
}
public void setNetwork(String network){
this.network = network;
}
public void setRunningTime(String time){
this.runningTime = time;
}
//getters
public String getDateOfE1(){
return this.dateOfE1;
}
public String getNetwork(){
return this.network;
}
public int getSeasons(){
return this.seasons;
}
public String getRunningTime(){
return this.runningTime;
}
//to string method
public String toString(){
return "TV Show\nTitle: "+getTitle()+"\nGenre: "+getGenre()+
"\nDate of First Episode: "+getDateOfE1()+"\nNumber of Seasons:
"+
+getSeasons()+"\n Network: "+getNetwork()+
"\nRunning Time: "+getRunningTime()+"\n";
}
}
//calss MininSeries which inherits from VideoContent class
class MiniSeries extends VideoContent{
//data variables
private int episodes;
private String runningTime;
private String lead;
//default constructor
public MiniSeries(){
this.episodes =0;
this. runningTime ="";
this.lead ="";
}
//overloaded constructor
public MiniSeries(String title, String genre, int episodes, String
runningTime
, String lead){
super(title, genre);
this.episodes =episodes;
this.runningTime = runningTime;
this.lead = lead;
}
//setters
public void setEpisodes(int episodes){
this.episodes = episodes;
}
public void setRunningTime(String runningTime){
this.runningTime = runningTime;
}
public void setLead(String lead){
this.lead = lead;
}
//getters
public int getEpisodes(){
return this.episodes;
}
public String getRunningTime(){
return this.runningTime;
}
public String getLead(){
return this.lead;
}
//toString method
public String toString(){
return "Mini Series\nTitle: "+getTitle()+"\nGenre: "+
getGenre()+"\nNumber of Episodes: "+getEpisodes()+
"\nRunning Time: "+getRunningTime()+"\nLead Actor/Actress: "+
getLead()+"\n";
}
}
//Driver class that demonstartes Inheritance
public class Inheritance{
//driver function
public static void main(String[] args) {
//object of class Movies using over loaded constructor
Movies m = new Movies("Extraction", "24 April,
2020","Acton/Thriller",
"AGBO", "Netflix" );
//object of class TvShow using overloaded constructor
TvShow t = new TvShow("Friends", "Sitcom", "22 September, 1994",10,
"NBC",
"20-22 minutes");
//object of MiniSeries using default constructor and
setters
MiniSeries s = new MiniSeries();
s.setTitle("Chernobyl");
s.setGenre("Historical Drama/ Tragedy");
s.setEpisodes(5);
s.setRunningTime("59-72 minutes");
s.setLead("Jared Harris, Stellan Skarsgård, Emily Watson, Paul
Ritter");
//Printing number of objects in Video Library
System.out.println("Number of objects in Video Library: "+
VideoContent.numberOfObjects);
//Printing details about each object
System.out.println(m);
System.out.println(t);
System.out.println(s);
}
}
------------------------------------------------------------------------------------------------------------------------------------
Note: Comments have been added for better understanding.
Output of the code:
----------------------------------------------------------------------------------------------------------------------------------------------------------
Hope this helps. If you like the answer, please upvote. Cheers!!
Get Answers For Free
Most questions answered within 1 hours.