Find the Duration units' average and subtract it from the max Duration unit using the minus() method.
Note that you need to implement the getMaxMinusAvg() method to return the result of the subtraction but this doesn't mean that all logic must be there. You can have more methods.
Computer scienceProgramming languagesJavaAdditional instrumentsEssential standard classesDate and time
Period and Duration classes
The difference between Duration units
Report a typo
Sample Input 1:
2
3
35
300
300Sample Output 1:
PT36H40MWrite a program in Java 17
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Duration> durations = createDurationList(scanner);
Duration result = getMaxMinusAvg(durations);
System.out.println(result);
}
public static List<Duration> createDurationList(Scanner scanner) {
List<Duration> list = new ArrayList<>();
list.add(Duration.of(scanner.nextInt(), ChronoUnit.DAYS));
list.add(Duration.of(scanner.nextInt(), ChronoUnit.HOURS));
list.add(Duration.of(scanner.nextInt(), ChronoUnit.MINUTES));
list.add(Duration.of(scanner.nextInt(), ChronoUnit.MINUTES));
list.add(Duration.of(scanner.nextInt(), ChronoUnit.SECONDS));
return list;
}
public static Duration getMaxMinusAvg(List<Duration> durations) {
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.