Question

In: Computer Science

Answer the following question in Java. Create a class called Movie. Move should contain: Two instance...

Answer the following question in Java.

Create a class called Movie. Move should contain:

  • Two instance variables: title of type String and rating of type int. Remember to encapsulate them.

  • Two constructors: One that takes a tile and rating as arguments and a copy constructor.

  • Getter and setter methods for both instance variables.

  • A method called getCategory that takes no arguments and return something of type char.

Additional information about the Movie class:

  • Title: The instance should always store the string in all upper case, if the method is called with a string that contains any lower case characters, the lower case characters should be converted to upper case.

  • Rating must be a value between 0 and 10 (inclusive). If the rating provided is outside of that range, the rating should remain unchanged.

  • getCategory: A movie is considered A category if its rating is 9 or 10, B category if the rating is 7 or 8, C category if the rating is 5 or 6, D category if the rating is 3 or 4 and F category otherwise.

Solutions

Expert Solution

Movie Class Code:


public class Movie {
        // Two instance variables: title of type String and rating of type int
        private String title;
        private int rating;

        // Constructor that takes a tile and rating as arguments
        public Movie(String title, int rating) {
                this.title = title.toUpperCase();
                if (rating >= 0 && rating <= 10) {
                        this.rating = rating;
                }
        }

        // Constructor copy constructor
        public Movie(Movie movie) {
                this.title = movie.title;
                this.rating = movie.rating;
        }

        // Setter method to set title of movie
        public void setTitle(String title) {
                this.title = title.toUpperCase();// movie in upper case
        }

        // Setter method to set rating of movie
        public void setRating(int rating) {
                // if entered rating between 0 and 10 (inclusive)
                if (rating >= 0 && rating <= 10) {
                        this.rating = rating;
                }
        }

        // Getter method to get title of movie
        public String getTitle() {
                return title;
        }

        // Getter method to get rating of movie
        public int getRating() {
                return rating;
        }

        // Method getCategory that takes no arguments and return something of type char.
        public char getCategory() {
                // movie is considered A category if its rating is 9 or 10
                if (this.rating == 9 || this.rating == 10) {
                        return 'A';
                }
                // B category if the rating is 7 or 8
                else if (this.rating == 7 || this.rating == 8) {
                        return 'B';
                }
                // C category if the rating is 5 or 6
                else if (this.rating == 5 || this.rating == 6) {
                        return 'C';
                }
                // D category if the rating is 3 or 4
                else if (this.rating == 3 || this.rating == 4) {
                        return 'D';
                }
                // F category otherwise.
                else {
                        return 'F';
                }
        }
}

Images Of Code:


Related Solutions

Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
Question 3: Write the following Java program. A. Create a class called Quadrilateral that contains the...
Question 3: Write the following Java program. A. Create a class called Quadrilateral that contains the following: [4 Marks] • Five double type data fields for: side1, side2, side3, side4, and perimeter • A no-argument constructor that creates a Quadrilateral with all sides equal to 1 • Methods named setSide1(double s1), setSide2(double s2), setSide3(double s3), and setSide4(double s4) to set the values of the four sides • A method named calculatePerimeter( ) to calculate and return the perimeter of the...
Create a class Person with two instance variables of type String called firstName and LastName, an...
Create a class Person with two instance variables of type String called firstName and LastName, an instance variable of type int called age, an instance variables of type int called height (measured in inches), and an instance variable of type double called weight. Define an appropriate constructor that takes initial values for all instance variables and calls the corresponding set methods. Define get and set methods for all instance variables. All set (mutator) methods should validate that reasonable data is...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT