Use the text file with the following data: Movie Name, duration You will be give
ID: 3859699 • Letter: U
Question
Use the text file with the following data:
Movie Name, duration
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.
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.
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.
Explanation / Answer
MovieDurations.txt(Save this file under D Drive.Then the path of the file pointing to it is D:\MovieDurations.txt)
The 400 Blows
99
La Haine
98
The Godfather
175
Man Bites Dog
95
The Departed
151
White Heat
114
Duck Soup
68
Mafioso
105
Stalag 17
120
The Shawshank Redemption
142
The Dark Knight
152
___________________
Movie.java
public class Movie {
//Declaring instance variables
private String name;
private int duration;
//Parameterized constructor
public Movie(String name, int duration) {
super();
this.name = name;
this.duration = duration;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Movie [name=" + name + ", duration=" + duration + "]";
}
}
__________________________
package org.students;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
//Creating An array of Movie Class objects
static ArrayList < Movie > movies = new ArrayList < Movie > ();
public static void main(String[] args) throws FileNotFoundException {
//Declaring variables
String name;
int duration;
int highest, lowest;
double average;
//Reading the data from the file
Scanner sc = new Scanner(new File("D:\MovieDurations.txt"));
while (sc.hasNext()) {
name = sc.nextLine();
duration = sc.nextInt();
movies.add(new Movie(name, duration));
if (sc.hasNext())
sc.nextLine();
}
//Closing the input data file
sc.close();
//Calling the methods
longestDuration();
shortestDuration();
average = averageDuration();
System.out.printf("The Average duration of all movies is %.2f", average);
}
//This method will calculate and displays the average of all movies duration
private static double averageDuration() {
double tot = 0.0, avg = 0.0;
for (int i = 0; i < movies.size(); i++) {
tot += movies.get(i).getDuration();
}
return (tot / movies.size());
}
//This method will find the movie with highest duration
private static void shortestDuration() {
int low = movies.get(0).getDuration();
String name = null;
for (int i = 0; i < movies.size(); i++) {
if (low > movies.get(i).getDuration()) {
low = movies.get(i).getDuration();
name = movies.get(i).getName();
}
}
System.out.println(name + " has the lowest duration " + low);
}
//This method will find the movie with lowest duration
private static void longestDuration() {
int high = movies.get(0).getDuration();
String name = null;
for (int i = 0; i < movies.size(); i++) {
if (high < movies.get(i).getDuration()) {
high = movies.get(i).getDuration();
name = movies.get(i).getName();
}
}
System.out.println(name + " has the highest duration " + high);
}
}
______________________
Output:
The Godfather has the highest duration 175
Duck Soup has the lowest duration 68
The Average duration of all movies is 119.91
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.