Question

In: Computer Science

In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...

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.

  • A constructor to initialize all instance variables, except earnings. A value for each instance variable will be passed as a parameter to the constructor with the exception of earnings, which will be set to zero. The parameters will be passed in the order name, directorsName, and distributor. The code will need to call addMovie() of

Distributor. Ensure that if the distributor cannot store the movie because the array is full, the distributor field will be null.

  • A second constructor to initialize all instance variables, except earnings. A value for each instance variable will be passed as a parameter to the constructor with the exception of earnings and distributor, which will be set to zero and null, respectively. The parameters will be passed in the order name and directorsName. This constructor will use the one listed above to initialize the fields. It should do no assignments on its own.
  • Getters for all instance variables.
  • A setter method for each instance variable, except earnings. The setter for setDistributor() will need to call addMovie() of Distributor. (Ensure that if the distributor cannot store the movie because the array is full, the distributor field will be null.
  • addToEarnings: a method than takes an amount (of type double) and adds that amount of money to the movie’s earnings.  
  • equals: a method to test the equality of two movies. Two movies are considered to be equal if they have the same name. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation. (See a copy of Class Exercise 1 for a verbatim copy of these requirements.)
  • hashCode: to be consistent with equals. See the JDK documentation copied in Class Exercise 1.
  • toString: a method that returns a nicely formatted string description of the movie. All fields except distributor and the distributor’s name must be part of the string.
  • Distributor Class:
  • The Distributor class represents a distributor of movies. Every movie has at most one distributor and every distributor can distribute zero or more movies. (In this simplistic application, a distributor can distribute at most five movies.) The Distributor class has the following instance variables, named exactly as follows:
  • name: a string that represents the distributor's name.
  • address: a string that represents the distributor’s address.
  • phone: a string that represents the distributor’s phone.
  • movies: an instance variable of type array of Movie with length of 5.
  • numberOfMovies: an instance variable that indicates the number of movies that have been added to the movies array.
  • Implement the following for the Distributor class. Use the names exactly as given (where applicable) and in the other cases, use standard naming conventions.

  • A constructor that takes as input only the distributor's name, address, and phone in that order, and creates the distributor by initializing the fields appropriately, and instantiating the array movies. Each Movie reference in the array will initially be null.
  • Getters and setters for name, address, and phone.
  • A getter for movies. Use Arrays.copyOf to trim the array to the exact number of movies and return the trimmed array. The statement
  • return Arrays.copyOf(movies, numberOfMovies);

    does the trick.

  • addMovie: a method that takes a single parameter of type Movie and returns a boolean. The method attempts to add the movie supplied as parameter to the array of Movie objects. If the number of movies is greater than or equal to the length of the array, the movie does not get added and the method returns false. Otherwise, the movie is added and the method returns true. Note that movies[0] must be filled before movies[1] and movies[1] must be filled before movies[2], etc. The field numberOfMovies is updated if necessary.
  • equals: a method to test the equality of two distributors. Two distributors are considered to be equal if they have the same name. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation.
  • hashCode: to be consistent with equals.
  • toString: a method that returns a nicely formatted string description of the distributor. A properly formatted list of movies must be part of the returned string.
  • 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:

  • Create at least 6 Movie objects with your choice of movie names and director names.
  • Create at least 2 Distributor objects with your choice of names, addresses, and phone numbers.
  • Make the same Distributor object (say, d) the distributor of 5 different movies. Attempt to add d as the distributor of a sixth movie. Demonstrate that this attempt does not crash the program, but the operation does not succeed. You must do this via the constructor of Movie as well as via the setDistributor() method.
  • Exercise every method of both Movie and Distributor classes. Demonstrate that they work. For this, I recommend using the assert statement. Here is an example.
  • 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

  • you had set the director’s name to DIR25 and
  • the getter returns DIR25, for which you need to examine the displays from the user.
  •             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.

Solutions

Expert Solution

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


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Why is it more feasible to use Objects and object oriented programming as compared to using...
Why is it more feasible to use Objects and object oriented programming as compared to using method based programs? What are the disadvantages of using only methods in your programs.
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop...
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop a Java Application to compute an individual student’s GPA and store the records in a database. The application should have two components i.e. The student and the course components. The following should be the minimal operations on the course component: – Set course information – Print course information – Show credit hours – Show course number The following should be the minimal operations on...
This week, you will create and implement an object-oriented programming design for your project. You will...
This week, you will create and implement an object-oriented programming design for your project. You will learn to identify and describe the classes, their attributes and operations, as well as the relations between the classes. Create class diagrams using Visual Studio. Review the How to: Add class diagrams to projects (Links to an external site.) page from Microsoft’s website; it will tell you how to install it. Submit a screen shot from Visual Studio with your explanation into a Word...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
Briefly explain the terms used in object-oriented programming with examples.
Briefly explain the terms used in object-oriented programming with examples.
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by...
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by writing some programs in Java. The first step will be to install and integrated development environment (IDE) that will be where you will write and compile your programs. You will also write your first program using Java to show that you have correctly installed the IDE. The project instructions and deliverables are as follows: Download and install Java JDK and NetBeans IDE using the...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT