PART 1. GETTING STARTED Crea.e a new class. Name it Film. Add this code to your
ID: 3750004 • Letter: P
Question
PART 1. GETTING STARTED Crea.e a new class. Name it Film. Add this code to your Film class. You do not eed to type in all the commes ·The Film class contains data about a film. Mcthods: Flm)-no argamenms constructor Flim String, int) constructor with two arguments String toStringo- convert data members to String boolean equals(Film) - compare two Film objccts for equality public class Film private String title; private int year, Title of film Year film was released // No arguments canstructor initializes data members to ull or zero public Filmo ile ear 0 Constructor initializes data memburs public FilmxString flmTide, int nim Year) title- new Stringnlmide ear nlm Year; String) converts data members of abject to String public String toString0 return titleyear eqals0 compares two Films for equality public boolean equals (Film 2) if (this.title.equals(2.title) return true; else return false public static void main (String argstD WNEW CODE GOES HEREExplanation / Answer
Film.java
public class Film {
private String title;
private int year;
public Film() {
this.title = "";
this.year = 0;
}
public Film(String title, int year) {
this.title = title;
this.year = year;
}
@Override
public String toString() {
return title + " " + year;
}
public boolean equals(Film other) {
if (this.title.equals(other.title))
return true;
return false;
}
public static void main(String args[])
{
Film predator;
predator=new Film("Predator",2018);
System.out.println(predator.toString());
System.out.println(predator);
Film f;
f=new Film("Avatar", 2010);
if(predator.equals(f))
System.out.println("The Two Films are same");
else
System.out.println("The Two Films are not same");
}
}
__________________
Output:
Predator 2018
Predator 2018
The Two Films are not same
_________________
What is the difference between the two statements?
System.out.println(predator.toString());
System.out.println(predator);
Ans) Both are same.Because, when we call predator.toString() it will call the toString() method in the Film class and return the String and it will be displayed.
When System.out.println(predator); is called the jvm will call the toString() method of the object class. But as we overrides the toString() method in the Film class the jvm will call toString() method in the Film class which will return a String and it will be displayed.
_______________
Based on understanding of films when would you consider two films to be the same (equal)?
Ans) if the two films are having same name and released in the same year then i will consider the two films are same.
__________________
Look at the equals() method in the code example .According to the code ,under what conditions are two films considered to be equal?
Ans)As per our code , two films are same when they are having the same name.
__________________
__________Could you plz rate me well.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.