Question: The program first reads integer lectureCount from input, representing the number of pairs of inputs to be read. Each pair has a string and an

The program first reads integer lectureCount from input, representing the number of pairs of inputs to be read. Each pair has a string and an integer, representing the lecture's topic and duration, respectively. One Lecture object is created for each pair and added to ArrayList lectureList. Write the findAverageLectureDuration() method in the Inventory class to return the average duration of all the Lecture objects as an integer.
Ex: If the input is:
3
Music 97 Fashion 85 Culture 130
then the output is:
Average lecture duration: 104
Note: The ArrayList has at least one element. Inventory.java: import java.util.Scanner;
import java.util.ArrayList;
public class Inventory {
private ArrayList lectureList = new ArrayList();
public void inputLectures(Scanner scnr){
Lecture currLecture;
String currTopic;
int currDuration;
int lectureCount;
int i;
lectureCount = scnr.nextInt();
for (i =0; i < lectureCount; ++i){
currTopic = scnr.next();
currDuration = scnr.nextInt();
currLecture = new Lecture();
currLecture.setTopicAndDuration(currTopic, currDuration);
lectureList.add(currLecture);
}
}
/* Your code goes here */
} Lecture.java: public class Lecture {
private String topic;
private int duration;
public void setTopicAndDuration(String newTopic, int newDuration){
topic = newTopic;
duration = newDuration;
}
public int getDuration(){
return duration;
}
} LectureSystem.java: import java.util.Scanner;
import java.util.ArrayList;
public class LectureSystem {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
Inventory inventory = new Inventory();
inventory.inputLectures(scnr);
System.out.println("Average lecture duration: "+ inventory.findAverageLectureDuration());
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!