Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Calendar Program Help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calendar Program Help

    So i am new to java programming. My University Professor assigned us a Lab to have a user input a year and a month so it displays the correct amount of days in a month etc. He told us to just find the "logic" error in the program and fix it. He also said "NOT" to make a new method or anything at all, just fix something within the program he gave us. I know the error appears when i put in Feb. 2012. This month should have 29 days. The first of the month being a Wednesday, and the last day of the month should be the 29th and also a Wednesday. I have a feeling you have to code in the formula for leap year which is (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) but i do not know where to type this in the given code. Or maybe i don't even need that formula? Again im new to programming. And again he told us NOT to make a new method, just fix the logic error....Any help is appreciated, here is the given code i have to work with.......Thanks

    import java.util.Scanner;

    public class Lab1 {
    /** Main method */
    public static void main(String[] args) {
    // Prompt the user to enter year
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter year
    System.out.print("Enter full year (e.g., 2001): ");
    int year = input.nextInt();

    // Prompt the user to enter month
    System.out.print("Enter month in number between 1 and 12: ");
    int month = input.nextInt();

    // Print calendar for the month of the year
    printMonth(year, month);
    }

    /** Print the calendar for a month in a year */
    static void printMonth(int year, int month) {
    // Print the headings of the calendar
    printMonthTitle(year, month);

    // Print the body of the calendar
    printMonthBody(year, month);
    }

    /** Print the month title, e.g., May, 1999 */
    static void printMonthTitle(int year, int month) {
    System.out.println(" " + getMonthName(month)
    + " " + year);
    System.out.println("-----------------------------");
    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }

    /** Get the English name for the month */
    static String getMonthName(int month) {
    String monthName = null;
    switch (month) {
    case 1: monthName = "January"; break;
    case 2: monthName = "February"; break;
    case 3: monthName = "March"; break;
    case 4: monthName = "April"; break;
    case 5: monthName = "May"; break;
    case 6: monthName = "June"; break;
    case 7: monthName = "July"; break;
    case 8: monthName = "August"; break;
    case 9: monthName = "September"; break;
    case 10: monthName = "October"; break;
    case 11: monthName = "November"; break;
    case 12: monthName = "December";
    }

    return monthName;
    }

    /** Print month body */
    static void printMonthBody(int year, int month) {
    // Get start day of the week for the first date in the month
    int startDay = getStartDay(year, month);

    // Get number of days in the month
    int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

    // Pad space before the first day of the month
    int i = 0;
    for (i = 0; i < startDay; i++)
    System.out.print(" ");

    for (i = 1; i <= numberOfDaysInMonth; i++) {
    if (i < 10)
    System.out.print(" " + i);
    else
    System.out.print(" " + i);

    if ((i + startDay) % 7 == 0)
    System.out.println();
    }

    System.out.println();
    }

    /** Get the start day of month/1/year */
    static int getStartDay(int year, int month) {
    return getDayofWeek(year, month, 1);
    }

    public static int getDayofWeek(int year, int month, int dayOfMonth) {
    if (month == 1) {
    month = 13;
    year--;
    }
    else if (month == 2) {
    month = 14;
    year--;
    }

    int k = year % 100;
    int j = (int)(year / 100);

    int dayOfWeek = (int)(dayOfMonth + (int)((month + 1) * 26.0 / 10)
    + k + (int)(k / 4.0) + (int)(j / 4.0) + 5 * j) % 7;

    return (dayOfWeek + 6) % 7;
    }

    /** Get the number of days in a month */
    static int getNumberOfDaysInMonth(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 ||
    month == 8 || month == 10 || month == 12)
    return 31;

    if (month == 4 || month == 6 || month == 9 || month == 11)
    return 30;

    if (month == 2) return 28;

    return 0; // If month is incorrect
    }

    }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Calendar Program Help


    You only need to modify the getNumberOfDaysInMonth method. Here is what the code should look like...
     
    /** Get the number of days in a month */
    static int getNumberOfDaysInMonth(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 ||
    month == 8 || month == 10 || month == 12)
    return 31;
     
    if (month == 4 || month == 6 || month == 9 || month == 11)
    return 30;
     
    if (month == 2 && year % 4 == 0) return 29; // If the month is 2 and the year is divisible by 4, return 29
     
    if(month == 2) return 28;
     
    return 0; // If month is incorrect



    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calendar Program Help

    omg! it worked!! Thank You! But I can't believe how i couldn't figure out how to write and where to write this 1 line of a simple code......

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Calendar Program Help


    I can see how it would be pretty confusing. I just finished my Intro to Java course, so I'm still kind of new to Java as well. This particular code is kind of hard to navigate due to the placement of certain methods. Glad I could help!


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Calendar Program Help

    Good post Whitesoup12. I wonder why the Prof. wanted to return 0 as an error. I was taught to return -1 instead.

  6. #6
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Calendar Program Help

    if (month == 2 && year % 4 == 0) return 29;
    please improve this for century years too.It will not work for century years.


    if year modulo 400 is 0
    then is_leap_year
    else if year modulo 100 is 0
    then not_leap_year
    else if year modulo 4 is 0
    then is_leap_year
    else
    not_leap_year
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  7. #7
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Calendar Program Help

    Quote Originally Posted by DanBrown View Post
    please improve this for century years too.It will not work for century years.

    Thanks for the reminder, Dan! I have changed the code to account for century years...
    /** Get the number of days in a month */
    static int getNumberOfDaysInMonth(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 ||
    month == 8 || month == 10 || month == 12)
    return 31;
     
    if (month == 4 || month == 6 || month == 9 || month == 11)
    return 30;
     
    if (month == 2 && year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return 29;
     
    if (month == 2) return 28;
     
    return 0; // If month is incorrect
    }


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  8. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calendar Program Help

    yea thanks for the posts WhiteSoup12 it's helping us new programmers understand more. what editing software are you using to write your code? i connect to my schools software from home. i downloaded eclipse but it didn't work for me for some reason

Similar Threads

  1. Calendar date
    By joshft91 in forum Object Oriented Programming
    Replies: 11
    Last Post: November 6th, 2010, 02:47 AM
  2. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  3. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  4. calendar
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2009, 11:02 PM
  5. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM