DocViewer Zoom Pages Practice Final Exam Program 1.) Use the text file with the
ID: 3915384 • Letter: D
Question
DocViewer
Zoom
Pages
Practice Final Exam Program
1.) Use the text file with the following data:
Movie Name, duration
2.) You will be given a Movie class that will contain the 2 fields, a constructor to initialize the 2
fields, and a toString method. It will also have the appropriate getters and setters in the Movie
class.
3.) You will need to create a Driver class that will read the input file. You will create a Movie object
per record in the file, and store the Movie object in an array of Movies. The array of Movies will
be created as a static global variable, before the main method.
4.) Write a method in the driver class that will find the movie with the longest duration and the
shortest duration. The method should also add up all the movie durations, so that after the
loop, you can calculate the average duration of all movies.
Annotations
Explanation / Answer
1.Create MovieDuration.txt file which contains list of movieNames and duration ,save it any drive
2.movie and driver class as follows
Public class Driver{
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("Movie.txt"));
input.useDelimiter("-| ");
double shortestDuration;
double longestDuration;
Movie[] movies = new Movie[0];
while(input.hasNext()) {
String movieName = input.next();
double duration = Double.valueOf(input.next().substring(1));
Movie newMovie = new Movie(movieName, duration);
movies = addMovie(movies, newMovie);
}
for (Movie : movies) {
System.out.println(Movie);
}
for (int i=0;i<n;i++)
double highDuration= movies.duration [0];
for(int i=1;i<=n;j++){
if(movies.duration [i]> highDuration)
{
shortestDuration= highDuration;
longestDuration = movies.duration [i];
}
System.out .print(“shortest duration”,+ shortestDuration,+” “,”longest duration”,+ longestDuration);
}
private static Movie[] addMovie(Movie[] movies, Movie MovieToAdd) {
Movie[] newMovies = new Movie[movies.length + 1];
System.arraycopy(movies, 0, newMovies, 0, movies.length);
newMovies[newMovies.length - 1] = MovieToAdd;
return newMovies;
}
}
public static class Movie {
protected String movieName;
protected double duration;
private static NumberFormat formatter = new DecimalFormat("#0.00");
public Movie(String movieName, double duration) {
this.movieName = movieName;
this.duration = duration;
}
@Override
public String toString() {
return String.format("%s movieName: %s duration: %d ",
movieName, formatter.format(duration));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.