0

I have a project due soon. We have to write a program that asks for a year and prints out a calendar. We can only use one while, one for, and one switch loop. (and no arrays! ugh) Im having trouble figuring out how to print out the days of each month, starting with the first day of the first week, as most months will not start on Sunday.

import java.util.*;
import java.util.Calendar;
class Lab2 {
    public static void main(String[] args) {
    Scanner user = new Scanner(System.in);
    System.out.print("What year do you want to view? ");
    int year = user.nextInt();
    System.out.printf("%12d\n", year);
    System.out.println();
    boolean leap = isLeap(year);    
    int firstDay = JulianDate(year);
    monthLoop(year, firstDay, leap);
}

public static boolean isLeap(int year) {
    boolean verdict = false;
    if (year % 100 == 0 && year % 400 == 0) {
        verdict = true;
    }
    if(year % 100 != 0 && year % 4 == 0) {
        verdict = true;
    }
    return verdict;
}


public static int JulianDate(int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.DAY_OF_YEAR, 1);
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    return dayOfWeek;
}

public static void monthLoop(int year, int firstDay, boolean leap) {
    for(int i=1; i <= 12; i++) {
        switch (i) {
            case 1: System.out.printf("%13s\n", "January");
                    break;
            case 2: System.out.printf("%13s\n", "February");
                    break;
            case 3: System.out.printf("%12s\n", "March");
                    break;
            case 4: System.out.printf("%12s\n", "April");
                    break;
            case 5: System.out.printf("%11s\n", "May");
                    break;
            case 6: System.out.printf("%11s\n", "June");
                    break;
            case 7: System.out.printf("%11s\n", "July");
                    break;
            case 8: System.out.printf("%13s\n", "August");
                    break;
            case 9: System.out.printf("%14s\n", "September");
                    break;
            case 10: System.out.printf("%13s\n", "October");
                    break;
            case 11: System.out.printf("%14s\n", "November");
                    break;
            case 12: System.out.printf("%14s\n", "December");
                    break;                      
        }

    System.out.println("S  M  Tu W  Th F  S");


    }

}

}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Andex24
  • 43
  • 1
  • 6
  • 1
    It seems like you're allowed to use `Calendar` based on your code, which should have a lot of it covered for you, including month and day names, and leap-year fun... Take a closer look at all the stuff that class will do for you - it's very likely you can just keep incrementing the day in your main loop. – Krease Feb 12 '14 at 03:04
  • `import java.util.calendar;` java has a built in calendar class that has method for leap years, days, everything. You can extend the class off of yours – ChriskOlson Feb 12 '14 at 03:33

1 Answers1

0

You would get the day of the week, as in this post, of the first day of the month and then start the counter from there.

How to get the day of the week in Java

For instance, if the day of the week was 5, you would put 4 "blanks" before the first date. The trick is that when you are doing your mod, to determine if there should be a new line, it would be (dayofMonth + firstDayOfWeekOfMonth) % 7

Community
  • 1
  • 1
Brian Hoover
  • 7,861
  • 2
  • 28
  • 41