In: Computer Science
Introduction
This lab will focus on creating a Movie class conforming to the diagram above. From this class Movie objects can be instantiated and used in a running program. I have provided a mostly completely driver, you will need to add a few lines of code where specified.
What you need to do
Declare you instance variables at the top of the class.
Write a specifying constructor. No default constructor is required.
Write getName and setName. There are no restrictions on the setting the name.
Write getMinutes and setMinutes. When setting minutes, it is not allowed to be negative.
Write getTomatoScore and setTomatoScore. When setting tomato score, score is not allowed
to be negative or over 100.
Write isFresh. A score is considered fresh if it is 60 or above. It is rotten otherwise.
Write display. Print out the name, the total minutes, and if the movie is “Fresh” or “Rotten”.
Remember, you have method which tells you this now.
The driver is mostly complete. I placed one TODO, add a new movie of your choice to the array
that stores movies (called myCollection) in the driver.
Your output should match the output below plus the one movie you add to it. Pay attention to how objects are accessed in the driver. See your TA when your output is correct.
Output:
Here are all the movies in my collection of movies.
Movie: Batman The Dark Knight Length: 2hrs 32min.
Tomato Meter: Fresh
Movie: Avengers: Endgame Length: 3hrs 2min. Tomato Meter: Fresh
Movie: The GodFather Length: 2hrs 58min. Tomato Meter: Fresh
Movie: Suicide Squad Length: 2hrs 17min. Tomato Meter: Rotten
//The movie you add will print out here.
_______________________________________________ Here are the Fresh movies.
Batman The Dark Knight is fresh. Avengers: Endgame is
fresh.
The GodFather is fresh.
Here are the Rotten movies. Suicide Squad is rotten.
//The movie you add will print out here, it’s score will determine if fresh or not.
_______________________________________________
The movie Harry Potter and the Prisoner of Azkaban was created.
Is Harry Potter and the Prisoner of Azkaban a long movie? Yes, it is a bit long.
Can I set the minutes of Harry Potter and the Prisoner of
Azkaban to a negative number?
It did NOT work. Negative runtimes are not allowed.
Can I set tomato score of Harry Potter and the Prisoner of
Azkaban to a negative number?
It did NOT work. Negative scores are not allowed.
Can I set tomato score of Harry Potter and the Prisoner of
Azkaban to a number greater than 100?
It did NOT work. Still the best Harry Potter movie out all the
movies though.
Movie.java
public class Movie{ public String movieName; public int tomatoScore; public int minutes; public Movie(String mName,int tScore,int min){ movieName = mName; tomatoScore = tScore; minutes = min; } public void setName(String mName){ movieName = mName; } public String getName(){ return movieName; } public void setTomatoScore(int tScore){ if(tScore < 0){ System.out.println("Tomato Score cannot be negative"); return; } if(tScore > 100){ System.out.println("Tomato Score cannot be greater than 100"); } tomatoScore = tScore; } public int getTomatoScore(){ return tomatoScore; } public void setMinutes(int min){ if(min < 0){ System.out.println("Running time cannot be negative"); return; } minutes = min; } public int getMinutes(){ return minutes; } public boolean isFresh(){ if(tomatoScore >= 60){ return true; } else return false; } public int getHours(){ return minutes/60; } public String runningTime(){ int hrs = getHours(); int min = minutes - (60 * hrs); String time = "" + hrs + "hrs " + min + "min."; return time; } public void display(){ String time = runningTime(); String freshness; if(isFresh()){ freshness = "Fresh"; } else{ freshness = "Rotten"; } System.out.println("Movie: " + movieName + " Length: " + runningTime() + " Tomato Meter: " + freshness); } }
MovieTester.java
public class MovieTester { public static void main(String... args){ Movie movie = new Movie("myMovie",75,200); movie.display(); movie.setTomatoScore(200); } }
Output:
I could not provide you the exact way that you require because you haven't provided any details about the diagram and the driver code.
But I hope the functions that I have given you will help.
Please appreciate the solution if you find it helpful.
If you have any doubts in the solution, feel free to ask me in the comment section.