In: Computer Science
***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.
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: