Question

In: Computer Science

***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class...

***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE***

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;
}
}
}

***IMPLEMENTATION CLASS BELOW HAS 3 BLOCKS OF CODE NEEDED. LOOK FOR THE COMMENTS LABELED "BLOCK"***

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);
   }
}

If you are not familiar with JOptionPane you can just code with Scanner and I will change it. Thank you.

Solutions

Expert Solution

Modified program with all three blocks

Program :

ThreatreIncomplete.java


/***IMPLEMENTATION CLASS BELOW HAS 3 BLOCKS OF CODE NEEDED. LOOK FOR THE COMMENTS LABELED "BLOCK"***/

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
          */
          //checking if there is room for another movie or not
                  if(Movie.getNumMovies() < NUM_MOVIES) {
                          movies[Movie.getNumMovies()] = inputMovie(); 
                  }
                  else {
                          //displaying error message
                          JOptionPane.showMessageDialog(null, "Sorry, You can only enter "+NUM_MOVIES+" movies maximum!");
                  }
         
         
         
         /** 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
          */
        //initially taking min as max integer value
                int minLength = Integer.MAX_VALUE;
                //loop for the movies which are available
                //to find the shortest movie
                for(int i=0; i<Movie.getNumMovies(); i++) {
                        
                        if(movies[i].getLength() < minLength) {
                                minLength = movies[i].getLength();
                                aMovie = movies[i];
                        }
                }
         
         
         
         /** 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
       */
      // taking title of each movie
       for(int i=0; i<Movie.getNumMovies(); i++) {
                 summary = summary + movies[i].getTitle()+"\n";
           }
           
       //adding the number of movies to summary
           summary = summary + "Number of Movies Entered : "+Movie.getNumMovies()+"\n";
           //adding shortest movie information to summary
           summary = summary + "Shortest Movie : \nTitle : "+shortestMovie.getTitle()+"\nLength : "+shortestMovie.getLength();
      
              
      /** END: BLOCK #3 **/
              
      JOptionPane.showMessageDialog(null, summary);
   }
}

Movie.java

/***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE***/

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;
        }
    }
}

output screenshots :

Program screenshot:


Related Solutions

Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: Write a method called "public void addFirst(T data)" that does the following: * Adds the element to the front of the deque. * Must be O(1) * @param data the data to add to the front of the deque * @throws java.lang.IllegalArgumentException if data is null Q2:...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry {...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry { private String month; private int day; private int year; //a four digit number. public void writeOutput() { System.out.println(month + " " + day + ", " + year); } public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); month = keyboard.next(); day = keyboard.nextInt(); year = keyboard.nextInt(); } public int getDay() { return day;...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public class PolkaDots { private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList. /** * Create a new list of Circles, starts with 5 default circles. */ public PolkaDots() { dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st circle dots.add(circle1);// add the 1st circle to the arrayList    Circle...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
**JAVA LANGUAGE** This assignment will use the Employee class that you developed for assignment 6. Design...
**JAVA LANGUAGE** This assignment will use the Employee class that you developed for assignment 6. Design two sub- classes of Employee...FullTimeEmployee and HourlyEmployee. A full-time employee has an annual salary attribute and may elect to receive life insurance. An hourly employee has an hourly pay rate attribute, an hours worked attribute for the current pay period, a total hours worked attribute for the current year, a current earnings attribute (for current pay period), a cumulative earnings attribute (for the current...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT