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 16 of 16

Thread: Homework - Calendar Assignment

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework - Calendar Assignment

    Hi, I'm stuck on this project. This is the instructions:

    CMPS 135 Programming Assignment 4

    Problem:
    Write a Java program to print out the calendar of any year provided that the day of January 1 of that year is known. (Note: Leap year is a year whose number is divisible by 4. The Feb. of a leap year has 29 days. You will receive extra points if you can print the calendar with 3 months in a row.)

    Specification:
    Use modular programming to write the program.





    Currently, this is the code I have, but the program isn't working correctly:

    import javax.swing.JOptionPane;
     
    public class Assignment4Test
    {
    public static void display()
    { System.out.println(" S M T W T F S");
    System.out.println("---------------------");
    }
     
    public static void main(String args[])
     
    { int year; 
    String yearstr; 
    int startday; 
    String startdaystr; 
    int month; 
    int daysinmonth; 
    String monthname; 
    int spacesforstartday;
    int date; 
     
    yearstr = JOptionPane.showInputDialog("What is the number of the year?");
    year = Integer.parseInt(yearstr);
     
    startdaystr = JOptionPane.showInputDialog("What is the day of January 1? (S-1 M-2 T-3 W-4 T-5 F-6 S-7)");
    startday = Integer.parseInt(startdaystr);
     
    System.out.println(" "+year);
    System.out.println(" ===="); 
     
    for (month=1; month<=12; month++)
    {
    monthname="";
    daysinmonth=0;
    switch (month)
    {
     
    case 1:
     
    monthname="Jan ";
    daysinmonth=31;
    break;
     
    case 2:
     
    monthname="Feb ";
    if (year%4 == 0 && year%100 != 0 || year%400 == 0)
    { daysinmonth = 29; }
    else
    { daysinmonth = 28; }
    break;
     
    case 3:
     
    monthname = "March ";
    daysinmonth = 31;
    break;
     
    case 4:
     
    monthname="April ";
    daysinmonth = 30;
    break;
     
    case 5:
     
    monthname="May ";
    daysinmonth = 31;
    break;
     
    case 6:
     
    monthname="June ";
    daysinmonth = 30;
    break;
     
    case 7:
     
    monthname="July ";
    daysinmonth = 31;
    break;
     
    case 8:
     
    monthname="Aug ";
    daysinmonth = 31;
    break;
     
    case 9:
     
    monthname="Sept ";
    daysinmonth = 30;
    break;
     
    case 10:
     
    monthname="Oct ";
    daysinmonth = 31;
    break;
     
    case 11:
     
    monthname="Nov ";
    daysinmonth = 30;
    break;
     
    case 12:
     
    monthname="Dec ";
    daysinmonth = 31;
    break;
     
    default:
     
    System.out.println("Invalid data");
    break;
     
    }
     
    System.out.println("");
    System.out.println(" "+monthname);
    System.out.println("");
     
    display();
     
    for(spacesforstartday=1;spacesforstartday<=startday;spacesforstartday++)
    {System.out.print(" ");}
     
    for(date=1; date<=daysinmonth; date++)
    { 
    if(startday%7 == 0 && startday != 0)
    {System.out.println();}
    if(date<10)
    {System.out.print(" "+date);}
    else
    {System.out.print(" "+date);}
    startday=startday+1;
    }
     
     
     
    startday%=7;
    System.out.println("");
    System.out.println("");
    }
     
     
     
    System.exit(0);
     
    }}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    program isn't working correctly:
    Please explain. Show what the program currently does and add some comments saying what the output should be.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    It's basically just supposed to print out a calendar, however for some reason the 1st of january always comes out one day early, which throws off the whole calendar.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    Can you post the contents of the console window from when you execute the program? Add some comments saying what is wrong with the output and show what it should be.


    What happened to the formatting of the code? All the statements should NOT start in the first column.
    Statements nested within {}s should be indented 3-4 spaces.
    Unformatted code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    Untitled.jpg
    I uploaded a picture, the formatting was really messed up when copy/pasting it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    Use code tags around the program's output to preserve its formatting.
    Add some comments saying what is wrong with the output.


    What happened to the formatting of the code? All the statements should NOT start in the first column.
    Statements nested within {}s should be indented 3-4 spaces.
    Unformatted code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    import javax.swing.JOptionPane;
     
    public class Assignment4Test
    {
      public static void display()
      { System.out.println("  S  M  T  W  T  F  S");
        System.out.println("---------------------");
      }
     
       public static void main(String args[])
     
      { int year;              
        String yearstr;        
        int startday;              
        String startdaystr;        
        int month;             
        int daysinmonth;             
        String monthname;          
        int spacesforstartday;
        int date;           
     
        yearstr = JOptionPane.showInputDialog("What is the number of the year?");
        year = Integer.parseInt(yearstr);
     
        startdaystr = JOptionPane.showInputDialog("What is the day of January 1? (S-1 M-2 T-3 W-4 T-5 F-6 S-7)");
        startday = Integer.parseInt(startdaystr);
     
        System.out.println("          "+year);
        System.out.println("          ====");      
     
        for (month=1; month<=12; month++)
        {
          monthname="";
          daysinmonth=0;
          switch (month)
          {
     
            case 1:
     
              monthname="Jan ";
              daysinmonth=31;
              break;
     
            case 2:
     
              monthname="Feb ";
              if (year%4 == 0 && year%100 != 0 || year%400 == 0)
              { daysinmonth = 29; }
              else
              { daysinmonth = 28; }
              break;
     
            case 3:
     
              monthname = "March ";
              daysinmonth = 31;
              break;
     
            case 4:
     
              monthname="April ";
              daysinmonth = 30;
              break;
     
            case 5:
     
              monthname="May ";
              daysinmonth = 31;
              break;
     
            case 6:
     
              monthname="June ";
              daysinmonth = 30;
              break;
     
            case 7:
     
              monthname="July ";
              daysinmonth = 31;
              break;
     
            case 8:
     
              monthname="Aug ";
              daysinmonth = 31;
              break;
     
            case 9:
     
              monthname="Sept ";
              daysinmonth = 30;
              break;
     
            case 10:
     
              monthname="Oct ";
              daysinmonth = 31;
              break;
     
            case 11:
     
              monthname="Nov ";
              daysinmonth = 30;
              break;
     
            case 12:
     
              monthname="Dec ";
              daysinmonth = 31;
              break;
     
            default:
     
              System.out.println("Invalid data");
              break;
     
          }
     
          System.out.println("");
          System.out.println("          "+monthname);
          System.out.println("");
     
          display();
     
          for(spacesforstartday=1;spacesforstartday<=startday;spacesforstartday++)
          {System.out.print("   ");}
     
          for(date=1; date<=daysinmonth; date++)
          { 
            if(startday%7 == 0 && startday != 0)
            {System.out.println();}
            if(date<10)
            {System.out.print("  "+date);}
            else
            {System.out.print(" "+date);}
            startday=startday+1;
          }
     
     
     
          startday%=7;
          System.out.println("");
          System.out.println("");
        }
     
     
     
        System.exit(0);
     
      }}

              2013
              ====
     
              Jan 
     
      S  M  T  W  T  F  S
    ---------------------
               1  2  3  4
      5  6  7  8  9 10 11
     12 13 14 15 16 17 18
     19 20 21 22 23 24 25
     26 27 28 29 30 31
     
     
              Feb 
     
      S  M  T  W  T  F  S
    ---------------------
                        1
      2  3  4  5  6  7  8
      9 10 11 12 13 14 15
     16 17 18 19 20 21 22
     23 24 25 26 27 28
     
     
              March 
     
      S  M  T  W  T  F  S
    ---------------------
                        1
      2  3  4  5  6  7  8
      9 10 11 12 13 14 15
     16 17 18 19 20 21 22
     23 24 25 26 27 28 29
     30 31
     
     
              April 
     
      S  M  T  W  T  F  S
    ---------------------
            1  2  3  4  5
      6  7  8  9 10 11 12
     13 14 15 16 17 18 19
     20 21 22 23 24 25 26
     27 28 29 30
     
     
              May 
     
      S  M  T  W  T  F  S
    ---------------------
                  1  2  3
      4  5  6  7  8  9 10
     11 12 13 14 15 16 17
     18 19 20 21 22 23 24
     25 26 27 28 29 30 31
     
     
              June 
     
      S  M  T  W  T  F  S
    ---------------------
      1  2  3  4  5  6  7
      8  9 10 11 12 13 14
     15 16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30
     
     
              July 
     
      S  M  T  W  T  F  S
    ---------------------
            1  2  3  4  5
      6  7  8  9 10 11 12
     13 14 15 16 17 18 19
     20 21 22 23 24 25 26
     27 28 29 30 31
     
     
              Aug 
     
      S  M  T  W  T  F  S
    ---------------------
                     1  2
      3  4  5  6  7  8  9
     10 11 12 13 14 15 16
     17 18 19 20 21 22 23
     24 25 26 27 28 29 30
     31
     
     
              Sept 
     
      S  M  T  W  T  F  S
    ---------------------
         1  2  3  4  5  6
      7  8  9 10 11 12 13
     14 15 16 17 18 19 20
     21 22 23 24 25 26 27
     28 29 30
     
     
              Oct 
     
      S  M  T  W  T  F  S
    ---------------------
               1  2  3  4
      5  6  7  8  9 10 11
     12 13 14 15 16 17 18
     19 20 21 22 23 24 25
     26 27 28 29 30 31
     
     
              Nov 
     
      S  M  T  W  T  F  S
    ---------------------
                        1
      2  3  4  5  6  7  8
      9 10 11 12 13 14 15
     16 17 18 19 20 21 22
     23 24 25 26 27 28 29
     30
     
     
              Dec 
     
      S  M  T  W  T  F  S
    ---------------------
         1  2  3  4  5  6
      7  8  9 10 11 12 13
     14 15 16 17 18 19 20
     21 22 23 24 25 26 27
     28 29 30 31

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    What is the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    January 1st of this year lands on a Tuesday, yet the program assigns it to Wednesday and it throws off the whole calendar.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    How does the program decide what day January 1 is on and use that to do the spacing to put the 1 in the right spot?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    This code gets the variable that assigns the day of the first of January

        startdaystr = JOptionPane.showInputDialog("What is the day of January 1? (S-1 M-2 T-3 W-4 T-5 F-6 S-7)");
        startday = Integer.parseInt(startdaystr);



    This code handles the spacing and the first of the month.

    for(spacesforstartday=1;spacesforstartday<=startday;spacesforstartday++)
          {System.out.print("   ");}
     
          for(date=1; date<=daysinmonth; date++)
          { 
            if(startday%7 == 0 && startday != 0)
            {System.out.println();}
            if(date<10)
            {System.out.print("  "+date);}
            else
            {System.out.print(" "+date);}
            startday=startday+1;
          }
     
     
     
          startday%=7;
          System.out.println("");
          System.out.println("");
        }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    Did you play computer with the code to see what it is doing? You know that it prints extra spaces to push the first 1 to the right 1 day. Look at the code to see why.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    I've messed with the code for quite a while, I can't seem to get it to work correctly without pushing all of the other dates onto a new line.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    Look at what value is put in the startday variable and how it is computed, how the loop control variable starts at 1 vs 0 and the loop terminating test uses <= vs <

    Consider this: Sunday means 0 leading spaces
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework - Calendar Assignment

    The first two variables that you mentioned were necessary, when I altered them the problem worsened.

    Nothing that I'm trying is helping to correct the issue. Do you have any suggested fixed?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Homework - Calendar Assignment

    DId you Consider this: The first day of Sunday means 0 leading spaces

    --- Update ---

    Also posted at Calendar Assignment - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 8
    Last Post: February 12th, 2013, 05:45 AM
  2. Need help with understanding a homework assignment, not asking for answers
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 22
    Last Post: September 14th, 2012, 11:12 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. Homework assignment (beginner)
    By z3ohyr84 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2011, 01:32 PM
  5. Array Homework assignment
    By lich0802 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2011, 08:17 PM

Tags for this Thread