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
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 Lecture1exercise-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.
Here is the movie class:
public class MOVIE {
private String name;private String
directorName;private double earnings;private int genre;
public MOVIE(String name,String directorName,int
genre) {
this.name=name;
this.directorName=directorName;
this.genre=genre;
earnings=0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirectorName() {
return directorName;
}
public void setDirectorName(String directorName)
{
this.directorName =
directorName;
}
public double getEarnings() {
return earnings;
}
public int getGenre() {
return genre;
}
public void setGenre(int genre) {
this.genre = genre;
}
public void addToEarning(double amount) {
earnings=earnings+amount;
}
public boolean equals(MOVIE movie1,MOVIE movie2)
{
boolean result=false;
if((movie1.getName().equalsIgnoreCase(movie2.getName()))&&
(movie1.getDirectorName().equalsIgnoreCase(movie2.getDirectorName()))&&(movie1.getGenre()==movie2.getGenre()))
{
result=true;
}return result;
}
public void tostring() {
System.out.println("MOVIE
NAME:"+this.getName()+" "+"DirectorName:"+this.getDirectorName()+"
"+"Genre:"+this.getGenre());
}
}
here is the Distributor Class
import java.util.Arrays;
public class Distributor {
String name;
String address;
String phone;
MOVIE [] movies=new MOVIE[5];
int numberOfMovies;
public Distributor(String name,String address,String phone) {
this.name=name;
this.address=address;
this.phone=phone;
Arrays.fill(movies, null);
numberOfMovies=0;
}
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[] getArr() {
return Arrays.copyOf(movies, numberOfMovies);
}
public boolean addMovie(MOVIE movie) {
if(numberOfMovies<movies.length) {
movies[numberOfMovies++]=movie;
return true;
} return false;
}
public boolean addMovie(String name,String directorName,int
genre,double earnings) {
MOVIE movie =new MOVIE(name,directorName,genre);
movie.addToEarning(earnings);
if(numberOfMovies<movies.length) {
movies[numberOfMovies++]=movie;
return true;
} return false;
}
public int totalNumMovies() {
return numberOfMovies;
}
public double findTotalEarnings() {
double total=0;
for(int i=0;i<movies.length;i++) {
total=total+movies[i].getEarnings();
}
return total;
}
public double comedyEarnings() {
double total=0;
for(int i=0;i<movies.length;i++) {
if(movies[i].getGenre()==0) {
total=total+movies[i].getEarnings();
}
}return total;
}
public boolean addEarnings(String name,double earnings) {
for(int i=0;i<movies.length;i++) {
if(movies[i].getName().equalsIgnoreCase(name)) {
movies[i].addToEarning(earnings);
return
true;
}
}return false;
}
public int getNumGenre(int genre) {
int count=0;
for(int i=0;i<movies.length;i++) {
if(movies[i].getGenre()==genre)
count++;
}
return count;
}
public static double calculateTax(int taxRate,Distributor
distributor) {
double
totalEarning=distributor.findTotalEarnings();
return totalEarning*(taxRate/100);
}
public boolean equals(Distributor distributor1,Distributor
distributor2) {
return
distributor1.getName().equalsIgnoreCase(distributor2.getName());
}
public String toString() {
System.out.println("NAME:"+this.getName()+"
"+"ADDRESS:"+this.getAddress()+" "+"PHONE:"+this.getPhone()+" "+"NO
OF MOVIES:"+this.numberOfMovies);
for(int i=0;i<this.totalNumMovies();i++) {
movies[i].tostring();
}return null;
}
}
Here is the Driver class:
public class Driver {
public static void main(String[] args) {
MOVIE movie1=new MOVIE("Mission: Impossible","Brian De
Palma",0);
MOVIE movie2=new MOVIE("Mission: Impossible2","John Woo",0);
MOVIE movie3=new MOVIE("Mission: Impossible3","j.Adams",0);
MOVIE movie4=new MOVIE("Mission: Impossible4","Brad Bird",0);
MOVIE movie5=new MOVIE("Mission: Impossible5","Christopher
Mcquarrie",0);
MOVIE movie6=new MOVIE("Mission: Impossible6","Christopher
Nolen",0);
Distributor distributor1=new Distributor("sam
Adams","San-Francisco","123455867");
Distributor distributor2=new Distributor("Eva
angela","San-martin","9845664734");
distributor1.addMovie(movie1);
distributor1.addMovie(movie2);
distributor1.addMovie(movie3);
distributor1.addMovie(movie4);
System.out.println(distributor1.addMovie(movie5));
//After adding 5 movies the return value is true.That means the
movie was added successfully.
//also the program continues
System.out.println(distributor1.addMovie(movie6));
//But when we add the 6th movie array is full,return value is false
,the movie was not added.
//also the program continues execution.
distributor2.addMovie(movie1);
distributor2.addMovie(movie2);
distributor2.toString();
//after printing the distributor info we get the two movies info
and
//the info of the distributor.
}
}
//I am also attaching the code overview and the live working:
as you can see at the output there is no errors and also when we add more than five movies the program does not get stopped but the movie does not get added.
//if you have any queries do comment.
//Please rate my answer as per your liking.
Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you...
A video store is made up of movies (and lots of other stuff too - but let's keep it simple) - so start by defining a Movie class, then define the VideoStore class and finally a test for it all. Movie Class: Instance fields: title (String) star (String) isCheckedOut (boolean) numTimesCheckedOut (int) Methods: public String getTitle() Returns title of movie public String getStar() Returns star of the movie public int getNumTimesCheckedOut() Returns number of times movie has been checked out...
LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT List by creating a list of movies. This assignment will help you practice: multiple file programming, classes, pablic and private methods, dynamie memory, constructors and destructors, arrays and files Implementation the required classes This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of There are two classes to implement: Movie and MovieList. As you can see...
Introduction In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist. The Base Class, Derived Class and Test Client Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need...
*Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows: For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method. For each...
Java Create four classes: 1. An Animal class that acts as a superclass for Dog and Bird 2. A Bird class that is a descendant of Animal 3. A Dog class that is a descendant of Animal 4. A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: · one instance variable, a private String variable named name · a single constructor that takes one argument, a String, used to set...
***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...
Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...
You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...
WONT COMPILE ERROR STRAY / TEXT.H AND CPP PROGRAM SAYING NOT DECLARED HAVE A DRIVER WILL PASTE AT BOTTOM MOVIES.CPP #include "movies.h" #include "Movie.h" Movies *Movies::createMovies(int max) { //dynamically create a new Movies structure Movies *myMovies = new Movies; myMovies->maxMovies = max; myMovies->numMovies = 0; //dynamically create the array that will hold the movies myMovies->moviesArray = new Movie *[max]; return myMovies; } void Movies::resizeMovieArray() { int max = maxMovies * 2; //increase size by 2 //make an array that is...
Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is to practice inheritance. To get credit for the lab, you need to demonstrate to the student helper that you have completed all the requirements. Question 1: Consider the following detailed inheritance hierarchy diagram in Fig. 1. 1) The Person.java constructor has two String parameters, a first name and a last name. The constructor initializes the email address to the first letter of the first...