This example shows how to compare the various entertainments like movie, drama and circus. We compare the collections made by each entertainment. We also print the details of the 'like percentage' (how much percentage of the audience like that entertainment).
Compare Entertainments
class CompareEntertainments
{
public static void main(String arg[])
{
Movie julai = new Movie();
julai.name = "Julai";
julai.director = "Trivikram";
julai.stuntMaster = "Peter Hein";
julai.numberOfArtists = 57;
julai.releaseDate = "15-Aug-2012";
julai.collectionsFirstWeek = 215467.8;
julai.collectionsRestOfTheDays = 541132.5;
Drama ramayan = new Drama();
ramayan.name = "Ramayana";
ramayan.writer = "Valmiki";
ramayan.stageSetter = "Anjaneya";
ramayan.numberOfArtists = 200000;
ramayan.releaseDate = "17-Mar-1659 BC";
ramayan.collectionsFirstWeek = 3282937242.86;
ramayan.collectionsRestOfTheDays = 93488272349.51;
Circus jumbo = new Circus();
jumbo.name = "Jumbo";
jumbo.ringMaster = "Antony";
jumbo.numberOfArtists = 316;
jumbo.releaseDate = "16-Dec-1997";
jumbo.collectionsFirstWeek = 2123132.21;
jumbo.collectionsRestOfTheDays = 234936725.09;
if((jumbo.getTotalCollections() > julai.getTotalCollections()) && (jumbo.getTotalCollections() > ramayan.getTotalCollections()))
{
jumbo.print();
}
else if ( julai.getTotalCollections() > ramayan.getTotalCollections())
{
julai.print();
}
else
{
ramayan.print();
}
}
}
class Movie
{
String name;
String director;
String stuntMaster;
int numberOfArtists;
String releaseDate;
double collectionsFirstWeek;
double collectionsRestOfTheDays;
double getTotalCollections()
{
return collectionsFirstWeek + collectionsRestOfTheDays;
}
void print()
{
System.out.println( name + " got the following collections " );
System.out.println("First Week : " + collectionsFirstWeek);
System.out.println("Rest Of The Days : " + collectionsRestOfTheDays);
System.out.println("Total Collections : " + getTotalCollections());
System.out.println("Total Actors : " + numberOfArtists);
System.out.println("Release Date : " + releaseDate);
System.out.println("Director : " + director);
System.out.println("Stunt Master : " + stuntMaster);
}
}
class Drama
{
String name;
String writer;
String stageSetter;
int numberOfArtists;
String releaseDate;
double collectionsFirstWeek;
double collectionsRestOfTheDays;
double getTotalCollections()
{
return collectionsFirstWeek + collectionsRestOfTheDays;
}
void print()
{
System.out.println( name + " got the following collections " );
System.out.println("First Week : " + collectionsFirstWeek);
System.out.println("Rest Of The Days : " + collectionsRestOfTheDays);
System.out.println("Total Collections : " + getTotalCollections());
System.out.println("Total Actors : " + numberOfArtists);
System.out.println("Release Date : " + releaseDate);
System.out.println("Writer : " + writer);
System.out.println("Stage Setter : " + stageSetter);
}
}
class Circus
{
String name;
String ringMaster;
int numberOfArtists;
String releaseDate;
double collectionsFirstWeek;
double collectionsRestOfTheDays;
double getTotalCollections()
{
return collectionsFirstWeek + collectionsRestOfTheDays;
}
void print()
{
System.out.println( name + " got the following collections " );
System.out.println("First Week : " + collectionsFirstWeek);
System.out.println("Rest Of The Days : " + collectionsRestOfTheDays);
System.out.println("Total Collections : " + getTotalCollections());
System.out.println("Total Actors : " + numberOfArtists);
System.out.println("Release Date : " + releaseDate);
System.out.println("Ring Master : " + ringMaster);
}
}
OUTPUTRamayana got the following collections
First Week : 3.28293724286E9
Rest Of The Days : 9.348827234951E10
Total Collections : 9.677120959237E10
Total Actors : 200000
Release Date : 17-Mar-1659 BC
Writer : Valmiki
Stage Setter : Anjaneya
DESCRIPTIONHere we have created three entertainment classes - namely Movie
, Drama
and Circus
. Depending upon the type of the entertainment the corresponding member variables are defined. All of them implement methods - getTotalCollections()
and print()
. Method getTotalCollections()
returns the sum of collectionsFirstWeek
and collectionsRestOfTheDays
. Method print()
simply prints all the details of the entertainment. In the main
method, we have created one object per entertainment and initialized the data. Later, we compared the total collections of every entertainment, and printed the details of the entertainment with highest collections.
THINGS TO TRY
- Add two more integer variables -
totalNumberOfAudience
and numberOfAudienceLiked
. Create a method getLikePercentage()
to calculate the like percentage. Like Percentage = 100.0 * Number Of Audience Liked / Total Number Of Audience. Do this for all the three classes - Movie
, Drama
and Circus
- Change all the print() methods, to print the details of
totalNumberOfAudience
, numberOfAudienceLiked
and the getLikePercentage()
- Change the main method to compare the like percentage of the three entertainments and print the details of the entertainment with highest like percentage.
If we observe these three classes, we will realize that there few attributes (or member variables) like
name
,
numberOfArtists
,
collectionsFirstWeek
and
collectionsRestOfTheDays
, which are common between these three types of entertainments. There is also the method
getTotalCollections()
which is exactly same across all the entertainments. Some statements of the
print()
method are also same.
Additionally, if we want to add the variables
totalNumberOfAudience
and
numberOfAudienceLiked
. We have to modify all the three classes for doing the same.
See
Java Class Inheritance to understand how we can reduce this duplicate code and how we can make changes easier.