#Need help with calendar program(new to java)

5 messages · Page 1 of 1 (latest)

fickle bison
#

import java.util.Scanner;

public class CalendarGo { 

    public static int firstMethod(int year) { //getYearOffset
        int daysS = 0;
    for (int i = 1970; i < year; i++) {
      if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0)) {
        daysS += 366;
      } else {
        daysS += 365;
      }
    }
        int d = (4 + daysS) % 7;
        return d;
    }

    public static int secondMethod(int month, int yearOffset) { //getMonthOffset
        int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (month == 2 && isLeapYear(year)) days[month] = 29;
        int offset = (yearOffset + ((31*(month-1))/12)) % 7;
        return offset;
    }

    public static void thirdMethod(int month, int yearOffset, int monthOffset) { //printCalendar
        String[] months = {"", "January", "February", "March", "April", "May", "June", "July",
                            "August", "September", "October", "November", "December"};
        int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if (month == 2 && isLeapYear(year)) days[month] = 29;

        System.out.println("   " + months[month] + " " + yearOffset);
        System.out.println(" S  M Tu  W Th  F  S");

        for (int i = 0; i < monthOffset; i++)
            System.out.print("   ");
        for (int i = 1; i <= days[month]; i++) {
            System.out.printf("%2d ", i);
            if (((i + monthOffset) % 7 == 0) || (i == days[month]))
                System.out.println();
        }
        System.out.println();
    }
    
    

    public static boolean isLeapYear(int year) { //method checks if leap year or not.
        if  ((year % 4 == 0) && (year % 100 != 0)) return true;
        if  (year % 400 == 0) return true;
        return false;
    }

    public static void main(String[] args) {
        int[] a = {1,2,3,4,5,6,7,8,9,10,11,12};
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a year between 1970 - 2030 :: ");
        int year = sc.nextInt();
        int yearOffset = firstMethod(year);
        for (int month : a) { //runs for loop for same index of a and month array
            int monthOffset =  secondMethod(month, yearOffset);
            thirdMethod(month, year, monthOffset);
        }
        
    }
}

It gives a cannot find symbol for secondMethod and thirdMethod. when i add year as parameter for them, the program prints the calendar wrong

wide ironBOT
#

This post has been reserved for your question.

Hey @fickle bison! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

zinc oak
#

then uh, fix it?

#

you need the year from somewhere, no?