In: Computer Science
Let’s say that we work at some new video store and need to design some classes that will help us organize our database of movies. You are to write the following classes:
Write a class called Video that will have the following properties:
Declare the instance fields title (String), length (int), avail (Boolean)
Declare two constructors
One default
One with title and length as parameters
Write a method that will output the fields of the objects by overriding the print method
Write an accessor method for availability:
public boolean getAvail()
Write a mutator method for availability!
public void setAvail(boolean b)
2. Write another class called Movie that will inherit the properties of the Video class but will also define two new fields: the movie rating and the name of the director. Give the movie class it’s own print method and constructors for default and with title, length, rating, and director as paramenters.
3. In the Main.java class create the following objects:
Video – Name: CPC Sports, 90 mins Available: No
Video - Name: CPC Through the Years, 2 hours, Available: Yes
Movie- Name: Mission Impossible 4, 2 hours 30 mins, Available: No, Director: Brad Bird, Rating: 4 stars
Movie- Name: Star Wars, 2 hours, Available: Yes: Director: George Lucas, Rating 5 stars
Can you change the availability of a movie using the mutator method from Video?
Given Code:
Main.java
class Main {
public static void main(String[] args) {
}
}
Video.java
class Video{
}
Movie.java
class Movie extends Video{
}
If anything is not clear please let me know in the comments.
Note: In order to change the availability of movie object we use super keyword. So what super does is that it is an object of base class i.e. Main class and thus we can access the set method of availability.
CODE:
class Video {
String title;
int length;
private boolean avail;
public Video() {
this.avail = false; // means not
available by default
}
public Video(String title, int length) {
this.title = title;
this.length = length;
}
public String toString() {
String avail;
if (getAvail() == true)
avail =
"Yes";
else
avail =
"No";
return "Video- Name: " +
getTitle() + ", " + length + " mins, Available: " + avail;
}
// to accessors
public String getTitle() {
return title;
}
public int getLength() {
return length;
}
public boolean isAvail() {
return avail;
}
public boolean getAvail() {
return avail;
}
void setAvail(boolean b) { // mutator
this.avail = b;
}
}
class Movie extends Video {
int movieRating;
String nameOfTheDirector;
public Movie() {
this.movieRating = 0; // by
default
}
public Movie(String title, int length, boolean avail, String nameOfTheDirector, int movieRating) {
super.title = title;
super.length = length;
super.setAvail(avail); // changing
the availability of a movie using the mutator method from
Video
this.movieRating =
movieRating;
this.nameOfTheDirector =
nameOfTheDirector;
}
@Override
public String toString() {
String avail;
if (getAvail() == true)
avail =
"Yes";
else
avail =
"No";
return "Movie- Name " +
getTitle() + ", " + getLength() + " hours" + ", Available: " +
avail + ", Director: "
+ nameOfTheDirector + "Rating: " + movieRating +
" stars";
}
}
public class Main {
public static void main(String[] args) {
Video video1 = new Video("CPC
SPorts", 90); // video object
video1.setAvail(false); // not
available
Video video2 = new Video("CPC
Through the Years", 2);
video2.setAvail(true);
Video movie1 = new
Movie("Mission Impossible 4", 2, false, "Brad Bird", 4); // movie
object
Video movie2 = new Movie("Star
Wars", 2, true, "George Lucas", 5);
System.out.println(video1.toString());
System.out.println(video2.toString());
System.out.println(movie1.toString());
System.out.println(movie2.toString());
}
}
Output:
Code Screenshot:
Please If something is missing or not clear
Please Thumbs Up
Thank You