-1

i need to modify the following code so that i can input a given year and count its days, considering that leap years have a day more than the others. I think the Scanner function and if statements would be useful here but i am confused on how to modify the code .

public class Homework {

public static void main(final String[] args)
    {
        final String[] months =
                {
                        "January", "February", "March", "April", "May", "June", "July", "August", "September",
                        "October", "November", "December"
                };
        final int[] days = {31, 28 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


        //Print out days for each month and add them
        int total = 0;
        for (int m = 0; m < months.length; m++) {
            System.out.println(months[m] + " has " + days[m] + " days.");
            total += days[m];
        }
        System.out.println("===================\nTotal" + total + " days");


    }
}

Also, i need to calculate the total number of days over all years from 1900 to 1999 (inclusive). The program will be neater if i write a method that takes a year as its parameter and returns the total number of days in that year but again, i am confused about the specifics of it.

Thanks!

  • The only month that would be affected would be February... So, check `m == 1 && isLeapYear(year)`, then print `29` instead of `28` – OneCricketeer Sep 13 '21 at 21:01

3 Answers3

1

java.time

I recommend you use java.time, the modern Date-Time API, to do it.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getDays(2020));
        System.out.println(getDays(2021));
    }

    static int getDays(int year) {
        return LocalDate.of(year, 12, 31).getDayOfYear();
    }
}

Output:

366
365

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Does your homework assignment allow you to use java.time, the modern Java date and time API? It should. It’s what one would unconditionally use in real life, so I’d say it’s what you need to learn to do.

  1. Yes: Just use the Year class and its length() method.
  2. No: Sorry, then you will have to do the math yourself. In the Gregorian calendar a year is a leap year and has 366 days if it’s divisible by 4, except if divisible by 100 it is not except if divisible by 400 it is. So 2000 was a leap year and 2100 will not be. This has been codified into Java thousands of times, just search if you need to see it.

Link: Documentation of Year.length().

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Doing minimum changes to your code, i wrote another loop to iterate through all required years. Here you just need to give start_year and end_year variable their values, also flag variable here will determine if current year is leap year or not. Based on that if month is feb and year is leap, it just increases days by 1 for feburary.

public class MyClass {
    public static void main(final String[] args)
    {
        final String[] months =
                {
                        "January", "February", "March", "April", "May", "June", "July", "August", "September",
                        "October", "November", "December"
                };
        final int[] days = {31, 28 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


        //Print out days for each month and add them
        int start_year = 2000;
        int end_year = 2001;
        
        int total = 0;
        for(int year=start_year ; year<=end_year; ++year){
            //to check if year is prime, flag is true if year is leap else false
            boolean flag = (((year % 4 == 0) && (year % 100 != 0)) ||(year % 400 == 0));
            
            for (int m = 0; m < months.length; m++) {
                if(flag&&m==1){
                    System.out.println(months[m] + " has " + (days[m]+1) + " days.");
                    total += days[m]+1;
                }
                else{
                    System.out.println(months[m] + " has " + days[m] + " days.");
                    total += days[m];
                }
            }
            System.out.println("===================\nTotal" + total + " days");
        }

    }
}