Find and subtract the smallest Period unit from the sum of all units using the minus() method. Note that you need to implement the getSumMinusMin() method to return the result of the subtraction but this doesn't mean that all logic must be there. You can have more methods. Use the calculateDayCount() method to calculate the number of days in a year for comparison.
Computer scienceProgramming languagesJavaAdditional instrumentsEssential standard classesDate and time
Period and Duration classes
The difference between Period units
Report a typo
Sample Input 1:
1989 6 23
1991 5 22
1994 4 21
1993 3 19
1988 2 18Sample Output 1:
P7967Y18M85DWrite a program in Java 17
import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Period> periods = createPeriodList(scanner);
Period result = getSumMinusMin(periods);
System.out.println(result);
}
public static List<Period> createPeriodList(Scanner scanner) {
List<Period> list = new ArrayList<>();
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
list.add(Period.of(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
return list;
}
/**
* Calculates days count in a Period unit.
* Assumes there are no leap years and all months have 30 days
*/
private static int calculateDayCount(Period p) {
return p.getYears() * 365 + p.getMonths() * 30 + p.getDays();
}
private static Period getSumMinusMin(List<Period> periods) {
}
}
___
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.