172k views
2 votes
Write a class named Episode. An instance of this episode class will represent a single episode of a TV show, with a title, a season number, and an episode number. For example "Friends, season 1, episode 1", or "The Wire, season 3, episode 5." Your class should have the following constructors and methods: A constructor that accepts three arguments: a String for the title, an int for the season number, and an int for the episode number. A constructor that accepts only a String, for the title, and sets the season and episode numbers to 1. A setter and getter for the title. (never mind the season and episode numbers.) A method named "equals", that accepts an Episode, and returns true if the title, season number and episode number are the same as the receiving object's. A method named "comesBefore". This method should also accept an Episode. It should return true if the episode that receives the invocation comes before the parameter. That is only true if the two objects have the same title, and if the argument comes from a later season, or the same season, but a later episode. Write only the class. Do not write a whole program

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

class Episode{

//Private variables

private String title;

private int seasonNumber;

private int episodeNumber;

//Argumented constructor

public Episode(String title, int seasonNumber, int episodeNumber) {

this.title = title;

this.seasonNumber = seasonNumber;

this.episodeNumber = episodeNumber;

}

//Getter and setters

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public int getSeasonNumber() {

return seasonNumber;

}

public void setSeasonNumber(int seasonNumber) {

this.seasonNumber = seasonNumber;

}

public int getEpisodeNumber() {

return episodeNumber;

}

public void setEpisodeNumber(int episodeNumber) {

this.episodeNumber = episodeNumber;

}

public boolean comesBefore(Episode e){

//Check if titles' match

if(this.title.equals(e.getTitle())){

//Compare season numbers

if(this.seasonNumber<e.seasonNumber){

return true;

}

//Check if season numbers match

else if(this.seasonNumber==e.getSeasonNumber()){

return this.episodeNumber<e.getEpisodeNumber();

}

}

return false;

}

atOverride // replace the at with at symbol

public String toString() {

return title+", season "+seasonNumber+", episode "+episodeNumber;

}

}

class Main{

public static void main(String[] args) {

Episode e = new Episode("Friends",1,1);

System.out.println(e);

Episode e2 = new Episode("The Wire",3,5);

System.out.println(e2);

System.out.println(e.getTitle()+" comes before "+e2.getTitle()+" = "+e.comesBefore(e2));

}

}

User Ritesh Gune
by
3.3k points