In: Computer Science
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below.
The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String), distributor (of type Distributor), and earnings (of type double).
Implement the following for the Movie class. Use the names exactly as given (where applicable) and in the other cases, use standard naming conventions.
Distributor. Ensure that if the distributor cannot store the movie because the array is full, the distributor field will be null.
Implement the following for the Distributor class. Use the names exactly as given (where applicable) and in the other cases, use standard naming conventions.
return Arrays.copyOf(movies, numberOfMovies);
does the trick.
Suppose Movie m stores a reference to Distributor object d. Then d stores a reference to
m. While coding the toString() method, ensure that these mutual references do not result in infinite recursion.
MovieDriver class:
Your driver class should perform the following actions:
Suppose you want to check the getter and setter for directorsName in the Movie class. For this, the code first sets the director’s name to some value, say, DIR25. Then you need to check that the getter returns the same name.
One way of accomplishing this is to code as below.
movie9.setDirectorsName("DIR25");
System.out.println(movie9.getDirectorsName());
While this approach works, testing is difficult. You have to remember while testing that
This is very tedious, time-consuming, and highly error-prone.
Instead, you can do the following.
movie9.setDirectorsName("DIR25");
assert movie9.getDirectorsName().equals("DIR25");
The assert statement checks whether movie9.getDirectorsName().equals("DIR25")
Is true. If not, the program crashes with an exception. So, in effect, we have a piece of code that checks itself. You save a lot of time with no additional effort.
Please find 3 classes, Enable assertions on your Environment. Program is Tested.
I have given one assertion, you can make as many as you want.
Distributer.java
package com.demos;
import java.util.Arrays;
public class Distributer {
private String name;
private String address;
private String phone;
private Movie[] movies;
private int numberOfMovies;
public Distributer(String name, String address, String
phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
movies=new Movie[5];
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Movie[] getMovies() {
return
Arrays.copyOf(movies,numberOfMovies);
}
//added
public void setMovies(Movie[] movies) {
this.movies = movies;
}
public boolean addMovie(Movie movie) {
if(numberOfMovies>=movies.length)
return
false;
else {
movies[numberOfMovies++]=new Movie(movie.getName(),
movie.getDirectorsName());
return
true;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name ==
null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Distributer other = (Distributer)
obj;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
return true;
}
@Override
public String toString() {
return "Distributer [name=" + name
+ ", address=" + address + ", phone=" + phone + ", movies="
+ Arrays.toString(movies) + ", numberOfMovies="
+ numberOfMovies + "]";
}
}
Movie.java
package com.demos;
public class Movie {
private String name;
private String directorsName;
private Distributer distributer;
private double earnings;
public Movie(String name, String directorsName,
Distributer distributer) {
super();
this.name = name;
this.directorsName =
directorsName;
this.distributer =
distributer;
}
public Movie(String name, String directorsName)
{
super();
this.name = name;
this.directorsName =
directorsName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirectorsName() {
return directorsName;
}
public void setDirectorsName(String directorsName)
{
this.directorsName =
directorsName;
}
public Distributer getDistributer() {
return distributer;
}
public void setDistributer(Distributer distributer)
{
this.distributer =
distributer;
System.out.println(this.distributer.addMovie(this));
}
public double getEarnings() {
return earnings;
}
public void addToEarnings(double amount) {
this.earnings+=amount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name ==
null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Movie other = (Movie) obj;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
return true;
}
@Override
public String toString() {
return "Movie [name=" + name + ",
directorsName=" + directorsName + ", distributer=" +
distributer
+ ", earnings=" + earnings + "]";
}
}
MovieDriver.java
package com.demos;
public class MovieDriver {
public static void main(String args[]) {
Movie m1=new Movie("Krish2","Rakesh
Roshan");
Movie m2=new Movie("Welcome
Back","Anees");
Movie m3=new Movie("KHNPH",
"Rakesh");
Movie m4=new Movie("Holiday", "AR
Murogaddos");
Movie m5=new Movie("Main Tera Hero
", "David");
Movie m6=new Movie("Judawaa2",
"David"); // when you run you will find false for this
Distributer d1=new
Distributer("pratik", "VPeast", "6626599930");
Distributer d2=new
Distributer("ayush", "Mumbai", "993026130340");
m1.setDistributer(d1);
m2.setDistributer(d1);
m3.setDistributer(d1);
m4.setDistributer(d1);
m5.setDistributer(d1);
m6.setDistributer(d1);
m2.setDirectorsName("Annes
Bazmee"); //set name of director with surname
System.out.println(m1);
System.out.println(m2);
assert
(m2.getDirectorsName().equals("Annes")):"Director name is not
Annes";
}
}
Thanks