Assignment 1: Object-Oriented Design and Arrays of Objects Out: Saturday, January 18th, 2020 Due: Saturday, February 1st, 2020, no later than 11:59 pm Total points: 50 Objective In this assignment,...

1 answer below »

Assignment 1: Object-Oriented Design and Arrays of Objects


Out: Saturday, January 18th, 2020 Due: Saturday, February 1st, 2020, no later than 11:59 pm


Total points: 50


Objective


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.


Problem Description


Movie class


The Movie class represents a movie and has the following attributes: name (of type String), directorName (of type String), earnings (of type double), and genre (of type int). The possible genres are Comedy (0), Action (1) or Fiction (2).


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 directorName, then genre.


• Getters for all instance variables.


• A setter method for each instance variable, except earnings.


• addToEarnings: a method than takes as input 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, the same directorName,


and the same genre and the comparison for string attributes must be case insensitive. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation. (See a copy of Lecture1- exercise-1 for a verbatim copy of these requirements).


• 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 integer 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 following statement does the trick. return Arrays.copyOf(movies, numberOfMovies);


• 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 movies array. 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 as necessary.


• addMovie: this an overload of addMovie method that takes four input parameters that represent a movie's name, directorName, genre, and earnings (in that


order) and returns a boolean. The method creates a Movie object using the input parameters and attempts to add that object to the movies array. 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 as necessary.


• totalNumMovies: a method that returns as output the number of movies of that distributor.


• findTotalEarnings: a method that returns as output the total earnings for the distributor which is the sum of all earnings for the distributor's movies.


• comedyEarnings: a method that returns as output the total earnings of movies of the comedy genre.


• addEarnings: a method that takes as input two parameters that represent a movie's name and earnings and returns a boolean as output. The method searches the movies array for a movie with the same name as the input name (case insensitive) and add the input earnings to the movie's earnings if the movie is found and return True. The method returns False if the input movie name is not found.


• getNumGenre: a method that takes as input an integer that represents a genre (i.e., 0, 1, or 2) and returns as output the number of distributor's movies of that genre. The method returns -1 if the input is invalid (i.e., less than 0 or greater than 2).


• calculateTax: a static method that takes two input parameters that represent (1) the tax rate (e.g., 10%) and (2) a distributor. The method returns as output the amount of tax that need to be paid by the distributor based on the total distributor's earning. For example, if the total earning for a distributor is $1M and the tax rate is 5%, then the method returns 50000.


• equals: a method to test the equality of two distributors. Two distributors are considered to be equal if they have the same name and the comparison must be case insensitive. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation.


• toString(): a method that returns a string representation of the distributor that includes the following:


• distributor's name, address, and phone.


• number of movies for this distributor,


• details of all the movies, one per line


• the total earnings for that distributor,


MovieDriver class


Your driver class should perform the following actions:


• Create at least 6 Movie objects with your choice of movie names, director names, and genres.


• Create 2 Distributor objects with your choice of name, address, and phone number.


• Add 5 different movies to the first distributor object that you created in the previous step. Attempt to add the sixth movie to the same distributor and demonstrate that this attempt does not crash the program, but the operation does not succeed.


• Add 2 movies to the second distributor object.


• Print the two distributors.


• Exercise every method of both the Movie and Distributor classes. Demonstrate that they work. For this, I recommend using the assert statement. Here is an example.


Using assert statement


Suppose you want to check the getter and setter for directorName 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 (1) you had set the director’s name to DIR25 , and (2) 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.


Here is some information about assert, taken verbatim from the JDK documentation:


assert Expression1 ; where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.


One thing you have to remember is that in normal execution, the JVM ignores any assert


statements, so these statements never crash. This makes the program run with little overhead. This also means that you need to enable assertions while testing. For this, use the parameter –ea while executing the program.


java -ea Driver


In Eclipse, you can achieve this as follows.


1. Right click on Driver.java. 2. Choose Run As-> Run Configurations. 3. In the resulting window, click on the Arguments tab. 4. In the VM arguments textbox, type


-ea 5. Click Run.


Remove the above VM argument to disable assertions, if you don’t need it.



Answered Same DayJan 28, 2021

Answer To: Assignment 1: Object-Oriented Design and Arrays of Objects Out: Saturday, January 18th, 2020 Due:...

Abhishek answered on Jan 30 2021
135 Votes
50013 - Java Movie Distributor/Screenshots/1.png
50013 - Java Movie Distributor/Screenshots/O2.png
50013 - Java Movie Distributor/Problem/Problem.txt
Assignment 1: Object-Oriented Design and Arrays of Objects
Out: Saturday, January 18th, 2020 Due: Saturday, February 1st, 2020, no later than 11:59 pm
Total points: 50
Objective
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.
Problem Description
Movie class
The Movie class represents a movie and has the following attributes: name (of type String), directorName (of type String), earnings (of type double), and genre (of type
int). The possible genres are Comedy (0), Action (1) or Fiction (2).
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 directorName, then genre.
• Getters for all instance variables.
• A setter method for each instance variable, except earnings.
• addToEarnings: a method than takes as input 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, the same directorName,
and the same genre and the comparison for string attributes must be case insensitive. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation. (See a copy of Lecture1- exercise-1 for a verbatim copy of these requirements).
• 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 integer 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 following statement does the trick. return Arrays.copyOf(movies, numberOfMovies);
• 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 movies array. 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 as necessary.
• addMovie: this an overload of addMovie method that takes four input parameters that represent a movie's name, directorName, genre, and earnings (in that
order) and returns a boolean. The method creates a Movie object using the input parameters and attempts to add that object to the movies array. 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 as necessary.
• totalNumMovies: a method that returns as output the number of movies of that distributor.
• findTotalEarnings: a method that returns as output the total earnings for the distributor which is the sum of all earnings for the distributor's movies.
• comedyEarnings: a method that returns as output the total earnings of movies of the comedy genre.
• addEarnings: a method that takes as input two parameters that represent a movie's name and earnings and returns a boolean as output. The method searches the movies array for a movie with the same name as the input name (case insensitive) and add the input earnings to the movie's earnings if the movie is found and return True. The method returns False if the input movie name is not found.
• getNumGenre: a method that takes as input an integer that represents a genre (i.e., 0, 1, or 2) and returns as output the number of distributor's movies of that genre. The method returns -1 if the input is invalid (i.e., less than 0 or greater than 2).
• calculateTax: a static method that takes two input parameters that represent (1) the tax rate (e.g., 10%) and (2) a distributor. The method returns as output the amount of tax that need to be paid by the distributor based on the total distributor's earning. For example, if the total earning for a distributor is $1M and the tax rate is 5%, then the method returns 50000.
• equals: a method to test the equality of two distributors. Two distributors are considered to be equal if they have the same name and the comparison must be case insensitive. Ensure that your implementation satisfies all of the requirements of the equals method specified in the JDK documentation.
• toString(): a method that returns a string representation of the distributor that includes the following:
• distributor's name, address, and phone.
• number of movies for this distributor,
• details of all the movies, one per line
• the total earnings for that distributor,
MovieDriver class
Your driver class should perform the following actions:
• Create at least 6 Movie objects with your choice of movie names, director names, and genres.
• Create 2 Distributor objects with your choice of name, address, and phone number.
• Add 5 different movies to the first distributor object that you created in the previous step. Attempt to add the sixth movie to the same distributor and demonstrate that this attempt does not crash the program, but the operation does not succeed.
• Add 2 movies to the second distributor object.
• Print the two distributors.
• Exercise every method of both the Movie and Distributor classes. Demonstrate that they work. For this, I recommend using the assert statement. Here is an example.
Using assert statement
Suppose you want to check the getter and setter for directorName 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 (1) you had set the director’s name to DIR25 , and (2) 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.
Here is some information about assert, taken verbatim from the JDK documentation:
assert Expression1 ; where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.
One thing you have to remember is that in normal execution, the JVM ignores any assert
statements, so these statements never crash. This makes the program run with little overhead. This also means that you need to enable assertions while testing. For this, use the parameter –ea while executing the program.
java -ea Driver
In Eclipse, you can achieve this as follows.
1. Right click on Driver.java. 2. Choose Run As-> Run Configurations. 3. In the resulting window, click on the Arguments tab. 4. In the VM arguments textbox, type
-ea 5. Click Run.
Remove the above VM argument to disable assertions, if you don’t need it.
50013 - Java Movie Distributor/Solution/Simple Code Files/Distributor.java
50013 - Java Movie Distributor/Solution/Simple Code...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here