Question

In: Computer Science

Consider the following problem: You are working at the Johnson Center Cinema. The theatre must keep...

Consider the following problem:

You are working at the Johnson Center Cinema. The theatre must keep track of the titles and lengths of all movies shown in the theatre. You may assume no two movies will have the same length. The theatre manager wants to know which movie is the shortest, so she can consider offering a discount for the movie. At any given time, the theatre can have up to 5 movies playing. Create an object oriented solution that allows the theatre manager to enter up to 5 movies playing. An error message will display when the manager tries to enter more than 5 movies. Display a summary of all of the movie titles, the number of movies entered, and the title and length of the shortest movie.

To solve this problem, you first create a data definition class (no changes are needed to this class). Copy and paste this code into jGrasp.

public class Movie {
   private String title;
   private int length;
   private static int numMovies = 0;
  
   public Movie() {
      this("", 0);
   }
  
   public Movie(String title, int length) {
      this.title = title;
      this.length = length;
      ++numMovies;
   }
  
   public String getTitle() { return this.title; }
   public int getLength() { return this.length; }
   public static int getNumMovies() { return numMovies; }
  
   public void setTitle(String title) {
      this.title = title;
   }
  
   public boolean setLength(int length) {
      if (length > 0) {
         this.length = length;
         return true;
      }
      else {
         return false;
      }
   }
}

Study the incomplete implementation class listed below. Copy and paste this code into jGrasp. Then fill in ONLY the parts where comments suggest code is needed. Do not create/edit code outside of the comment blocks.

import javax.swing.JOptionPane;
public class TheatreIncomplete {
   public static void main(String[] args) {
      final int NUM_MOVIES = 5;
      Movie[] movies = new Movie[NUM_MOVIES];

      int x = 0;
      do {
     
         /** START: BLOCK #1
          * In the block below, enter code that will first check if there is room to enter another movie in the
          * movies array. If there is, call the add the movie to the array using the inputMovie() method.
          * Else, if there is not, display an error message to the user letting them know the maximum nubmer of movies
          * that can be entered.
          * Hint: To check the number of movies already entered, use the getNumMovies() static method
          */
        
        
        
         /** END: BLOCK #1 **/        

      }
      while (JOptionPane.showConfirmDialog(null, "Enter another movie?") == JOptionPane.YES_OPTION);

      Movie shortestMovie = getShortest(movies);
      printSummary(movies, shortestMovie);
   }

   private static Movie inputMovie() {
      Movie aMovie = new Movie();
     
      aMovie.setTitle(JOptionPane.showInputDialog(null, "Enter the title for movie " + Movie.getNumMovies() + ": "));
     
      int length;
      boolean isLengthSet = false;
      do {
         try {
            length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of " + aMovie.getTitle() + " in (minutes)"));
         }
         catch (NumberFormatException e) {
            length = -1;
         }
         isLengthSet = aMovie.setLength(length);
         if (!isLengthSet) {
            JOptionPane.showMessageDialog(null, "Sorry, you entered an invalid movie length. Please try again.");
         }
      } while(!isLengthSet);
     
      return aMovie;
   }
  
   private static Movie getShortest(Movie[] movies) {
      Movie aMovie = null;
     
      if (movies.length > 0) {
        
         /** START: BLOCK #2
          * In the block below, enter code that will find the movie object containing the shortest length
          * Hint: You will need to loop through all movies to find the shortest
          */
        
        
        
         /** END: BLOCK #2 **/
      }
     
      return aMovie;
   }
  
   private static void printSummary(Movie[] movies, Movie shortestMovie) {
      String summary="**Movie Summary**\n";
     
      /** START: BLOCK #3
       * First, using the summary variable declared above, loop through all of the movies entered, appending the title of each
       * movie to the summary. Then, append to the summary the number of movies entered, the title of the shortest movie
      * and the length of the shortest movie
       * Hint: To get the number of movies entered, use the getNumMovies() static method
       * Hint: To get the title and length of the shortest movie, use the object reference passed into the method
       */
     
             
      /** END: BLOCK #3 **/
             
      JOptionPane.showMessageDialog(null, summary);
   }
}

Solutions

Expert Solution

Movie.java

public class Movie {

private String title;
private int length;
private static int numMovies = 0;

public Movie() {
this("", 0);
}

public Movie(String title, int length) {
this.title = title;
this.length = length;
++numMovies;
}

public String getTitle() {
return this.title;
}

public int getLength() {
return this.length;
}

public static int getNumMovies() {
return numMovies;
}

public void setTitle(String title) {
this.title = title;
}

public boolean setLength(int length) {
if (length > 0) {
this.length = length;
return true;
} else {
return false;
}
}
}

TheatreIncomplete.java (Main class)

import javax.swing.JOptionPane;

public class TheatreIncomplete {
  
public static void main(String[] args) {
final int NUM_MOVIES = 5;
Movie[] movies = new Movie[NUM_MOVIES];
int x = 0;
do
{
/** START: BLOCK #1
* In the block below, enter code that will first check if there is room to enter another movie in the
* movies array. If there is, call the add the movie to the array using the inputMovie() method.
* Else, if there is not, display an error message to the user letting them know the maximum number of movies
* that can be entered.
* Hint: To check the number of movies already entered, use the getNumMovies() static method
*/
if(Movie.getNumMovies() == NUM_MOVIES)
{
System.out.println("Sorry, you can enter upto a maximum of " + NUM_MOVIES + " movies!\n");
return;
}
movies[x++] = inputMovie();

/** END: BLOCK #1 **/
}while (JOptionPane.showConfirmDialog(null, "Enter another movie?") == JOptionPane.YES_OPTION);
Movie shortestMovie = getShortest(movies);
printSummary(movies, shortestMovie);
}
  
private static Movie inputMovie() {
Movie aMovie = new Movie();

aMovie.setTitle(JOptionPane.showInputDialog(null, "Enter the title for movie " + Movie.getNumMovies() + ": "));

int length;
boolean isLengthSet = false;
do {
try {
length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of " + aMovie.getTitle() + " in (minutes)"));
} catch (NumberFormatException e) {
length = -1;
}
isLengthSet = aMovie.setLength(length);
if (!isLengthSet) {
JOptionPane.showMessageDialog(null, "Sorry, you entered an invalid movie length. Please try again.");
}
} while (!isLengthSet);

return aMovie;
}
  
private static Movie getShortest(Movie[] movies) {
Movie aMovie = null;

if (movies.length > 0) {

/**
* START: BLOCK #2 In the block below, enter code that will find the
* movie object containing the shortest length Hint: You will need
* to loop through all movies to find the shortest
*/
  
int minLength = movies[0].getLength();
int minIndex = 0;
for(int i = 0; i < movies.length; i++)
{
if(movies[i] != null && movies[i].getLength() < minLength)
{
minLength = movies[i].getLength();
minIndex = i;
}
}
aMovie = movies[minIndex];
  
/**
* END: BLOCK #2 *
*/
}
return aMovie;
}
  
private static void printSummary(Movie[] movies, Movie shortestMovie) {
String summary = "**Movie Summary**\n";

/**
* START: BLOCK #3 First, using the summary variable declared above,
* loop through all of the movies entered, appending the title of each
* movie to the summary. Then, append to the summary the number of
* movies entered, the title of the shortest movie and the length of the
* shortest movie Hint: To get the number of movies entered, use the
* getNumMovies() static method Hint: To get the title and length of the
* shortest movie, use the object reference passed into the method
*/
  
for(int i = 0; i < movies.length; i++)
{
if(movies[i] != null)
summary += (i + 1) + ". " + movies[i].getTitle() + "\n";
}
summary += "\nNumber of movies entered = " + Movie.getNumMovies() + "\n"
+ "Shortest Movie:\n"
+ "Title: " + shortestMovie.getTitle() + "\n"
+ "Length: " + shortestMovie.getLength() + " minutes";
  
/**
* END: BLOCK #3 *
*/
JOptionPane.showMessageDialog(null, summary);
}
}

******************************************************** SCREENSHOT ********************************************************


Related Solutions

Suppose you are working as a portfolio manager for Goldman Sacks and advising Johnson & Johnson,...
Suppose you are working as a portfolio manager for Goldman Sacks and advising Johnson & Johnson, a U.S. Pharmaceutical company, that expects to receive a payment of 500 million Japanese yen in 180 days for goods exported to Japan. The current spot exchange rate is 100 yen per U.S. dollar (e yen/$=100, or e $/yen =0.01). You are concerned that due to recent events in Japan, the U.S. dollar is expected to appreciate against the yen over the next 6...
Imagine you are a nurse working in a surgical theatre, or in imaging. A patient tells...
Imagine you are a nurse working in a surgical theatre, or in imaging. A patient tells you that they have to go in for a scan and that it is " of a longitudinal sagittal section through the left thorax and abdomen midway between the sternum and shoulder" 1. in lay terms describe the orientation of this scan to the patient 2. name as many organs as possible that you could see in this scan 3. furthermore, describe what you...
You are working in a senior center. The primary population served in this center are those...
You are working in a senior center. The primary population served in this center are those who have suffered a stroke in the past and required some sort of rehabilitation during recovery. The clients may or may not still have some sort of neurological deficits (think eating, fine motor skills or mobility).   You have determined that this ADA risk test would be useful to use in your clinic. You will need to develop educational materials to be included in this risk...
Consider the industry in which you are working. (If you are not currently working consider the...
Consider the industry in which you are working. (If you are not currently working consider the industry where you worked before returning to school, instead. Or if you never worked before, pick an industry of interest to you.) What were the key forces shaping the nature of competition and the opportunities for making profit in that industry? What, if anything, did firms do to insulate themselves from these forces? Continuing with the industry you picked above, what were substitutes and...
Please answer the following questions: 1) You are working at a call center at NYU calling...
Please answer the following questions: 1) You are working at a call center at NYU calling alumni and trying to get them to donate money to the scholarship program (because God knows you the alumni didn’t drop enough of their money there already). Anyway, you realize that over the last month you average 9 donations per 4 hour shift. You are scheduled to work 7 hours Tuesday, what is the probability that get at least 16 donations? 2) You have...
You are working at a bariatric center and a patient comes to you for pre-operative exercise...
You are working at a bariatric center and a patient comes to you for pre-operative exercise counseling. Your role is to assess and counsel/ prescribe patients on exercise prior and post-surgery. With this, each patient must participate in at least 8 weeks of an exercise program prior to surgery. (10 points) Patient Profile: the patient is 48 years old, weight is 345 pounds; BMI= 57 kg/m2. She had a recent oral glucose tolerance test result of 285 mg/dl, TC= 290...
You are working at a bariatric center and a patient comes to you for pre-operative exercise...
You are working at a bariatric center and a patient comes to you for pre-operative exercise counseling. Your role is to assess and counsel/ prescribe patients on exercise prior and post-surgery. With this, each patient must participate in at least 8 weeks of an exercise program prior to surgery. (10 points) Patient Profile: the patient is 48 years old, weight is 345 pounds; BMI= 57 kg/m2. She had a recent oral glucose tolerance test result of 285 mg/dl, TC= 290...
You are working at a bariatric center and a patient comes to you for pre-operative exercise...
You are working at a bariatric center and a patient comes to you for pre-operative exercise counseling. Your role is to assess and counsel/ prescribe patients on exercise prior and post-surgery. With this, each patient must participate in at least 8 weeks of an exercise program prior to surgery. (10 points) Patient Profile: the patient is 48 years old, weight is 345 pounds; BMI= 57 kg/m2. She had a recent oral glucose tolerance test result of 285 mg/dl, TC= 290...
You are working at a bariatric center and a patient comes to you for pre-operative exercise...
You are working at a bariatric center and a patient comes to you for pre-operative exercise counseling. Your role is to assess and counsel/ prescribe patients on exercise prior and post-surgery. With this, each patient must participate in at least 8 weeks of an exercise program prior to surgery. Patient Profile: the patient is 48 years old, weight is 345 pounds; BMI= 57 kg/m2. She had a recent oral glucose tolerance test result of 285 mg/dl, TC= 290 mg/dl; TG=...
You are working at a bariatric center and a patient comes to you for pre-operative exercise...
You are working at a bariatric center and a patient comes to you for pre-operative exercise counseling. Your role is to assess and counsel/ prescribe patients on exercise prior and post-surgery. With this, each patient must participate in at least 8 weeks of an exercise program prior to surgery. Patient Profile: the patient is 48 years old, weight is 345 pounds; BMI= 57 kg/m2. She had a recent oral glucose tolerance test result of 285 mg/dl, TC= 290 mg/dl; TG=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT