In: Computer Science
CompSci 251: Intermediate Computer ProgrammingLab 3 – 2019
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.
Given Driver
public class MovieDriver {
public static void main (String [] args){
Movie[] myCollection = new Movie[5];
myCollection[0] = new Movie("Batman The Dark Knight", 152, 94);
myCollection[1] = new Movie("Avengers: Endgame", 182, 94);
myCollection[2] = new Movie("The GodFather", 178, 98);
myCollection[3] = new Movie("Suicide Squad", 137, 27);
//TODO
//Initialize the variable below and add it to myCollection at index 4.
//You can pick any movie you wish.
Movie yourMovie;
System.out.println("Here are all the movies in my collection of movies.\n");
for(int i = 0; i < myCollection.length; i++) {
if(myCollection[i] != null) {
myCollection[i].display();
System.out.println();
}
}
System.out.println("_______________________________________________");
System.out.println("\nHere are the Fresh movies.\n");
for(int i = 0; i < myCollection.length; i++) {
if(myCollection[i] != null && myCollection[i].isFresh()) {
System.out.println(myCollection[i].getName() + " is fresh.");
}
}
System.out.println();
System.out.println("Here are the Rotten movies.\n");
for(Movie movieTmp: myCollection){
if (movieTmp != null && !movieTmp.isFresh())
System.out.println(movieTmp.getName() + " is rotten.");
}
System.out.println("_______________________________________________\n");
Movie harryPotter = new Movie("Harry Potter and the Prisoner of Azkaban", 144, 91);
System.out.println("The movie " + harryPotter.getName() + " was created.\n");
System.out.println("Is " + harryPotter.getName() + " a long movie?");
if(harryPotter.getMinutes() > 120) {
System.out.println("Yes, it is a bit long.\n");
} else {
System.out.println("Nope, that isn't too bad.\n");
}
System.out.println("Can I set the minutes of " + harryPotter.getName() + " to a negative number?");
harryPotter.setMinutes(-5);
if(harryPotter.getMinutes() == -5) {
System.out.println("It worked. The runtime is -5 minutes.\n");
} else {
System.out.println("It did NOT work. Negative runtimes are not allowed.\n");
}
System.out.println("Can I set tomato score of " + harryPotter.getName() + " to a negative number?");
harryPotter.setTomatoScore(-100);
if(harryPotter.getTomatoScore() == -100) {
System.out.println("It worked. The score is -100. This movie is terrible according to the site.\n");
} else {
System.out.println("It did NOT work. Negative scores are not allowed.\n");
}
System.out.println("Can I set tomato score of " + harryPotter.getName() + " to a number greater than 100?");
harryPotter.setTomatoScore(101);
if(harryPotter.getTomatoScore() == 101) {
System.out.println("It worked. The score is 101. Best Harry Potter movie ever!\n");
} else {
System.out.println("It did NOT work. Still the best Harry Potter movie out all the movies though.\n");
}
}
}
Please create a file named Movie.java and execute MovieDriver class. Here is the code for Movie class.
public class Movie{
String movieName;
int minutes;
int tomatoScore;
Movie(String movieName ,int
minutes, int tomatoScore){
setName(movieName);
setMinutes(minutes);
setTomatoScore(tomatoScore);
}
public String getName() {
return
movieName;
}
public void setName(String
movieName) {
this.movieName =
movieName;
}
public int getMinutes() {
return
minutes;
}
public void setMinutes(int
minutes) {
if( minutes >
0)
this.minutes = minutes;
}
public int getTomatoScore() {
return
tomatoScore;
}
public void setTomatoScore(int
tomatoScore) {
if(tomatoScore
>= 0 && tomatoScore <= 100)
this.tomatoScore = tomatoScore;
}
public boolean isFresh() {
if(tomatoScore
> 60)
return true;
else
return false;
}
public void display() {
int hrs =
minutes/60 ;
int mins =
minutes - (hrs*60);
String
freshness;
System.out.print("Movie:"+ movieName);
System.out.print(" Length:"+ hrs+"hrs"+mins+"min.");
if(isFresh())
freshness = "Fresh";
else
freshness = "Rotten";
System.out.print(" Tomato Meter:" + freshness);
}
}
In the TODO of the movie driver class add a variable for the movie class . For example add the following line.
myCollection[4] = new Movie("Bahubali" ,190 , 99);